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 56% rename from src/Rector/MethodCall/GetDoctrineControllerToManagerRegistryRector.php rename to src/Rector/MethodCall/GetHelperControllerToServiceRector.php index 1b5c6d0a..1f78bd48 100644 --- a/src/Rector/MethodCall/GetDoctrineControllerToManagerRegistryRector.php +++ b/src/Rector/MethodCall/GetHelperControllerToServiceRector.php @@ -8,9 +8,11 @@ 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; +use Rector\Naming\Naming\PropertyNaming; use Rector\PostRector\Collector\PropertyToAddCollector; use Rector\PostRector\ValueObject\PropertyMetadata; use Rector\Symfony\TypeAnalyzer\ControllerAnalyzer; @@ -21,20 +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 { public function __construct( private readonly ControllerAnalyzer $controllerAnalyzer, private readonly PropertyToAddCollector $propertyToAddCollector, + 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' @@ -88,22 +91,49 @@ public function refactor(Node $node): ?Node return null; } - if (! $this->isName($node->name, 'getDoctrine')) { - return null; - } - $class = $this->betterNodeFinder->findParentType($node, Class_::class); if (! $class instanceof Class_) { return null; } + if ($this->isName($node->name, 'getDoctrine')) { + return $this->refactorGetDoctrine($class); + } + + 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 - $propertyMetadata = new PropertyMetadata('managerRegistry', new ObjectType( - 'Doctrine\Persistence\ManagerRegistry' - ), Class_::MODIFIER_PRIVATE); + $propertyObjectType = new ObjectType('Symfony\Component\Messenger\MessageBusInterface'); + $propertyMetadata = new PropertyMetadata($propertyName, $propertyObjectType, Class_::MODIFIER_PRIVATE); $this->propertyToAddCollector->addPropertyToClass($class, $propertyMetadata); $thisVariable = new Variable('this'); - return new PropertyFetch($thisVariable, 'managerRegistry'); + $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); };