diff --git a/composer.json b/composer.json index 33d1a9032d5..75f2d833dd6 100644 --- a/composer.json +++ b/composer.json @@ -102,7 +102,8 @@ "Rector\\Utils\\RectorGenerator\\": "utils/RectorGenerator/src", "Rector\\StrictCodeQuality\\": "packages/StrictCodeQuality/src", "Rector\\DynamicTypeAnalysis\\": "packages/DynamicTypeAnalysis/src", - "Rector\\PhpDeglobalize\\": "packages/PhpDeglobalize/src" + "Rector\\PhpDeglobalize\\": "packages/PhpDeglobalize/src", + "Rector\\Phalcon\\": "packages/Phalcon/src" } }, "autoload-dev": { @@ -160,7 +161,8 @@ "Rector\\ZendToSymfony\\Tests\\": "packages/ZendToSymfony/tests", "Rector\\StrictCodeQuality\\Tests\\": "packages/StrictCodeQuality/tests", "Rector\\DynamicTypeAnalysis\\Tests\\": "packages/DynamicTypeAnalysis/tests", - "Rector\\PhpDeglobalize\\Tests\\": "packages/PhpDeglobalize/tests" + "Rector\\PhpDeglobalize\\Tests\\": "packages/PhpDeglobalize/tests", + "Rector\\Phalcon\\Tests\\": "packages/Phalcon/tests" }, "classmap": [ "packages/Symfony/tests/Rector/FrameworkBundle/AbstractToConstructorInjectionRectorSource", diff --git a/config/set/phalcon/phalcon40.yaml b/config/set/phalcon/phalcon40.yaml index 37418cdd2ec..849435d180c 100644 --- a/config/set/phalcon/phalcon40.yaml +++ b/config/set/phalcon/phalcon40.yaml @@ -114,3 +114,4 @@ services: Rector\Renaming\Rector\ConstFetch\RenameConstantRector: FILTER_SPECIAL_CHARS: 'FILTER_SPECIAL' FILTER_ALPHANUM: 'FILTER_ALNUM' + Rector\Phalcon\Rector\Assign\FlashWithCssClassesToExtraCallRector: ~ diff --git a/packages/Phalcon/src/Rector/Assign/FlashWithCssClassesToExtraCallRector.php b/packages/Phalcon/src/Rector/Assign/FlashWithCssClassesToExtraCallRector.php new file mode 100644 index 00000000000..c5a6fde98c1 --- /dev/null +++ b/packages/Phalcon/src/Rector/Assign/FlashWithCssClassesToExtraCallRector.php @@ -0,0 +1,88 @@ +setCssClasses($cssClasses); + } +} +PHP + + ), + ]); + } + + /** + * @return string[] + */ + public function getNodeTypes(): array + { + return [Assign::class]; + } + + /** + * @param Assign $node + */ + public function refactor(Node $node): ?Node + { + if (! $node->expr instanceof Node\Expr\New_) { + return null; + } + + if (! $this->isName($node->expr->class, 'Phalcon\Flash')) { + return null; + } + + if (! isset($node->expr->args[0])) { + return null; + } + + $argument = $node->expr->args[0]; + + // remove arg + unset($node->expr->args[0]); + + // change the node + + $variable = $node->var; + $setCssClassesMethodCall = new Node\Expr\MethodCall($variable, 'setCssClasses', [$argument]); + + $this->addNodeAfterNode($setCssClassesMethodCall, $node); + + return $node; + } +} diff --git a/packages/Phalcon/tests/Rector/Assign/FlashWithCssClassesToExtraCallRector/Fixture/fixture.php.inc b/packages/Phalcon/tests/Rector/Assign/FlashWithCssClassesToExtraCallRector/Fixture/fixture.php.inc new file mode 100644 index 00000000000..1832df4a19e --- /dev/null +++ b/packages/Phalcon/tests/Rector/Assign/FlashWithCssClassesToExtraCallRector/Fixture/fixture.php.inc @@ -0,0 +1,28 @@ + +----- +setCssClasses($cssClasses); + } +} + +?> diff --git a/packages/Phalcon/tests/Rector/Assign/FlashWithCssClassesToExtraCallRector/FlashWithCssClassesToExtraCallRectorTest.php b/packages/Phalcon/tests/Rector/Assign/FlashWithCssClassesToExtraCallRector/FlashWithCssClassesToExtraCallRectorTest.php new file mode 100644 index 00000000000..0704446ce83 --- /dev/null +++ b/packages/Phalcon/tests/Rector/Assign/FlashWithCssClassesToExtraCallRector/FlashWithCssClassesToExtraCallRectorTest.php @@ -0,0 +1,30 @@ +doTestFile($file); + } + + public function provideDataForTest(): Iterator + { + return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + protected function getRectorClass(): string + { + return FlashWithCssClassesToExtraCallRector::class; + } +} diff --git a/rector.yaml b/rector.yaml index 6bb9ab94135..0022e6f40f7 100644 --- a/rector.yaml +++ b/rector.yaml @@ -19,23 +19,22 @@ parameters: # @see utils/RectorGenerator/config/config.yaml rector_recipe: # run "bin/rector create" to create a new Rector + tests from this config - package: "Rector" - name: "SwapClassMethodArgumentsRector" + package: "Phalcon" + name: "FlashWithCssClassesToExtraCallRector" node_types: # put main node first, it is used to create namespace - - "StaticCall" - - "MethodCall" - - "ClassMethod" + - "Assign" - description: "Reorder class method arguments, including their calls" + description: "Add $cssClasses in Flash to separated method call" code_before: > setCssClasses($cssClasses); } } source: # e.g. link to RFC or headline in upgrade guide, 1 or more in the list - - "" - set: "" # e.g. symfony30, target config to append this rector to + - "https://github.com/rectorphp/rector/issues/2408#issue-534441142" + set: "phalcon40" # e.g. symfony30, target config to append this rector to