diff --git a/composer.json b/composer.json index a1535eac633a..b115a92a1d7b 100644 --- a/composer.json +++ b/composer.json @@ -32,7 +32,7 @@ "nette/utils": "^3.2", "nikic/php-parser": "^4.10.4", "phpstan/phpdoc-parser": "^0.4.9", - "phpstan/phpstan": "^0.12.64", + "phpstan/phpstan": "^0.12.64, <0.12.70", "phpstan/phpstan-phpunit": "^0.12.17", "psr/simple-cache": "^1.0", "sebastian/diff": "^4.0", diff --git a/config/set/symfony52.php b/config/set/symfony52.php index 1cd4ccc1daba..3253542eb92c 100644 --- a/config/set/symfony52.php +++ b/config/set/symfony52.php @@ -7,6 +7,7 @@ use Rector\Renaming\ValueObject\MethodCallRename; use Rector\Renaming\ValueObject\RenameClassAndConstFetch; use Rector\Symfony5\Rector\MethodCall\DefinitionAliasSetPrivateToSetPublicRector; +use Rector\Symfony5\Rector\MethodCall\FormBuilderSetDataMapperRector; use Rector\Symfony5\Rector\MethodCall\ReflectionExtractorEnableMagicCallExtractorRector; use Rector\Symfony5\Rector\New_\PropertyAccessorCreationBooleanToFlagsRector; use Rector\Symfony5\Rector\New_\PropertyPathMapperToDataMapperRector; @@ -112,4 +113,7 @@ # https://github.com/symfony/symfony/blob/5.x/UPGRADE-5.2.md#dependencyinjection $services->set(DefinitionAliasSetPrivateToSetPublicRector::class); + + # https://github.com/symfony/symfony/blob/5.x/UPGRADE-5.2.md#form + $services->set(FormBuilderSetDataMapperRector::class); }; diff --git a/rules/symfony5/src/Rector/MethodCall/FormBuilderSetDataMapperRector.php b/rules/symfony5/src/Rector/MethodCall/FormBuilderSetDataMapperRector.php new file mode 100644 index 000000000000..dc7f0dc996a8 --- /dev/null +++ b/rules/symfony5/src/Rector/MethodCall/FormBuilderSetDataMapperRector.php @@ -0,0 +1,104 @@ +setDataMapper(new PropertyPathMapper()) to Builder->setDataMapper(new DataMapper(new PropertyPathAccessor()))', + [ + new CodeSample( + <<<'CODE_SAMPLE' +use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper; +use Symfony\Component\Form\FormConfigBuilderInterface; + +class SomeClass +{ + public function run(FormConfigBuilderInterface $builder) + { + $builder->setDataMapper(new PropertyPathMapper()); + } +} +CODE_SAMPLE + , + <<<'CODE_SAMPLE' +use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper; +use Symfony\Component\Form\FormConfigBuilderInterface; + +class SomeClass +{ + public function run(FormConfigBuilderInterface $builder) + { + $builder->setDataMapper(new \Symfony\Component\Form\Extension\Core\DataMapper\DataMapper(new \Symfony\Component\Form\Extension\Core\DataAccessor\PropertyPathAccessor())); + } +} +CODE_SAMPLE + ), + ]); + } + + /** + * @return string[] + */ + public function getNodeTypes(): array + { + return [MethodCall::class]; + } + + /** + * @param MethodCall $node + */ + public function refactor(Node $node): ?Node + { + if (! $this->isObjectType($node->var, self::REQUIRED_TYPE)) { + return null; + } + + if (! $this->isName($node->name, 'setDataMapper')) { + return null; + } + + $argumentValue = $node->args[0]->value; + if ($this->isObjectType($argumentValue, self::ARG_CORRECT_TYPE)) { + return null; + } + + $propertyPathAccessor = new New_(new FullyQualified(self::ARG_MAPPER_TYPE)); + $newArgumentValue = new New_(new FullyQualified(self::ARG_CORRECT_TYPE), [new Arg($propertyPathAccessor)]); + $node->args[0]->value = $newArgumentValue; + + return $node; + } +} diff --git a/rules/symfony5/tests/Rector/MethodCall/FormBuilderSetDataMapperRector/Fixture/fixture.php.inc b/rules/symfony5/tests/Rector/MethodCall/FormBuilderSetDataMapperRector/Fixture/fixture.php.inc new file mode 100644 index 000000000000..c3b25b37704b --- /dev/null +++ b/rules/symfony5/tests/Rector/MethodCall/FormBuilderSetDataMapperRector/Fixture/fixture.php.inc @@ -0,0 +1,29 @@ +setDataMapper(new PropertyPathMapper()); + } +} +----- +setDataMapper(new \Symfony\Component\Form\Extension\Core\DataMapper\DataMapper(new \Symfony\Component\Form\Extension\Core\DataAccessor\PropertyPathAccessor())); + } +} \ No newline at end of file diff --git a/rules/symfony5/tests/Rector/MethodCall/FormBuilderSetDataMapperRector/Fixture/skip_correct_arg.php.inc b/rules/symfony5/tests/Rector/MethodCall/FormBuilderSetDataMapperRector/Fixture/skip_correct_arg.php.inc new file mode 100644 index 000000000000..55a8f673bfcb --- /dev/null +++ b/rules/symfony5/tests/Rector/MethodCall/FormBuilderSetDataMapperRector/Fixture/skip_correct_arg.php.inc @@ -0,0 +1,15 @@ +setDataMapper(new \Symfony\Component\Form\Extension\Core\DataMapper\DataMapper($propertyPathAccessor)); + } +} \ No newline at end of file diff --git a/rules/symfony5/tests/Rector/MethodCall/FormBuilderSetDataMapperRector/Fixture/skip_not_form_builder.php.inc b/rules/symfony5/tests/Rector/MethodCall/FormBuilderSetDataMapperRector/Fixture/skip_not_form_builder.php.inc new file mode 100644 index 000000000000..7bf425394f86 --- /dev/null +++ b/rules/symfony5/tests/Rector/MethodCall/FormBuilderSetDataMapperRector/Fixture/skip_not_form_builder.php.inc @@ -0,0 +1,12 @@ +format('Y-m-d'); + } +} diff --git a/rules/symfony5/tests/Rector/MethodCall/FormBuilderSetDataMapperRector/Fixture/skip_not_set_datamapper.php.inc b/rules/symfony5/tests/Rector/MethodCall/FormBuilderSetDataMapperRector/Fixture/skip_not_set_datamapper.php.inc new file mode 100644 index 000000000000..abc59059fa9e --- /dev/null +++ b/rules/symfony5/tests/Rector/MethodCall/FormBuilderSetDataMapperRector/Fixture/skip_not_set_datamapper.php.inc @@ -0,0 +1,13 @@ +foo(); + } +} diff --git a/rules/symfony5/tests/Rector/MethodCall/FormBuilderSetDataMapperRector/FormBuilderSetDataMapperRectorTest.php b/rules/symfony5/tests/Rector/MethodCall/FormBuilderSetDataMapperRector/FormBuilderSetDataMapperRectorTest.php new file mode 100644 index 000000000000..9264adf3b692 --- /dev/null +++ b/rules/symfony5/tests/Rector/MethodCall/FormBuilderSetDataMapperRector/FormBuilderSetDataMapperRectorTest.php @@ -0,0 +1,31 @@ +doTestFileInfo($fileInfo); + } + + public function provideData(): Iterator + { + return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + protected function getRectorClass(): string + { + return FormBuilderSetDataMapperRector::class; + } +}