From 82ed160c68deab489d27b7f8f2792456cb2cc079 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Thu, 6 Jan 2022 14:56:31 +0100 Subject: [PATCH 1/2] make GetDoctrineControllerToManagerRegistryRector generic --- ...trineControllerToManagerRegistryRector.php | 46 +++++++++++++------ 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/src/Rector/MethodCall/GetDoctrineControllerToManagerRegistryRector.php b/src/Rector/MethodCall/GetDoctrineControllerToManagerRegistryRector.php index 1b5c6d0a..6c586c74 100644 --- a/src/Rector/MethodCall/GetDoctrineControllerToManagerRegistryRector.php +++ b/src/Rector/MethodCall/GetDoctrineControllerToManagerRegistryRector.php @@ -11,6 +11,7 @@ use PhpParser\Node\Stmt\Class_; use PHPStan\Type\ObjectType; use Rector\Core\Rector\AbstractRector; +use Rector\Naming\Naming\PropertyNaming; use Rector\PostRector\Collector\PropertyToAddCollector; use Rector\PostRector\ValueObject\PropertyMetadata; use Rector\Symfony\TypeAnalyzer\ControllerAnalyzer; @@ -25,9 +26,21 @@ */ final class GetDoctrineControllerToManagerRegistryRector extends AbstractRector { + /** + * @var array + */ + private const METHOD_NAME_TO_PROPERTY_TYPE = [ + 'getDoctrine' => 'Doctrine\Persistence\ManagerRegistry', + ]; + public function __construct( private readonly ControllerAnalyzer $controllerAnalyzer, +<<<<<<< HEAD private readonly PropertyToAddCollector $propertyToAddCollector, +======= + private PropertyToAddCollector $propertyToAddCollector, + private PropertyNaming $propertyNaming, +>>>>>>> make GetDoctrineControllerToManagerRegistryRector generic ) { } @@ -88,22 +101,27 @@ public function refactor(Node $node): ?Node return null; } - if (! $this->isName($node->name, 'getDoctrine')) { - return null; - } + foreach (self::METHOD_NAME_TO_PROPERTY_TYPE as $methodName => $propertyType) { + if (! $this->isName($node->name, $methodName)) { + continue; + } - $class = $this->betterNodeFinder->findParentType($node, Class_::class); - if (! $class instanceof Class_) { - return null; - } + $propertyName = $this->propertyNaming->fqnToVariableName($propertyType); + + $class = $this->betterNodeFinder->findParentType($node, Class_::class); + if (! $class instanceof Class_) { + return null; + } - // add dependency - $propertyMetadata = new PropertyMetadata('managerRegistry', new ObjectType( - 'Doctrine\Persistence\ManagerRegistry' - ), Class_::MODIFIER_PRIVATE); - $this->propertyToAddCollector->addPropertyToClass($class, $propertyMetadata); + // add dependency + $propertyObjectType = new ObjectType($propertyType); + $propertyMetadata = new PropertyMetadata($propertyName, $propertyObjectType, Class_::MODIFIER_PRIVATE); + $this->propertyToAddCollector->addPropertyToClass($class, $propertyMetadata); + + $thisVariable = new Variable('this'); + return new PropertyFetch($thisVariable, $propertyName); + } - $thisVariable = new Variable('this'); - return new PropertyFetch($thisVariable, 'managerRegistry'); + return null; } } From b8cac60e6a6ecad1718e2fe4c06d7b16de333ded Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Thu, 6 Jan 2022 14:58:19 +0100 Subject: [PATCH 2/2] rename to GetHelperControllerToServiceRector --- config/sets/symfony/symfony60.php | 4 +- docs/rector_rules_overview.md | 6 +- ...=> GetHelperControllerToServiceRector.php} | 76 +++++++++++-------- src/TypeAnalyzer/ControllerAnalyzer.php | 3 +- .../Fixture/dispatch_message.php.inc | 34 +++++++++ .../Fixture/some_class.php.inc | 4 +- ...etHelperControllerToServiceRectorTest.php} | 4 +- .../config/configured_rule.php | 4 +- 8 files changed, 91 insertions(+), 44 deletions(-) rename src/Rector/MethodCall/{GetDoctrineControllerToManagerRegistryRector.php => GetHelperControllerToServiceRector.php} (51%) create mode 100644 tests/Rector/MethodCall/GetHelperControllerToServiceRector/Fixture/dispatch_message.php.inc rename tests/Rector/MethodCall/{GetDoctrineControllerToManagerRegistryRector => GetHelperControllerToServiceRector}/Fixture/some_class.php.inc (75%) rename tests/Rector/MethodCall/{GetDoctrineControllerToManagerRegistryRector/GetDoctrineControllerToManagerRegistryRectorTest.php => GetHelperControllerToServiceRector/GetHelperControllerToServiceRectorTest.php} (76%) rename tests/Rector/MethodCall/{GetDoctrineControllerToManagerRegistryRector => GetHelperControllerToServiceRector}/config/configured_rule.php (68%) diff --git a/config/sets/symfony/symfony60.php b/config/sets/symfony/symfony60.php index af3525f0..432f1520 100644 --- a/config/sets/symfony/symfony60.php +++ b/config/sets/symfony/symfony60.php @@ -9,7 +9,7 @@ use Rector\Renaming\Rector\Name\RenameClassRector; use Rector\Renaming\ValueObject\MethodCallRename; use Rector\Symfony\Rector\FuncCall\ReplaceServiceArgumentRector; -use Rector\Symfony\Rector\MethodCall\GetDoctrineControllerToManagerRegistryRector; +use Rector\Symfony\Rector\MethodCall\GetHelperControllerToServiceRector; use Rector\Symfony\Set\SymfonySetList; use Rector\Symfony\ValueObject\ReplaceServiceArgument; use Rector\TypeDeclaration\Rector\ClassMethod\AddParamTypeDeclarationRector; @@ -64,5 +64,5 @@ 'loadUserByIdentifier' ), ]); - $services->set(GetDoctrineControllerToManagerRegistryRector::class); + $services->set(GetHelperControllerToServiceRector::class); }; diff --git a/docs/rector_rules_overview.md b/docs/rector_rules_overview.md index dd0705c5..751a7b92 100644 --- a/docs/rector_rules_overview.md +++ b/docs/rector_rules_overview.md @@ -469,11 +469,11 @@ Changes createForm(new FormType), add(new FormType) to ones with "FormType::clas
-## GetDoctrineControllerToManagerRegistryRector +## GetHelperControllerToServiceRector -Replace `$this->getDoctrine()` calls in AbstractController with direct Doctrine\Persistence\ManagerRegistry service +Replace `$this->getDoctrine()` and `$this->dispatchMessage()` calls in AbstractController with direct service use -- class: [`Rector\Symfony\Rector\MethodCall\GetDoctrineControllerToManagerRegistryRector`](../src/Rector/MethodCall/GetDoctrineControllerToManagerRegistryRector.php) +- class: [`Rector\Symfony\Rector\MethodCall\GetHelperControllerToServiceRector`](../src/Rector/MethodCall/GetHelperControllerToServiceRector.php) ```diff use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; diff --git a/src/Rector/MethodCall/GetDoctrineControllerToManagerRegistryRector.php b/src/Rector/MethodCall/GetHelperControllerToServiceRector.php similarity index 51% rename from src/Rector/MethodCall/GetDoctrineControllerToManagerRegistryRector.php rename to src/Rector/MethodCall/GetHelperControllerToServiceRector.php index 6c586c74..1f78bd48 100644 --- a/src/Rector/MethodCall/GetDoctrineControllerToManagerRegistryRector.php +++ b/src/Rector/MethodCall/GetHelperControllerToServiceRector.php @@ -8,6 +8,7 @@ use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\PropertyFetch; use PhpParser\Node\Expr\Variable; +use PhpParser\Node\Identifier; use PhpParser\Node\Stmt\Class_; use PHPStan\Type\ObjectType; use Rector\Core\Rector\AbstractRector; @@ -22,32 +23,21 @@ * @changelog https://github.com/symfony/symfony/pull/42422 * @changelog https://github.com/symfony/symfony/pull/1195 * - * @see \Rector\Symfony\Tests\Rector\MethodCall\GetDoctrineControllerToManagerRegistryRector\GetDoctrineControllerToManagerRegistryRectorTest + * @see \Rector\Symfony\Tests\Rector\MethodCall\GetHelperControllerToServiceRector\GetHelperControllerToServiceRectorTest */ -final class GetDoctrineControllerToManagerRegistryRector extends AbstractRector +final class GetHelperControllerToServiceRector extends AbstractRector { - /** - * @var array - */ - private const METHOD_NAME_TO_PROPERTY_TYPE = [ - 'getDoctrine' => 'Doctrine\Persistence\ManagerRegistry', - ]; - public function __construct( private readonly ControllerAnalyzer $controllerAnalyzer, -<<<<<<< HEAD private readonly PropertyToAddCollector $propertyToAddCollector, -======= - private PropertyToAddCollector $propertyToAddCollector, - private PropertyNaming $propertyNaming, ->>>>>>> make GetDoctrineControllerToManagerRegistryRector generic + private readonly PropertyNaming $propertyNaming, ) { } public function getRuleDefinition(): RuleDefinition { return new RuleDefinition( - 'Replace $this->getDoctrine() calls in AbstractController with direct Doctrine\Persistence\ManagerRegistry service', + 'Replace $this->getDoctrine() and $this->dispatchMessage() calls in AbstractController with direct service use', [ new CodeSample( <<<'CODE_SAMPLE' @@ -101,27 +91,49 @@ public function refactor(Node $node): ?Node return null; } - foreach (self::METHOD_NAME_TO_PROPERTY_TYPE as $methodName => $propertyType) { - if (! $this->isName($node->name, $methodName)) { - continue; - } - - $propertyName = $this->propertyNaming->fqnToVariableName($propertyType); - - $class = $this->betterNodeFinder->findParentType($node, Class_::class); - if (! $class instanceof Class_) { - return null; - } + $class = $this->betterNodeFinder->findParentType($node, Class_::class); + if (! $class instanceof Class_) { + return null; + } - // add dependency - $propertyObjectType = new ObjectType($propertyType); - $propertyMetadata = new PropertyMetadata($propertyName, $propertyObjectType, Class_::MODIFIER_PRIVATE); - $this->propertyToAddCollector->addPropertyToClass($class, $propertyMetadata); + if ($this->isName($node->name, 'getDoctrine')) { + return $this->refactorGetDoctrine($class); + } - $thisVariable = new Variable('this'); - return new PropertyFetch($thisVariable, $propertyName); + if ($this->isName($node->name, 'dispatchMessage')) { + return $this->refactorDispatchMessage($class, $node); } return null; } + + private function refactorDispatchMessage(Class_ $class, MethodCall $methodCall): Node|MethodCall + { + $propertyName = $this->propertyNaming->fqnToVariableName('Symfony\Component\Messenger\MessageBusInterface'); + + // add dependency + $propertyObjectType = new ObjectType('Symfony\Component\Messenger\MessageBusInterface'); + $propertyMetadata = new PropertyMetadata($propertyName, $propertyObjectType, Class_::MODIFIER_PRIVATE); + $this->propertyToAddCollector->addPropertyToClass($class, $propertyMetadata); + + $thisVariable = new Variable('this'); + $methodCall->var = new PropertyFetch($thisVariable, $propertyName); + $methodCall->name = new Identifier('dispatch'); + + return $methodCall; + } + + private function refactorGetDoctrine(Class_ $class): PropertyFetch + { + $propertyName = $this->propertyNaming->fqnToVariableName('Doctrine\Persistence\ManagerRegistry'); + + // add dependency + $propertyObjectType = new ObjectType('Doctrine\Persistence\ManagerRegistry'); + $propertyMetadata = new PropertyMetadata($propertyName, $propertyObjectType, Class_::MODIFIER_PRIVATE); + $this->propertyToAddCollector->addPropertyToClass($class, $propertyMetadata); + + $thisVariable = new Variable('this'); + + return new PropertyFetch($thisVariable, $propertyName); + } } diff --git a/src/TypeAnalyzer/ControllerAnalyzer.php b/src/TypeAnalyzer/ControllerAnalyzer.php index 7be9775a..79981e52 100644 --- a/src/TypeAnalyzer/ControllerAnalyzer.php +++ b/src/TypeAnalyzer/ControllerAnalyzer.php @@ -5,6 +5,7 @@ namespace Rector\Symfony\TypeAnalyzer; use PhpParser\Node; +use PhpParser\Node\Expr; use PHPStan\Analyser\Scope; use PHPStan\Reflection\ClassReflection; use PHPStan\Type\ObjectType; @@ -14,7 +15,7 @@ final class ControllerAnalyzer { - public function isController(Node\Expr $expr): bool + public function isController(Expr $expr): bool { $scope = $expr->getAttribute(AttributeKey::SCOPE); diff --git a/tests/Rector/MethodCall/GetHelperControllerToServiceRector/Fixture/dispatch_message.php.inc b/tests/Rector/MethodCall/GetHelperControllerToServiceRector/Fixture/dispatch_message.php.inc new file mode 100644 index 00000000..410f9406 --- /dev/null +++ b/tests/Rector/MethodCall/GetHelperControllerToServiceRector/Fixture/dispatch_message.php.inc @@ -0,0 +1,34 @@ +dispatchMessage('hey'); + } +} + +?> +----- +messageBus->dispatch('hey'); + } +} + +?> diff --git a/tests/Rector/MethodCall/GetDoctrineControllerToManagerRegistryRector/Fixture/some_class.php.inc b/tests/Rector/MethodCall/GetHelperControllerToServiceRector/Fixture/some_class.php.inc similarity index 75% rename from tests/Rector/MethodCall/GetDoctrineControllerToManagerRegistryRector/Fixture/some_class.php.inc rename to tests/Rector/MethodCall/GetHelperControllerToServiceRector/Fixture/some_class.php.inc index 7e6c1521..2da629f0 100644 --- a/tests/Rector/MethodCall/GetDoctrineControllerToManagerRegistryRector/Fixture/some_class.php.inc +++ b/tests/Rector/MethodCall/GetHelperControllerToServiceRector/Fixture/some_class.php.inc @@ -1,6 +1,6 @@ import(__DIR__ . '/../../../../../config/config.php'); $services = $containerConfigurator->services(); - $services->set(GetDoctrineControllerToManagerRegistryRector::class); + $services->set(GetHelperControllerToServiceRector::class); };