12
12
use Rector \Php80 \NodeAnalyzer \PhpAttributeAnalyzer ;
13
13
use Rector \Rector \AbstractRector ;
14
14
use Rector \Symfony \ApplicationMetadata \ListenerServiceDefinitionProvider ;
15
+ use Rector \Symfony \Enum \SymfonyAttribute ;
16
+ use Rector \Symfony \Enum \SymfonyClass ;
15
17
use Rector \Symfony \NodeAnalyzer \ClassAnalyzer ;
16
18
use Rector \Symfony \NodeFactory \GetSubscribedEventsClassMethodFactory ;
17
19
use Rector \Symfony \ValueObject \EventNameToClassAndConstant ;
24
26
*/
25
27
final class EventListenerToEventSubscriberRector extends AbstractRector
26
28
{
27
- /**
28
- * @var string
29
- */
30
- private const EVENT_SUBSCRIBER_INTERFACE = 'Symfony\Component\EventDispatcher\EventSubscriberInterface ' ;
31
-
32
- /**
33
- * @var string
34
- */
35
- private const EVENT_LISTENER_ATTRIBUTE = 'Symfony\Component\EventDispatcher\Attribute\AsEventListener ' ;
36
-
37
- /**
38
- * @var string
39
- */
40
- private const KERNEL_EVENTS_CLASS = 'Symfony\Component\HttpKernel\KernelEvents ' ;
41
-
42
- /**
43
- * @var string
44
- */
45
- private const CONSOLE_EVENTS_CLASS = 'Symfony\Component\Console\ConsoleEvents ' ;
46
-
47
29
/**
48
30
* @var string
49
31
* @changelog https://regex101.com/r/qiHZ4T/1
@@ -63,22 +45,26 @@ public function __construct(
63
45
) {
64
46
$ this ->eventNamesToClassConstants = [
65
47
// kernel events
66
- new EventNameToClassAndConstant ('kernel.request ' , self ::KERNEL_EVENTS_CLASS , 'REQUEST ' ),
67
- new EventNameToClassAndConstant ('kernel.exception ' , self ::KERNEL_EVENTS_CLASS , 'EXCEPTION ' ),
68
- new EventNameToClassAndConstant ('kernel.view ' , self ::KERNEL_EVENTS_CLASS , 'VIEW ' ),
69
- new EventNameToClassAndConstant ('kernel.controller ' , self ::KERNEL_EVENTS_CLASS , 'CONTROLLER ' ),
48
+ new EventNameToClassAndConstant ('kernel.request ' , SymfonyClass ::KERNEL_EVENTS_CLASS , 'REQUEST ' ),
49
+ new EventNameToClassAndConstant ('kernel.exception ' , SymfonyClass ::KERNEL_EVENTS_CLASS , 'EXCEPTION ' ),
50
+ new EventNameToClassAndConstant ('kernel.view ' , SymfonyClass ::KERNEL_EVENTS_CLASS , 'VIEW ' ),
51
+ new EventNameToClassAndConstant ('kernel.controller ' , SymfonyClass ::KERNEL_EVENTS_CLASS , 'CONTROLLER ' ),
70
52
new EventNameToClassAndConstant (
71
53
'kernel.controller_arguments ' ,
72
- self ::KERNEL_EVENTS_CLASS ,
54
+ SymfonyClass ::KERNEL_EVENTS_CLASS ,
73
55
'CONTROLLER_ARGUMENTS '
74
56
),
75
- new EventNameToClassAndConstant ('kernel.response ' , self ::KERNEL_EVENTS_CLASS , 'RESPONSE ' ),
76
- new EventNameToClassAndConstant ('kernel.terminate ' , self ::KERNEL_EVENTS_CLASS , 'TERMINATE ' ),
77
- new EventNameToClassAndConstant ('kernel.finish_request ' , self ::KERNEL_EVENTS_CLASS , 'FINISH_REQUEST ' ),
57
+ new EventNameToClassAndConstant ('kernel.response ' , SymfonyClass::KERNEL_EVENTS_CLASS , 'RESPONSE ' ),
58
+ new EventNameToClassAndConstant ('kernel.terminate ' , SymfonyClass::KERNEL_EVENTS_CLASS , 'TERMINATE ' ),
59
+ new EventNameToClassAndConstant (
60
+ 'kernel.finish_request ' ,
61
+ SymfonyClass::KERNEL_EVENTS_CLASS ,
62
+ 'FINISH_REQUEST '
63
+ ),
78
64
// console events
79
- new EventNameToClassAndConstant ('console.command ' , self ::CONSOLE_EVENTS_CLASS , 'COMMAND ' ),
80
- new EventNameToClassAndConstant ('console.terminate ' , self ::CONSOLE_EVENTS_CLASS , 'TERMINATE ' ),
81
- new EventNameToClassAndConstant ('console.error ' , self ::CONSOLE_EVENTS_CLASS , 'ERROR ' ),
65
+ new EventNameToClassAndConstant ('console.command ' , SymfonyClass ::CONSOLE_EVENTS_CLASS , 'COMMAND ' ),
66
+ new EventNameToClassAndConstant ('console.terminate ' , SymfonyClass ::CONSOLE_EVENTS_CLASS , 'TERMINATE ' ),
67
+ new EventNameToClassAndConstant ('console.error ' , SymfonyClass ::CONSOLE_EVENTS_CLASS , 'ERROR ' ),
82
68
];
83
69
}
84
70
@@ -139,17 +125,7 @@ public function getNodeTypes(): array
139
125
*/
140
126
public function refactor (Node $ node ): ?Node
141
127
{
142
- // anonymous class
143
- if (! $ node ->name instanceof Identifier) {
144
- return null ;
145
- }
146
-
147
- // is already a subscriber
148
- if ($ this ->classAnalyzer ->hasImplements ($ node , 'Symfony\Component\EventDispatcher\EventSubscriberInterface ' )) {
149
- return null ;
150
- }
151
-
152
- if ($ this ->hasAsListenerAttribute ($ node )) {
128
+ if ($ this ->shouldSkipClass ($ node )) {
153
129
return null ;
154
130
}
155
131
@@ -173,7 +149,7 @@ public function refactor(Node $node): ?Node
173
149
*/
174
150
private function changeListenerToSubscriberWithMethods (Class_ $ class , array $ eventsToMethods ): void
175
151
{
176
- $ class ->implements [] = new FullyQualified (self ::EVENT_SUBSCRIBER_INTERFACE );
152
+ $ class ->implements [] = new FullyQualified (SymfonyClass ::EVENT_SUBSCRIBER_INTERFACE );
177
153
178
154
$ classShortName = $ this ->nodeNameResolver ->getShortName ($ class );
179
155
@@ -194,7 +170,7 @@ private function changeListenerToSubscriberWithMethods(Class_ $class, array $eve
194
170
*/
195
171
private function hasAsListenerAttribute (Class_ $ class ): bool
196
172
{
197
- if ($ this ->phpAttributeAnalyzer ->hasPhpAttribute ($ class , self ::EVENT_LISTENER_ATTRIBUTE )) {
173
+ if ($ this ->phpAttributeAnalyzer ->hasPhpAttribute ($ class , SymfonyAttribute ::EVENT_LISTENER_ATTRIBUTE )) {
198
174
return true ;
199
175
}
200
176
@@ -203,11 +179,29 @@ private function hasAsListenerAttribute(Class_ $class): bool
203
179
continue ;
204
180
}
205
181
206
- if ($ this ->phpAttributeAnalyzer ->hasPhpAttribute ($ classMethod , self ::EVENT_LISTENER_ATTRIBUTE )) {
182
+ if ($ this ->phpAttributeAnalyzer ->hasPhpAttribute (
183
+ $ classMethod ,
184
+ SymfonyAttribute::EVENT_LISTENER_ATTRIBUTE
185
+ )) {
207
186
return true ;
208
187
}
209
188
}
210
189
211
190
return false ;
212
191
}
192
+
193
+ private function shouldSkipClass (Class_ $ class ): bool
194
+ {
195
+ // anonymous class
196
+ if ($ class ->isAnonymous ()) {
197
+ return true ;
198
+ }
199
+
200
+ // is already a subscriber
201
+ if ($ this ->classAnalyzer ->hasImplements ($ class , SymfonyClass::EVENT_SUBSCRIBER_INTERFACE )) {
202
+ return true ;
203
+ }
204
+
205
+ return $ this ->hasAsListenerAttribute ($ class );
206
+ }
213
207
}
0 commit comments