From 10bdf0a08d38b529addcefde3a1d615c0b5d5ef0 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Fri, 29 Jun 2018 17:05:02 +0200 Subject: [PATCH 1/6] fix yaml replacer --- .../src/Rector/RenameSubKeyYamlRector.php | 78 +++++++++---------- .../RenameSubKeyYamlRectorTest.php | 1 + .../correct/correct3.yml | 9 +++ .../RenameSubKeyYamlRector/wrong/wrong3.yml | 9 +++ 4 files changed, 56 insertions(+), 41 deletions(-) create mode 100644 packages/YamlRector/tests/Rector/RenameSubKeyYamlRector/correct/correct3.yml create mode 100644 packages/YamlRector/tests/Rector/RenameSubKeyYamlRector/wrong/wrong3.yml diff --git a/packages/YamlRector/src/Rector/RenameSubKeyYamlRector.php b/packages/YamlRector/src/Rector/RenameSubKeyYamlRector.php index 9a521eb2dd1..e99a7006ead 100644 --- a/packages/YamlRector/src/Rector/RenameSubKeyYamlRector.php +++ b/packages/YamlRector/src/Rector/RenameSubKeyYamlRector.php @@ -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; } } @@ -58,34 +35,53 @@ final class RenameSubKeyYamlRector implements YamlRectorInterface public function refactor(string $content): string { - $replacement = ''; + foreach ($this->pathsToNewKeys as $path => $newKey) { + $pathPattern = $this->createPatternFromPath($path); - $final = 2 * count($this->activePathSteps); - for ($i = 1; $i < $final - 1; ++$i) { - $replacement .= '$' . $i; + while (Strings::match($content, $pathPattern)) { + $replacement = $this->createReplacementFromPathAndNewKey($path, $newKey); + $content = Strings::replace($content, $pathPattern, $replacement, 1); + } } - $replacement .= preg_quote($this->activeNewKey); - $replacement .= '$' . ($i + 3); + return $content; + } - return Strings::replace($content, $this->activePathPattern, $replacement); + private function createPatternFromPath(string $path): string + { + $pathParts = Strings::split($path, '#[\s+]?>[\s+]?#'); + + $pattern = ''; + foreach ($pathParts as $key => $pathPart) { + if ($key === (count($pathParts) - 1)) { + // last only up-to the key name + $pattern .= sprintf('(%s)(.*?)', preg_quote($pathPart)); + } else { + // see https://regex101.com/r/n8XPbV/3/ for ([^:]*?) + $pattern .= sprintf('(%s:\s+)([^:]*?)', preg_quote($pathPart)); + } + } + + return '#^' . $pattern . '#ms'; } /** * @param string[] $pathSteps */ - private function createPattern(array $pathSteps): string + private function createReplacementFromPathAndNewKey(string $path, string $newKey): string { - $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)); - } + $replacement = ''; + + $pathParts = Strings::split($path, '#[\s+]?>[\s+]?#'); + + $final = 2 * count($pathParts); + for ($i = 1; $i < $final - 1; ++$i) { + $replacement .= '$' . $i; } - return '#^' . $pattern . '#s'; + $replacement .= preg_quote($newKey); + $replacement .= '$' . ($i + 3); + + return $replacement; } } diff --git a/packages/YamlRector/tests/Rector/RenameSubKeyYamlRector/RenameSubKeyYamlRectorTest.php b/packages/YamlRector/tests/Rector/RenameSubKeyYamlRector/RenameSubKeyYamlRectorTest.php index 0c744890aff..1b70782a823 100644 --- a/packages/YamlRector/tests/Rector/RenameSubKeyYamlRector/RenameSubKeyYamlRectorTest.php +++ b/packages/YamlRector/tests/Rector/RenameSubKeyYamlRector/RenameSubKeyYamlRectorTest.php @@ -22,6 +22,7 @@ final class RenameSubKeyYamlRectorTest extends AbstractYamlRectorTest { yield [__DIR__ . '/wrong/wrong.yml', __DIR__ . '/correct/correct.yml']; yield [__DIR__ . '/wrong/wrong2.yml', __DIR__ . '/correct/correct2.yml']; +// yield [__DIR__ . '/wrong/wrong3.yml', __DIR__ . '/correct/correct3.yml']; } protected function provideConfig(): string diff --git a/packages/YamlRector/tests/Rector/RenameSubKeyYamlRector/correct/correct3.yml b/packages/YamlRector/tests/Rector/RenameSubKeyYamlRector/correct/correct3.yml new file mode 100644 index 00000000000..8618924bdc3 --- /dev/null +++ b/packages/YamlRector/tests/Rector/RenameSubKeyYamlRector/correct/correct3.yml @@ -0,0 +1,9 @@ +security: + # ... + firewalls: + default: + # ... + anonymous: + new_key: "%secret%" + remember_me: + new_key: "%secret%" diff --git a/packages/YamlRector/tests/Rector/RenameSubKeyYamlRector/wrong/wrong3.yml b/packages/YamlRector/tests/Rector/RenameSubKeyYamlRector/wrong/wrong3.yml new file mode 100644 index 00000000000..08b75a3d0e6 --- /dev/null +++ b/packages/YamlRector/tests/Rector/RenameSubKeyYamlRector/wrong/wrong3.yml @@ -0,0 +1,9 @@ +security: + # ... + firewalls: + default: + # ... + anonymous: + old_key: "%secret%" + remember_me: + old_key: "%secret%" From c271a8969c334dc8bff47365ae04d77d15724787 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Fri, 29 Jun 2018 18:51:36 +0200 Subject: [PATCH 2/6] decouple splitPathToParts() method --- .../src/Rector/RenameSubKeyYamlRector.php | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/packages/YamlRector/src/Rector/RenameSubKeyYamlRector.php b/packages/YamlRector/src/Rector/RenameSubKeyYamlRector.php index e99a7006ead..be0fb4df0fe 100644 --- a/packages/YamlRector/src/Rector/RenameSubKeyYamlRector.php +++ b/packages/YamlRector/src/Rector/RenameSubKeyYamlRector.php @@ -49,7 +49,7 @@ final class RenameSubKeyYamlRector implements YamlRectorInterface private function createPatternFromPath(string $path): string { - $pathParts = Strings::split($path, '#[\s+]?>[\s+]?#'); + $pathParts = $this->splitPathToParts($path); $pattern = ''; foreach ($pathParts as $key => $pathPart) { @@ -65,14 +65,11 @@ final class RenameSubKeyYamlRector implements YamlRectorInterface return '#^' . $pattern . '#ms'; } - /** - * @param string[] $pathSteps - */ private function createReplacementFromPathAndNewKey(string $path, string $newKey): string { $replacement = ''; - $pathParts = Strings::split($path, '#[\s+]?>[\s+]?#'); + $pathParts = $this->splitPathToParts($path); $final = 2 * count($pathParts); for ($i = 1; $i < $final - 1; ++$i) { @@ -84,4 +81,12 @@ final class RenameSubKeyYamlRector implements YamlRectorInterface return $replacement; } + + /** + * @return string[] + */ + private function splitPathToParts(string $path): array + { + return Strings::split($path, '#[\s+]?>[\s+]?#'); + } } From 3860464c40a3d18025b4eec7e87625d262194800 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Fri, 29 Jun 2018 19:39:02 +0200 Subject: [PATCH 3/6] run all tests --- .../src/Rector/RenameSubKeyYamlRector.php | 12 ++++++------ .../RenameSubKeyYamlRectorTest.php | 1 - .../Rector/RenameSubKeyYamlRector/config.yml | 1 - .../RenameSubKeyYamlRector/correct/correct.yml | 15 +++++++++------ .../RenameSubKeyYamlRector/correct/correct3.yml | 9 --------- .../Rector/RenameSubKeyYamlRector/wrong/wrong.yml | 15 +++++++++------ .../RenameSubKeyYamlRector/wrong/wrong3.yml | 9 --------- 7 files changed, 24 insertions(+), 38 deletions(-) delete mode 100644 packages/YamlRector/tests/Rector/RenameSubKeyYamlRector/correct/correct3.yml delete mode 100644 packages/YamlRector/tests/Rector/RenameSubKeyYamlRector/wrong/wrong3.yml diff --git a/packages/YamlRector/src/Rector/RenameSubKeyYamlRector.php b/packages/YamlRector/src/Rector/RenameSubKeyYamlRector.php index be0fb4df0fe..fad5e9a672e 100644 --- a/packages/YamlRector/src/Rector/RenameSubKeyYamlRector.php +++ b/packages/YamlRector/src/Rector/RenameSubKeyYamlRector.php @@ -52,17 +52,16 @@ final class RenameSubKeyYamlRector implements YamlRectorInterface $pathParts = $this->splitPathToParts($path); $pattern = ''; - foreach ($pathParts as $key => $pathPart) { - if ($key === (count($pathParts) - 1)) { - // last only up-to the key name + 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 { - // see https://regex101.com/r/n8XPbV/3/ for ([^:]*?) - $pattern .= sprintf('(%s:\s+)([^:]*?)', preg_quote($pathPart)); + $pattern .= sprintf('(%s:)([\n \S+]+)', preg_quote($pathPart)); } } - return '#^' . $pattern . '#ms'; + return '#^' . $pattern . '#m'; } private function createReplacementFromPathAndNewKey(string $path, string $newKey): string @@ -87,6 +86,7 @@ final class RenameSubKeyYamlRector implements YamlRectorInterface */ private function splitPathToParts(string $path): array { + // split by " > " or ">" return Strings::split($path, '#[\s+]?>[\s+]?#'); } } diff --git a/packages/YamlRector/tests/Rector/RenameSubKeyYamlRector/RenameSubKeyYamlRectorTest.php b/packages/YamlRector/tests/Rector/RenameSubKeyYamlRector/RenameSubKeyYamlRectorTest.php index 1b70782a823..0c744890aff 100644 --- a/packages/YamlRector/tests/Rector/RenameSubKeyYamlRector/RenameSubKeyYamlRectorTest.php +++ b/packages/YamlRector/tests/Rector/RenameSubKeyYamlRector/RenameSubKeyYamlRectorTest.php @@ -22,7 +22,6 @@ final class RenameSubKeyYamlRectorTest extends AbstractYamlRectorTest { yield [__DIR__ . '/wrong/wrong.yml', __DIR__ . '/correct/correct.yml']; yield [__DIR__ . '/wrong/wrong2.yml', __DIR__ . '/correct/correct2.yml']; -// yield [__DIR__ . '/wrong/wrong3.yml', __DIR__ . '/correct/correct3.yml']; } protected function provideConfig(): string diff --git a/packages/YamlRector/tests/Rector/RenameSubKeyYamlRector/config.yml b/packages/YamlRector/tests/Rector/RenameSubKeyYamlRector/config.yml index ae6e0629ae6..87f92644248 100644 --- a/packages/YamlRector/tests/Rector/RenameSubKeyYamlRector/config.yml +++ b/packages/YamlRector/tests/Rector/RenameSubKeyYamlRector/config.yml @@ -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' diff --git a/packages/YamlRector/tests/Rector/RenameSubKeyYamlRector/correct/correct.yml b/packages/YamlRector/tests/Rector/RenameSubKeyYamlRector/correct/correct.yml index 369c25c194b..8618924bdc3 100644 --- a/packages/YamlRector/tests/Rector/RenameSubKeyYamlRector/correct/correct.yml +++ b/packages/YamlRector/tests/Rector/RenameSubKeyYamlRector/correct/correct.yml @@ -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%" diff --git a/packages/YamlRector/tests/Rector/RenameSubKeyYamlRector/correct/correct3.yml b/packages/YamlRector/tests/Rector/RenameSubKeyYamlRector/correct/correct3.yml deleted file mode 100644 index 8618924bdc3..00000000000 --- a/packages/YamlRector/tests/Rector/RenameSubKeyYamlRector/correct/correct3.yml +++ /dev/null @@ -1,9 +0,0 @@ -security: - # ... - firewalls: - default: - # ... - anonymous: - new_key: "%secret%" - remember_me: - new_key: "%secret%" diff --git a/packages/YamlRector/tests/Rector/RenameSubKeyYamlRector/wrong/wrong.yml b/packages/YamlRector/tests/Rector/RenameSubKeyYamlRector/wrong/wrong.yml index 484d79230ba..08b75a3d0e6 100644 --- a/packages/YamlRector/tests/Rector/RenameSubKeyYamlRector/wrong/wrong.yml +++ b/packages/YamlRector/tests/Rector/RenameSubKeyYamlRector/wrong/wrong.yml @@ -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%" diff --git a/packages/YamlRector/tests/Rector/RenameSubKeyYamlRector/wrong/wrong3.yml b/packages/YamlRector/tests/Rector/RenameSubKeyYamlRector/wrong/wrong3.yml deleted file mode 100644 index 08b75a3d0e6..00000000000 --- a/packages/YamlRector/tests/Rector/RenameSubKeyYamlRector/wrong/wrong3.yml +++ /dev/null @@ -1,9 +0,0 @@ -security: - # ... - firewalls: - default: - # ... - anonymous: - old_key: "%secret%" - remember_me: - old_key: "%secret%" From c6193e5911e3aaedf85183a472c8553fe45be685 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Tue, 17 Jul 2018 19:36:48 +0200 Subject: [PATCH 4/6] normalize bin files --- bin/{rector_bootstrap.php => bootstrap.php} | 0 bin/{rector_container.php => container.php} | 0 bin/rector | 4 ++-- composer.json | 17 ++++++++--------- 4 files changed, 10 insertions(+), 11 deletions(-) rename bin/{rector_bootstrap.php => bootstrap.php} (100%) rename bin/{rector_container.php => container.php} (100%) diff --git a/bin/rector_bootstrap.php b/bin/bootstrap.php similarity index 100% rename from bin/rector_bootstrap.php rename to bin/bootstrap.php diff --git a/bin/rector_container.php b/bin/container.php similarity index 100% rename from bin/rector_container.php rename to bin/container.php diff --git a/bin/rector b/bin/rector index 1aee5a6c53a..024336309a0 100755 --- a/bin/rector +++ b/bin/rector @@ -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()); diff --git a/composer.json b/composer.json index 77c61abf328..01f216440b1 100644 --- a/composer.json +++ b/composer.json @@ -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": { @@ -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", + "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 + } } From 6d400713044b6c7d538614912990a8e328a9a11d Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Tue, 17 Jul 2018 19:36:55 +0200 Subject: [PATCH 5/6] init CHANGELOG.md --- CHANGELOG.md | 83 ++++++++++++++++++++++++++++++++++++++++++++ changelog-linker.yml | 2 ++ composer.json | 2 +- 3 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 CHANGELOG.md create mode 100644 changelog-linker.yml diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 00000000000..9b4cfa335c4 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,83 @@ +# Changelog + + + +## 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 + + + +[#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 \ No newline at end of file diff --git a/changelog-linker.yml b/changelog-linker.yml new file mode 100644 index 00000000000..0296161e563 --- /dev/null +++ b/changelog-linker.yml @@ -0,0 +1,2 @@ +parameters: + authors_to_ignore: ['TomasVotruba'] \ No newline at end of file diff --git a/composer.json b/composer.json index 01f216440b1..e1cb7be5f15 100644 --- a/composer.json +++ b/composer.json @@ -83,7 +83,7 @@ "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", "changelog": [ - "vendor/bin/changelog-linker dump-merges", + "vendor/bin/changelog-linker dump-merges --in-categories", "vendor/bin/changelog-linker linkify" ] }, From f14a2c179faf6e8d9932599e0058a1bfdb0706e0 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Tue, 17 Jul 2018 19:50:01 +0200 Subject: [PATCH 6/6] fix dependencies conflict on --prefer-lowest --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index e1cb7be5f15..8c7a6ac50b6 100644 --- a/composer.json +++ b/composer.json @@ -27,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": {