Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b8cac60

Browse filesBrowse files
committed
rename to GetHelperControllerToServiceRector
1 parent 82ed160 commit b8cac60
Copy full SHA for b8cac60

File tree

Expand file treeCollapse file tree

8 files changed

+91
-44
lines changed
Filter options
Expand file treeCollapse file tree

8 files changed

+91
-44
lines changed

‎config/sets/symfony/symfony60.php

Copy file name to clipboardExpand all lines: config/sets/symfony/symfony60.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Rector\Renaming\Rector\Name\RenameClassRector;
1010
use Rector\Renaming\ValueObject\MethodCallRename;
1111
use Rector\Symfony\Rector\FuncCall\ReplaceServiceArgumentRector;
12-
use Rector\Symfony\Rector\MethodCall\GetDoctrineControllerToManagerRegistryRector;
12+
use Rector\Symfony\Rector\MethodCall\GetHelperControllerToServiceRector;
1313
use Rector\Symfony\Set\SymfonySetList;
1414
use Rector\Symfony\ValueObject\ReplaceServiceArgument;
1515
use Rector\TypeDeclaration\Rector\ClassMethod\AddParamTypeDeclarationRector;
@@ -64,5 +64,5 @@
6464
'loadUserByIdentifier'
6565
),
6666
]);
67-
$services->set(GetDoctrineControllerToManagerRegistryRector::class);
67+
$services->set(GetHelperControllerToServiceRector::class);
6868
};

‎docs/rector_rules_overview.md

Copy file name to clipboardExpand all lines: docs/rector_rules_overview.md
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -469,11 +469,11 @@ Changes createForm(new FormType), add(new FormType) to ones with "FormType::clas
469469

470470
<br>
471471

472-
## GetDoctrineControllerToManagerRegistryRector
472+
## GetHelperControllerToServiceRector
473473

474-
Replace `$this->getDoctrine()` calls in AbstractController with direct Doctrine\Persistence\ManagerRegistry service
474+
Replace `$this->getDoctrine()` and `$this->dispatchMessage()` calls in AbstractController with direct service use
475475

476-
- class: [`Rector\Symfony\Rector\MethodCall\GetDoctrineControllerToManagerRegistryRector`](../src/Rector/MethodCall/GetDoctrineControllerToManagerRegistryRector.php)
476+
- class: [`Rector\Symfony\Rector\MethodCall\GetHelperControllerToServiceRector`](../src/Rector/MethodCall/GetHelperControllerToServiceRector.php)
477477

