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 82ed160

Browse filesBrowse files
committed
make GetDoctrineControllerToManagerRegistryRector generic
1 parent b58f5c9 commit 82ed160
Copy full SHA for 82ed160

File tree

Expand file treeCollapse file tree

1 file changed

+32
-14
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+32
-14
lines changed

‎src/Rector/MethodCall/GetDoctrineControllerToManagerRegistryRector.php

Copy file name to clipboardExpand all lines: src/Rector/MethodCall/GetDoctrineControllerToManagerRegistryRector.php
+32-14Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PhpParser\Node\Stmt\Class_;
1212
use PHPStan\Type\ObjectType;
1313
use Rector\Core\Rector\AbstractRector;
14+
use Rector\Naming\Naming\PropertyNaming;
1415
use Rector\PostRector\Collector\PropertyToAddCollector;
1516
use Rector\PostRector\ValueObject\PropertyMetadata;
1617
use Rector\Symfony\TypeAnalyzer\ControllerAnalyzer;
@@ -25,9 +26,21 @@
2526
*/
2627
final class GetDoctrineControllerToManagerRegistryRector extends AbstractRector
2728
{
29+
/**
30+
* @var array<string, string>
31+
*/
32+
private const METHOD_NAME_TO_PROPERTY_TYPE = [
33+
'getDoctrine' => 'Doctrine\Persistence\ManagerRegistry',
34+
];
35+
2836
public function __construct(
2937
private readonly ControllerAnalyzer $controllerAnalyzer,
38+
<<<<<<< HEAD
3039
private readonly PropertyToAddCollector $propertyToAddCollector,
40+
=======
41+
private PropertyToAddCollector $propertyToAddCollector,
42+
private PropertyNaming $propertyNaming,
43+
>>>>>>> make GetDoctrineControllerToManagerRegistryRector generic
3144
) {
3245
}
3346

@@ -88,22 +101,27 @@ public function refactor(Node $node): ?Node
88101
return null;
89102
}
90103

91-
if (! $this->isName($node->name, 'getDoctrine')) {
92-
return null;
93-
}
104+
foreach (self::METHOD_NAME_TO_PROPERTY_TYPE as $methodName => $propertyType) {
105+
if (! $this->isName($node->name, $methodName)) {
106+
continue;
107+
}
94108

95-
$class = $this->betterNodeFinder->findParentType($node, Class_::class);
96-
if (! $class instanceof Class_) {
97-
return null;
98-
}
109+
$propertyName = $this->propertyNaming->fqnToVariableName($propertyType);
110+
111+
$class = $this->betterNodeFinder->findParentType($node, Class_::class);
112+
if (! $class instanceof Class_) {
113+
return null;
114+
}
99115

100-
// add dependency
101-
$propertyMetadata = new PropertyMetadata('managerRegistry', new ObjectType(
102-
'Doctrine\Persistence\ManagerRegistry'
103-
), Class_::MODIFIER_PRIVATE);
104-
$this->propertyToAddCollector->addPropertyToClass($class, $propertyMetadata);
116+
// add dependency
117+
$propertyObjectType = new ObjectType($propertyType);
118+
$propertyMetadata = new PropertyMetadata($propertyName, $propertyObjectType, Class_::MODIFIER_PRIVATE);
119+
$this->propertyToAddCollector->addPropertyToClass($class, $propertyMetadata);
120+
121+
$thisVariable = new Variable('this');
122+
return new PropertyFetch($thisVariable, $propertyName);
123+
}
105124

106-
$thisVariable = new Variable('this');
107-
return new PropertyFetch($thisVariable, 'managerRegistry');
125+
return null;
108126
}
109127
}

0 commit comments

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