11
11
use PhpParser \Node \Stmt \Class_ ;
12
12
use PHPStan \Type \ObjectType ;
13
13
use Rector \Core \Rector \AbstractRector ;
14
+ use Rector \Naming \Naming \PropertyNaming ;
14
15
use Rector \PostRector \Collector \PropertyToAddCollector ;
15
16
use Rector \PostRector \ValueObject \PropertyMetadata ;
16
17
use Rector \Symfony \TypeAnalyzer \ControllerAnalyzer ;
25
26
*/
26
27
final class GetDoctrineControllerToManagerRegistryRector extends AbstractRector
27
28
{
29
+ /**
30
+ * @var array<string, string>
31
+ */
32
+ private const METHOD_NAME_TO_PROPERTY_TYPE = [
33
+ 'getDoctrine ' => 'Doctrine\Persistence\ManagerRegistry ' ,
34
+ ];
35
+
28
36
public function __construct (
29
37
private readonly ControllerAnalyzer $ controllerAnalyzer ,
38
+ <<<<<<< HEAD
30
39
private readonly PropertyToAddCollector $ propertyToAddCollector ,
40
+ =======
41
+ private PropertyToAddCollector $ propertyToAddCollector ,
42
+ private PropertyNaming $ propertyNaming ,
43
+ >>>>>>> make GetDoctrineControllerToManagerRegistryRector generic
31
44
) {
32
45
}
33
46
@@ -88,22 +101,27 @@ public function refactor(Node $node): ?Node
88
101
return null ;
89
102
}
90
103
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
+ }
94
108
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
+ }
99
115
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
+ }
105
124
106
- $ thisVariable = new Variable ('this ' );
107
- return new PropertyFetch ($ thisVariable , 'managerRegistry ' );
125
+ return null ;
108
126
}
109
127
}
0 commit comments