478478
```diff
479479
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

‎src/Rector/MethodCall/GetDoctrineControllerToManagerRegistryRector.php renamed to ‎src/Rector/MethodCall/GetHelperControllerToServiceRector.php

Copy file name to clipboardExpand all lines: src/Rector/MethodCall/GetHelperControllerToServiceRector.php
+44-32Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PhpParser\Node\Expr\MethodCall;
99
use PhpParser\Node\Expr\PropertyFetch;
1010
use PhpParser\Node\Expr\Variable;
11+
use PhpParser\Node\Identifier;
1112
use PhpParser\Node\Stmt\Class_;
1213
use PHPStan\Type\ObjectType;
1314
use Rector\Core\Rector\AbstractRector;
@@ -22,32 +23,21 @@
2223
* @changelog https://github.com/symfony/symfony/pull/42422
2324
* @changelog https://github.com/symfony/symfony/pull/1195
2425
*
25-
* @see \Rector\Symfony\Tests\Rector\MethodCall\GetDoctrineControllerToManagerRegistryRector\GetDoctrineControllerToManagerRegistryRectorTest
26+
* @see \Rector\Symfony\Tests\Rector\MethodCall\GetHelperControllerToServiceRector\GetHelperControllerToServiceRectorTest
2627
*/
27-
final class GetDoctrineControllerToManagerRegistryRector extends AbstractRector
28+
final class GetHelperControllerToServiceRector extends AbstractRector
2829
{
29-
/**
30-
* @var array<string, string>
31-
*/
32-
private const METHOD_NAME_TO_PROPERTY_TYPE = [
33-
'getDoctrine' => 'Doctrine\Persistence\ManagerRegistry',
34-
];
35-
3630
public function __construct(
3731
private readonly ControllerAnalyzer $controllerAnalyzer,
38-
<<<<<<< HEAD
3932
private readonly PropertyToAddCollector $propertyToAddCollector,
40-
=======
41-
private PropertyToAddCollector $propertyToAddCollector,
42-
private PropertyNaming $propertyNaming,
43-
>>>>>>> make GetDoctrineControllerToManagerRegistryRector generic
33+
private readonly PropertyNaming $propertyNaming,
4434
) {
4535
}
4636

4737
public function getRuleDefinition(): RuleDefinition
4838
{
4939
return new RuleDefinition(
50-
'Replace $this->getDoctrine() calls in AbstractController with direct Doctrine\Persistence\ManagerRegistry service',
40+
'Replace $this->getDoctrine() and $this->dispatchMessage() calls in AbstractController with direct service use',
5141
[
5242
new CodeSample(
5343
<<<'CODE_SAMPLE'
@@ -101,27 +91,49 @@ public function refactor(Node $node): ?Node
10191
return null;
10292
}
10393

104-
foreach (self::METHOD_NAME_TO_PROPERTY_TYPE as $methodName => $propertyType) {
105-
if (! $this->isName($node->name, $methodName)) {
106-
continue;
107-
}
108-
109-
$propertyName = $this->propertyNaming->fqnToVariableName($propertyType);
110-
111-
$class = $this->betterNodeFinder->findParentType($node, Class_::class);
112-
if (! $class instanceof Class_) {
113-
return null;
114-
}
94+
$class = $this->betterNodeFinder->findParentType($node, Class_::class);
95+
if (! $class instanceof Class_) {
96+
return null;
97+
}
11598

116-
// add dependency
117-
$propertyObjectType = new ObjectType($propertyType);
118-
$propertyMetadata = new PropertyMetadata($propertyName, $propertyObjectType, Class_::MODIFIER_PRIVATE);
119-
$this->propertyToAddCollector->addPropertyToClass($class, $propertyMetadata);
99+
if ($this->isName($node->name, 'getDoctrine')) {
100+
return $this->refactorGetDoctrine($class);
101+
}
120102

121-
$thisVariable = new Variable('this');
122-
return new PropertyFetch($thisVariable, $propertyName);
103+
if ($this->isName($node->name, 'dispatchMessage')) {
104+
return $this->refactorDispatchMessage($class, $node);
123105
}
124106

125107
return null;
126108
}
109+
110+
private function refactorDispatchMessage(Class_ $class, MethodCall $methodCall): Node|MethodCall
111+
{
112+
$propertyName = $this->propertyNaming->fqnToVariableName('Symfony\Component\Messenger\MessageBusInterface');
113+
114+
// add dependency
115+
$propertyObjectType = new ObjectType('Symfony\Component\Messenger\MessageBusInterface');
116+
$propertyMetadata = new PropertyMetadata($propertyName, $propertyObjectType, Class_::MODIFIER_PRIVATE);
117+
$this->propertyToAddCollector->addPropertyToClass($class, $propertyMetadata);
118+
119+
$thisVariable = new Variable('this');
120+
$methodCall->var = new PropertyFetch($thisVariable, $propertyName);
121+
$methodCall->name = new Identifier('dispatch');
122+
123+
return $methodCall;
124+
}
125+
126+
private function refactorGetDoctrine(Class_ $class): PropertyFetch
127+
{
128+
$propertyName = $this->propertyNaming->fqnToVariableName('Doctrine\Persistence\ManagerRegistry');
129+
130+
// add dependency
131+
$propertyObjectType = new ObjectType('Doctrine\Persistence\ManagerRegistry');
132+
$propertyMetadata = new PropertyMetadata($propertyName, $propertyObjectType, Class_::MODIFIER_PRIVATE);
133+
$this->propertyToAddCollector->addPropertyToClass($class, $propertyMetadata);
134+
135+
$thisVariable = new Variable('this');
136+
137+
return new PropertyFetch($thisVariable, $propertyName);
138+
}
127139
}

‎src/TypeAnalyzer/ControllerAnalyzer.php

Copy file name to clipboardExpand all lines: src/TypeAnalyzer/ControllerAnalyzer.php
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Rector\Symfony\TypeAnalyzer;
66

77
use PhpParser\Node;
8+
use PhpParser\Node\Expr;
89
use PHPStan\Analyser\Scope;
910
use PHPStan\Reflection\ClassReflection;
1011
use PHPStan\Type\ObjectType;
@@ -14,7 +15,7 @@
1415

1516
final class ControllerAnalyzer
1617
{
17-
public function isController(Node\Expr $expr): bool
18+
public function isController(Expr $expr): bool
1819
{
1920
$scope = $expr->getAttribute(AttributeKey::SCOPE);
2021

+34Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Rector\Symfony\Tests\Rector\MethodCall\GetHelperControllerToServiceRector\Fixture;
4+
5+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
6+
7+
final class DispatchMessage extends AbstractController
8+
{
9+
public function run()
10+
{
11+
$productRepository = $this->dispatchMessage('hey');
12+
}
13+
}
14+
15+
?>
16+
-----
17+
<?php
18+
19+
namespace Rector\Symfony\Tests\Rector\MethodCall\GetHelperControllerToServiceRector\Fixture;
20+
21+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
22+
23+
final class DispatchMessage extends AbstractController
24+
{
25+
public function __construct(private \Symfony\Component\Messenger\MessageBusInterface $messageBus)
26+
{
27+
}
28+
public function run()
29+
{
30+
$productRepository = $this->messageBus->dispatch('hey');
31+
}
32+
}
33+
34+
?>
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Rector\Symfony\Tests\Rector\MethodCall\GetDoctrineControllerToManagerRegistryRector\Fixture;
3+
namespace Rector\Symfony\Tests\Rector\MethodCall\GetHelperControllerToServiceRector\Fixture;
44

55
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
66

@@ -16,7 +16,7 @@ final class SomeController extends AbstractController
1616
-----
1717
<?php
1818

19-
namespace Rector\Symfony\Tests\Rector\MethodCall\GetDoctrineControllerToManagerRegistryRector\Fixture;
19+
namespace Rector\Symfony\Tests\Rector\MethodCall\GetHelperControllerToServiceRector\Fixture;
2020

2121
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
2222

+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
declare(strict_types=1);
44

5-
namespace Rector\Symfony\Tests\Rector\MethodCall\GetDoctrineControllerToManagerRegistryRector;
5+
namespace Rector\Symfony\Tests\Rector\MethodCall\GetHelperControllerToServiceRector;
66

77
use Iterator;
88
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
99
use Symplify\SmartFileSystem\SmartFileInfo;
1010

11-
final class GetDoctrineControllerToManagerRegistryRectorTest extends AbstractRectorTestCase
11+
final class GetHelperControllerToServiceRectorTest extends AbstractRectorTestCase
1212
{
1313
/**
1414
* @dataProvider provideData()
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
declare(strict_types=1);
44

5-
use Rector\Symfony\Rector\MethodCall\GetDoctrineControllerToManagerRegistryRector;
5+
use Rector\Symfony\Rector\MethodCall\GetHelperControllerToServiceRector;
66

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

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

1212
$services = $containerConfigurator->services();
13-
$services->set(GetDoctrineControllerToManagerRegistryRector::class);
13+
$services->set(GetHelperControllerToServiceRector::class);
1414
};

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.