Merge pull request #511 from rectorphp/regex

Improve RenameSubKeyYamlRector
This commit is contained in:
Tomáš Votruba 2018-07-17 20:03:40 +02:00 committed by GitHub
commit ae5ca3ac90
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 155 additions and 65 deletions

83
CHANGELOG.md Normal file
View File

@ -0,0 +1,83 @@
# Changelog
<!-- changelog-linker -->
## Unreleased
### Added
- [#508] [Rector] Add MergeInterfaceRector
- [#509] Add Yaml refactoring support
- [#512] Add --with-style option to apply basic style [closes [#506]]
### Changed
- [#493] Improve Test Performance
- [#500] Change RemoveConfiguratorConstantsRector to generic RenameClassConstantsUseToStringsRector
- [#515] Improvements, Thanks to [@carusogabriel]
### Fixed
- [#507] Fix chain method multi-rename
- [#513] Fixed README badge with packagist URL, Thanks to [@fulopattila122]
### Unknown Category
- [#492] Lower Cognitive Complexity to 6
- [#499] Change InjectPropertyRector to generic AnnotatedPropertyInjectToConstructorInjectionRector
- [#501] Change SetInjectToAddTagRector to generic MethodCallToAnotherMethodCallWithArgumentsRector
- [#502] Change more Nette specific Rectors to generic
- [#504] narrow focus to packages with 10 M+ downloads [closes [#495]]
- [#516] init monorepo split [closes [#425]]
## [v0.3.3] - 2018-06-02
### Changed
- [#479] Various NodeTypeResolver and tests improvements
- [#480] Decouple Rectors dependency on specific classes
- [#482] Decouple Symfony-related code to own package
- [#484] Decouple Sensio-related code to own package
- [#485] Decouple Nette-related code to own package
- [#486] Decouple Sylius-related code to own package
- [#487] Decouple Nette-related code to own package
- [#488] Decouple PHP-Parser-related upgrade code to own package
- [#489] Decouple Doctrine-related code to own package
### Unknown Category
- [#478] Lower deps to Symfony3.4
- [#490] Move CodeQuality from Contrib up
<!-- dumped content end -->
[#516]: https://github.com/rectorphp/rector/pull/516
[#515]: https://github.com/rectorphp/rector/pull/515
[#513]: https://github.com/rectorphp/rector/pull/513
[#512]: https://github.com/rectorphp/rector/pull/512
[#509]: https://github.com/rectorphp/rector/pull/509
[#508]: https://github.com/rectorphp/rector/pull/508
[#507]: https://github.com/rectorphp/rector/pull/507
[#506]: https://github.com/rectorphp/rector/pull/506
[#504]: https://github.com/rectorphp/rector/pull/504
[#502]: https://github.com/rectorphp/rector/pull/502
[#501]: https://github.com/rectorphp/rector/pull/501
[#500]: https://github.com/rectorphp/rector/pull/500
[#499]: https://github.com/rectorphp/rector/pull/499
[#495]: https://github.com/rectorphp/rector/pull/495
[#493]: https://github.com/rectorphp/rector/pull/493
[#492]: https://github.com/rectorphp/rector/pull/492
[#490]: https://github.com/rectorphp/rector/pull/490
[#489]: https://github.com/rectorphp/rector/pull/489
[#488]: https://github.com/rectorphp/rector/pull/488
[#487]: https://github.com/rectorphp/rector/pull/487
[#486]: https://github.com/rectorphp/rector/pull/486
[#485]: https://github.com/rectorphp/rector/pull/485
[#484]: https://github.com/rectorphp/rector/pull/484
[#482]: https://github.com/rectorphp/rector/pull/482
[#480]: https://github.com/rectorphp/rector/pull/480
[#479]: https://github.com/rectorphp/rector/pull/479
[#478]: https://github.com/rectorphp/rector/pull/478
[#425]: https://github.com/rectorphp/rector/pull/425
[@fulopattila122]: https://github.com/fulopattila122
[@carusogabriel]: https://github.com/carusogabriel

View File

@ -9,11 +9,11 @@ use Symplify\PackageBuilder\Console\ThrowableRenderer;
gc_disable();
// Require Composer autoload.php
require_once __DIR__ . '/rector_bootstrap.php';
require_once __DIR__ . '/bootstrap.php';
try {
/** @var ContainerInterface $container */
$container = require_once __DIR__ . '/rector_container.php';
$container = require_once __DIR__ . '/container.php';
$application = $container->get(Application::class);
exit($application->run());

2
changelog-linker.yml Normal file
View File

@ -0,0 +1,2 @@
parameters:
authors_to_ignore: ['TomasVotruba']

View File

@ -19,6 +19,7 @@
"symfony/dependency-injection": "^3.4|^4.0",
"symfony/finder": "^3.4|^4.0",
"symplify/better-phpdoc-parser": "^4.5",
"symplify/changelog-linker": "^4.5",
"symplify/monorepo-builder": "^4.5"
},
"require-dev": {
@ -26,7 +27,7 @@
"phpstan/phpstan-shim": "^0.9",
"phpunit/phpunit": "^7.1",
"slam/php-cs-fixer-extensions": "^1.15",
"symplify/easy-coding-standard": "^4.5",
"symplify/easy-coding-standard": "^4.5.1",
"tracy/tracy": "^2.4"
},
"autoload": {
@ -80,16 +81,14 @@
"bin/clean_levels.sh"
],
"phpstan": "vendor/bin/phpstan.phar analyse packages src tests --level max --configuration phpstan.neon",
"update-docs": "bin/rector describe --level all --format md > docs/AllRectorsOverview.md"
"update-docs": "bin/rector describe --level all --format md > docs/AllRectorsOverview.md",
"changelog": [
"vendor/bin/changelog-linker dump-merges --in-categories",
"vendor/bin/changelog-linker linkify"
]
},
"bin": [
"bin/rector",
"bin/rector_bootstrap.php",
"bin/rector_container.php"
],
"bin": ["bin/rector"],
"config": {
"sort-packages": true
},
"minimum-stability": "dev",
"prefer-stable": true
}
}

View File

@ -12,21 +12,6 @@ final class RenameSubKeyYamlRector implements YamlRectorInterface
*/
private $pathsToNewKeys = [];
/**
* @var string|null
*/
private $activePathPattern;
/**
* @var string[]
*/
private $activePathSteps = [];
/**
* @var string|null
*/
private $activeNewKey;
/**
* @param string[] $pathsToNewKeys
*/
@ -37,18 +22,10 @@ final class RenameSubKeyYamlRector implements YamlRectorInterface
public function isCandidate(string $content): bool
{
$this->activePathPattern = null;
$this->activePathSteps = [];
$this->activeNewKey = null;
foreach ($this->pathsToNewKeys as $path => $newKey) {
$pathSteps = Strings::split($path, '#[\s+]?>[\s+]?#');
$pathPattern = $this->createPattern($pathSteps);
$pathPattern = $this->createPatternFromPath($path);
if ((bool) Strings::match($content, $pathPattern)) {
$this->activePathSteps = $pathSteps;
$this->activePathPattern = $pathPattern;
$this->activeNewKey = $newKey;
return true;
}
}
@ -57,35 +34,59 @@ final class RenameSubKeyYamlRector implements YamlRectorInterface
}
public function refactor(string $content): string
{
foreach ($this->pathsToNewKeys as $path => $newKey) {
$pathPattern = $this->createPatternFromPath($path);
while (Strings::match($content, $pathPattern)) {
$replacement = $this->createReplacementFromPathAndNewKey($path, $newKey);
$content = Strings::replace($content, $pathPattern, $replacement, 1);
}
}
return $content;
}
private function createPatternFromPath(string $path): string
{
$pathParts = $this->splitPathToParts($path);
$pattern = '';
foreach ($pathParts as $nesting => $pathPart) {
if ($nesting === (count($pathParts) - 1)) {
// last only up-to the key name + the rest
$pattern .= sprintf('(%s)(.*?)', preg_quote($pathPart));
} else {
$pattern .= sprintf('(%s:)([\n \S+]+)', preg_quote($pathPart));
}
}
return '#^' . $pattern . '#m';
}
private function createReplacementFromPathAndNewKey(string $path, string $newKey): string
{
$replacement = '';
$final = 2 * count($this->activePathSteps);
$pathParts = $this->splitPathToParts($path);
$final = 2 * count($pathParts);
for ($i = 1; $i < $final - 1; ++$i) {
$replacement .= '$' . $i;
}
$replacement .= preg_quote($this->activeNewKey);
$replacement .= preg_quote($newKey);
$replacement .= '$' . ($i + 3);
return Strings::replace($content, $this->activePathPattern, $replacement);
return $replacement;
}
/**
* @param string[] $pathSteps
* @return string[]
*/
private function createPattern(array $pathSteps): string
private function splitPathToParts(string $path): array
{
$pattern = '';
foreach ($pathSteps as $key => $pathStep) {
if ($key === (count($pathSteps) - 1)) {
// last only up-to the key name
$pattern .= sprintf('(%s)(.*?)', preg_quote($pathStep));
} else {
$pattern .= sprintf('(%s:\s+)(.*?)', preg_quote($pathStep));
}
}
return '#^' . $pattern . '#s';
// split by " > " or ">"
return Strings::split($path, '#[\s+]?>[\s+]?#');
}
}

View File

@ -1,7 +1,6 @@
services:
Rector\YamlRector\Rector\RenameSubKeyYamlRector:
$pathsToNewKeys:
'main_key > old_sub_key': 'new_sub_key'
'security > firewalls > default > anonymous > old_key': 'new_key'
'security > firewalls > default > http_digest > old_key': 'new_key'
'security > firewalls > default > remember_me > old_key': 'new_key'

View File

@ -1,6 +1,9 @@
main_key:
new_sub_key: value
sub:
main_key:
old_sub_key: value
security:
# ...
firewalls:
default:
# ...
anonymous:
new_key: "%secret%"
remember_me:
new_key: "%secret%"

View File

@ -1,6 +1,9 @@
main_key:
old_sub_key: value
sub:
main_key:
old_sub_key: value
security:
# ...
firewalls:
default:
# ...
anonymous:
old_key: "%secret%"
remember_me:
old_key: "%secret%"