diff --git a/config/sets/symfony/configs.php b/config/sets/symfony/configs.php index 39398737..217e2ffe 100644 --- a/config/sets/symfony/configs.php +++ b/config/sets/symfony/configs.php @@ -10,6 +10,7 @@ use Rector\Symfony\Configs\Rector\Closure\ServiceSetStringNameToClassNameRector; use Rector\Symfony\Configs\Rector\Closure\ServiceSettersToSettersAutodiscoveryRector; use Rector\Symfony\Configs\Rector\Closure\ServiceTagsToDefaultsAutoconfigureRector; +use Rector\Symfony\Configs\Rector\MethodCall\EnableValidationAttributesRector; return static function (RectorConfig $rectorConfig): void { $rectorConfig->rules([ @@ -20,5 +21,6 @@ ServiceTagsToDefaultsAutoconfigureRector::class, RemoveConstructorAutowireServiceRector::class, FromServicePublicToDefaultsPublicRector::class, + EnableValidationAttributesRector::class, ]); }; diff --git a/rules-tests/Configs/Rector/MethodCall/EnableValidationAttributesRector/EnableValidationAttributesRectorTest.php b/rules-tests/Configs/Rector/MethodCall/EnableValidationAttributesRector/EnableValidationAttributesRectorTest.php new file mode 100644 index 00000000..7543e2f9 --- /dev/null +++ b/rules-tests/Configs/Rector/MethodCall/EnableValidationAttributesRector/EnableValidationAttributesRectorTest.php @@ -0,0 +1,28 @@ +doTestFile($filePath); + } + + public static function provideData(): Iterator + { + return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/rules-tests/Configs/Rector/MethodCall/EnableValidationAttributesRector/Fixture/enable_attributes.php.inc b/rules-tests/Configs/Rector/MethodCall/EnableValidationAttributesRector/Fixture/enable_attributes.php.inc new file mode 100644 index 00000000..5fccdd4e --- /dev/null +++ b/rules-tests/Configs/Rector/MethodCall/EnableValidationAttributesRector/Fixture/enable_attributes.php.inc @@ -0,0 +1,33 @@ +loadFromExtension('framework', [ + 'secret' => '%secret_key%', + 'form' => null, + 'validation' => [ + 'enable_attributes' => false, + ], +]); + +?> +----- +loadFromExtension('framework', [ + 'secret' => '%secret_key%', + 'form' => null, + 'validation' => [ + 'enable_attributes' => true, + ], +]); + +?> diff --git a/rules-tests/Configs/Rector/MethodCall/EnableValidationAttributesRector/Fixture/skip_already_enabled.php.inc b/rules-tests/Configs/Rector/MethodCall/EnableValidationAttributesRector/Fixture/skip_already_enabled.php.inc new file mode 100644 index 00000000..f232a455 --- /dev/null +++ b/rules-tests/Configs/Rector/MethodCall/EnableValidationAttributesRector/Fixture/skip_already_enabled.php.inc @@ -0,0 +1,12 @@ +loadFromExtension('framework', [ + 'validation' => [ + 'enable_attributes' => true, + ], +]); diff --git a/rules-tests/Configs/Rector/MethodCall/EnableValidationAttributesRector/Fixture/skip_another_extension.php.inc b/rules-tests/Configs/Rector/MethodCall/EnableValidationAttributesRector/Fixture/skip_another_extension.php.inc new file mode 100644 index 00000000..3acd0ee9 --- /dev/null +++ b/rules-tests/Configs/Rector/MethodCall/EnableValidationAttributesRector/Fixture/skip_another_extension.php.inc @@ -0,0 +1,12 @@ +loadFromExtension('twig', [ + 'validation' => [ + 'enable_attributes' => false, + ], +]); diff --git a/rules-tests/Configs/Rector/MethodCall/EnableValidationAttributesRector/Fixture/skip_missing_validation.php.inc b/rules-tests/Configs/Rector/MethodCall/EnableValidationAttributesRector/Fixture/skip_missing_validation.php.inc new file mode 100644 index 00000000..55fb409c --- /dev/null +++ b/rules-tests/Configs/Rector/MethodCall/EnableValidationAttributesRector/Fixture/skip_missing_validation.php.inc @@ -0,0 +1,10 @@ +loadFromExtension('framework', [ + 'secret' => '%secret_key%', +]); diff --git a/rules-tests/Configs/Rector/MethodCall/EnableValidationAttributesRector/config/configured_rule.php b/rules-tests/Configs/Rector/MethodCall/EnableValidationAttributesRector/config/configured_rule.php new file mode 100644 index 00000000..9a1e7ab5 --- /dev/null +++ b/rules-tests/Configs/Rector/MethodCall/EnableValidationAttributesRector/config/configured_rule.php @@ -0,0 +1,10 @@ +rule(EnableValidationAttributesRector::class); +}; diff --git a/rules/Configs/Rector/MethodCall/EnableValidationAttributesRector.php b/rules/Configs/Rector/MethodCall/EnableValidationAttributesRector.php new file mode 100644 index 00000000..454b2aa3 --- /dev/null +++ b/rules/Configs/Rector/MethodCall/EnableValidationAttributesRector.php @@ -0,0 +1,129 @@ +loadFromExtension('framework', [ + 'validation' => [ + 'enable_attributes' => false, + ], +]); +CODE_SAMPLE + , + <<<'CODE_SAMPLE' +$container->loadFromExtension('framework', [ + 'validation' => [ + 'enable_attributes' => true, + ], +]); +CODE_SAMPLE + ), + ] + ); + } + + /** + * @return array> + */ + public function getNodeTypes(): array + { + return [MethodCall::class]; + } + + /** + * @param MethodCall $node + */ + public function refactor(Node $node): ?Node + { + if (! $this->isName($node->name, 'loadFromExtension')) { + return null; + } + + $args = $node->getArgs(); + if (count($args) < 2) { + return null; + } + + if (! $this->valueResolver->isValue($args[0]->value, 'framework')) { + return null; + } + + $configArray = $args[1]->value; + if (! $configArray instanceof Array_) { + return null; + } + + $validationArrayItem = $this->matchArrayItemByKey($configArray, 'validation'); + if (! $validationArrayItem instanceof ArrayItem) { + return null; + } + + if (! $validationArrayItem->value instanceof Array_) { + return null; + } + + $enableAttributesArrayItem = $this->matchArrayItemByKey($validationArrayItem->value, 'enable_attributes'); + if (! $enableAttributesArrayItem instanceof ArrayItem) { + return null; + } + + if (! $this->valueResolver->isFalse($enableAttributesArrayItem->value)) { + return null; + } + + $enableAttributesArrayItem->value = new ConstFetch(new Name('true')); + + return $node; + } + + private function matchArrayItemByKey(Array_ $array, string $keyName): ?ArrayItem + { + foreach ($array->items as $arrayItem) { + if (! $arrayItem instanceof ArrayItem) { + continue; + } + + if (! $arrayItem->key instanceof Expr) { + continue; + } + + if (! $this->valueResolver->isValue($arrayItem->key, $keyName)) { + continue; + } + + return $arrayItem; + } + + return null; + } +}