From 23179d00859ca75e0b2e1ceff61bba93a9558806 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 20 Feb 2024 22:48:32 +0700 Subject: [PATCH] [Transform] Allow transform fileExtensions() to withFileExtensions() (#5641) --- ...extensions_to_with_file_extensions.php.inc | 21 ++++++ .../RectorConfigBuilderRector.php | 67 ++++++++++++++++--- 2 files changed, 77 insertions(+), 11 deletions(-) create mode 100644 rules-tests/Transform/Rector/FileWithoutNamespace/RectorConfigBuilderRector/Fixture/file_extensions_to_with_file_extensions.php.inc diff --git a/rules-tests/Transform/Rector/FileWithoutNamespace/RectorConfigBuilderRector/Fixture/file_extensions_to_with_file_extensions.php.inc b/rules-tests/Transform/Rector/FileWithoutNamespace/RectorConfigBuilderRector/Fixture/file_extensions_to_with_file_extensions.php.inc new file mode 100644 index 00000000000..1fda906fa88 --- /dev/null +++ b/rules-tests/Transform/Rector/FileWithoutNamespace/RectorConfigBuilderRector/Fixture/file_extensions_to_with_file_extensions.php.inc @@ -0,0 +1,21 @@ +fileExtensions(['php', 'phtml']); +}; + +?> +----- +withFileExtensions(['php', 'phtml']); + +?> diff --git a/rules/Transform/Rector/FileWithoutNamespace/RectorConfigBuilderRector.php b/rules/Transform/Rector/FileWithoutNamespace/RectorConfigBuilderRector.php index 52021b979df..ae2f2ca2000 100644 --- a/rules/Transform/Rector/FileWithoutNamespace/RectorConfigBuilderRector.php +++ b/rules/Transform/Rector/FileWithoutNamespace/RectorConfigBuilderRector.php @@ -128,33 +128,78 @@ public function refactor(Node $node): ?Node if ($name === 'rule') { Assert::isAOf($rules, Array_::class); $rules->items[] = new ArrayItem($value); - } elseif (($name === 'rules')) { + + continue; + } + + if (($name === 'rules')) { if ($value instanceof Array_) { Assert::isAOf($rules, Array_::class); $rules->items = array_merge($rules->items, $value->items); } else { $rules = $value; } - } elseif ($name === 'paths') { + + continue; + } + + if ($name === 'paths') { $paths = $value; - } elseif ($name === 'skip') { + continue; + } + + if ($name === 'skip') { $skips = $value; - } elseif ($name === 'autoloadPaths') { + + continue; + } + + if ($name === 'autoloadPaths') { Assert::isAOf($value, Array_::class); $autoloadPaths = $value; - } elseif ($name === 'bootstrapFiles') { + + continue; + } + + if ($name === 'bootstrapFiles') { Assert::isAOf($value, Array_::class); $bootstrapFiles = $value; - } elseif ($name === 'ruleWithConfiguration') { - $newExpr = $this->nodeFactory->createMethodCall($newExpr, 'withConfiguredRule', [$value, $args[1]->value]); + + continue; + } + + if ($name === 'ruleWithConfiguration') { + $newExpr = $this->nodeFactory->createMethodCall( + $newExpr, + 'withConfiguredRule', + [$value, $args[1]->value] + ); $hasChanged = true; - } elseif ($name === 'sets') { + + continue; + } + + if ($name === 'sets') { Assert::isAOf($value, Array_::class); $sets->items = array_merge($sets->items, $value->items); - } else { - // implementing method by method - return null; + + continue; } + + if ($name === 'fileExtensions') { + Assert::isAOf($value, Array_::class); + $newExpr = $this->nodeFactory->createMethodCall( + $newExpr, + 'withFileExtensions', + [$value] + ); + $hasChanged = true; + + continue; + } + + // implementing method by method + return null; } if (! $paths instanceof Array_ || $paths->items !== []) {