Skip to content

Commit

Permalink
[Symfony 6.0] Add dispatchMessage() to service in controller (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jan 6, 2022
1 parent b58f5c9 commit 5c3c503
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 23 deletions.
4 changes: 2 additions & 2 deletions config/sets/symfony/symfony60.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -64,5 +64,5 @@
'loadUserByIdentifier'
),
]);
$services->set(GetDoctrineControllerToManagerRegistryRector::class);
$services->set(GetHelperControllerToServiceRector::class);
};
6 changes: 3 additions & 3 deletions docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -469,11 +469,11 @@ Changes createForm(new FormType), add(new FormType) to ones with "FormType::clas

<br>

## 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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'
Expand Down Expand Up @@ -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);
}
}
3 changes: 2 additions & 1 deletion src/TypeAnalyzer/ControllerAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Rector\Symfony\Tests\Rector\MethodCall\GetHelperControllerToServiceRector\Fixture;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

final class DispatchMessage extends AbstractController
{
public function run()
{
$productRepository = $this->dispatchMessage('hey');
}
}

?>
-----
<?php

namespace Rector\Symfony\Tests\Rector\MethodCall\GetHelperControllerToServiceRector\Fixture;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

final class DispatchMessage extends AbstractController
{
public function __construct(private \Symfony\Component\Messenger\MessageBusInterface $messageBus)
{
}
public function run()
{
$productRepository = $this->messageBus->dispatch('hey');
}
}

?>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rector\Symfony\Tests\Rector\MethodCall\GetDoctrineControllerToManagerRegistryRector\Fixture;
namespace Rector\Symfony\Tests\Rector\MethodCall\GetHelperControllerToServiceRector\Fixture;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

Expand All @@ -16,7 +16,7 @@ final class SomeController extends AbstractController
-----
<?php

namespace Rector\Symfony\Tests\Rector\MethodCall\GetDoctrineControllerToManagerRegistryRector\Fixture;
namespace Rector\Symfony\Tests\Rector\MethodCall\GetHelperControllerToServiceRector\Fixture;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

declare(strict_types=1);

namespace Rector\Symfony\Tests\Rector\MethodCall\GetDoctrineControllerToManagerRegistryRector;
namespace Rector\Symfony\Tests\Rector\MethodCall\GetHelperControllerToServiceRector;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

final class GetDoctrineControllerToManagerRegistryRectorTest extends AbstractRectorTestCase
final class GetHelperControllerToServiceRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

declare(strict_types=1);

use Rector\Symfony\Rector\MethodCall\GetDoctrineControllerToManagerRegistryRector;
use Rector\Symfony\Rector\MethodCall\GetHelperControllerToServiceRector;

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$containerConfigurator->import(__DIR__ . '/../../../../../config/config.php');

$services = $containerConfigurator->services();
$services->set(GetDoctrineControllerToManagerRegistryRector::class);
$services->set(GetHelperControllerToServiceRector::class);
};

0 comments on commit 5c3c503

Please sign in to comment.