mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-31 20:51:46 +01:00
improve RenameSubKeyYamlRector to match any nesting
This commit is contained in:
parent
57f33d7c87
commit
19c9ed6bcb
@ -8,39 +8,77 @@ use Rector\YamlRector\Contract\YamlRectorInterface;
|
||||
final class RenameSubKeyYamlRector implements YamlRectorInterface
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
* @var string[]
|
||||
*/
|
||||
private $mainKey;
|
||||
private $pathsToNewKeys = [];
|
||||
|
||||
/**
|
||||
* @var string
|
||||
* @var string|null
|
||||
*/
|
||||
private $subKey;
|
||||
private $activePathPattern;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
* @var string[]
|
||||
*/
|
||||
private $newKey;
|
||||
private $activePathSteps = [];
|
||||
|
||||
public function __construct(string $mainKey, string $subKey, string $newKey)
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
private $activeNewKey;
|
||||
|
||||
/**
|
||||
* @param string[] $pathsToNewKeys
|
||||
*/
|
||||
public function __construct(array $pathsToNewKeys)
|
||||
{
|
||||
$this->mainKey = $mainKey;
|
||||
$this->subKey = $subKey;
|
||||
$this->newKey = $newKey;
|
||||
$this->pathsToNewKeys = $pathsToNewKeys;
|
||||
}
|
||||
|
||||
public function isCandidate(string $content): bool
|
||||
{
|
||||
return (bool) Strings::match($content, $this->createPattern());
|
||||
$this->activePathPattern = null;
|
||||
$this->activePathSteps = [];
|
||||
$this->activeNewKey = null;
|
||||
|
||||
foreach ($this->pathsToNewKeys as $path => $newKey) {
|
||||
$pathSteps = Strings::split($path, '#[\s+]?>[\s+]?#');
|
||||
$pathPattern = $this->createPattern($pathSteps);
|
||||
|
||||
if ((bool) Strings::match($content, $pathPattern)) {
|
||||
$this->activePathSteps = $pathSteps;
|
||||
$this->activePathPattern = $pathPattern;
|
||||
$this->activeNewKey = $newKey;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function refactor(string $content): string
|
||||
{
|
||||
return Strings::replace($content, $this->createPattern(), '$1' . $this->newKey . '$3');
|
||||
$replacement = '';
|
||||
for ($i = 1; $i < count($this->activePathSteps); ++$i) {
|
||||
$replacement .= '$' . $i;
|
||||
}
|
||||
|
||||
$replacement .= preg_quote($this->activeNewKey) . ': ';
|
||||
$replacement .= '$' . ($i + 1);
|
||||
|
||||
return Strings::replace($content, $this->activePathPattern, $replacement);
|
||||
}
|
||||
|
||||
private function createPattern(): string
|
||||
/**
|
||||
* @param string[] $pathSteps
|
||||
*/
|
||||
private function createPattern(array $pathSteps): string
|
||||
{
|
||||
return sprintf('#(^%s:\s+)(%s)(:)#s', preg_quote($this->mainKey), preg_quote($this->subKey));
|
||||
$pattern = '';
|
||||
foreach ($pathSteps as $pathStep) {
|
||||
$pattern .= sprintf('(%s:\s+)', preg_quote($pathStep));
|
||||
}
|
||||
|
||||
return '#^' . $pattern . '#s';
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,4 @@
|
||||
services:
|
||||
Rector\YamlRector\Rector\RenameSubKeyYamlRector:
|
||||
- 'main_key'
|
||||
- 'old_sub_key'
|
||||
- 'new_sub_key'
|
||||
$pathsToNewKeys:
|
||||
'main_key > old_sub_key': 'new_sub_key'
|
||||
|
@ -0,0 +1,10 @@
|
||||
security:
|
||||
# ...
|
||||
firewalls:
|
||||
default:
|
||||
# ...
|
||||
anonymous: { secret: "%secret%" }
|
||||
remember_me:
|
||||
secret: "%secret%"
|
||||
http_digest:
|
||||
secret: "%secret%"
|
@ -0,0 +1,6 @@
|
||||
main_key:
|
||||
old_sub_key: value
|
||||
|
||||
sub:
|
||||
main_key:
|
||||
old_sub_key: value
|
Loading…
x
Reference in New Issue
Block a user