1111use PhpParser \Node \Stmt \ClassMethod ;
1212use PHPStan \Type \ObjectType ;
1313use Rector \NodeTypeResolver \Node \AttributeKey ;
14+ use Rector \PHPUnit \Enum \PHPUnitClassName ;
1415use Rector \Rector \AbstractRector ;
1516use Symplify \RuleDocGenerator \ValueObject \CodeSample \CodeSample ;
1617use Symplify \RuleDocGenerator \ValueObject \RuleDefinition ;
@@ -137,26 +138,30 @@ public function getNodeTypes(): array
137138 */
138139 public function refactor (Node $ node ): ?Node
139140 {
140- if (! $ this ->isObjectType ($ node , new ObjectType (' PHPUnit\Framework\TestListener ' ))) {
141+ if (! $ this ->isObjectType ($ node , new ObjectType (PHPUnitClassName:: TEST_LISTENER ))) {
141142 return null ;
142143 }
143144
144145 foreach ($ node ->implements as $ key => $ implement ) {
145- if (! $ this ->isName ($ implement , ' PHPUnit\Framework\TestListener ' )) {
146+ if (! $ this ->isName ($ implement , PHPUnitClassName:: TEST_LISTENER )) {
146147 continue ;
147148 }
148149
149150 unset($ node ->implements [$ key ]);
150151 }
151152
152- foreach ($ node ->getMethods () as $ classMethod ) {
153- $ this ->processClassMethod ($ node , $ classMethod );
153+ foreach ($ node ->stmts as $ key => $ classStmt ) {
154+ if (! $ classStmt instanceof ClassMethod) {
155+ continue ;
156+ }
157+
158+ $ this ->processClassMethod ($ node , $ key , $ classStmt );
154159 }
155160
156161 return $ node ;
157162 }
158163
159- private function processClassMethod (Class_ $ class , ClassMethod $ classMethod ): void
164+ private function processClassMethod (Class_ $ class , int $ classMethodPosition , ClassMethod $ classMethod ): void
160165 {
161166 foreach (self ::LISTENER_METHOD_TO_HOOK_INTERFACES as $ methodName => $ hookClassAndMethod ) {
162167 /** @var string $methodName */
@@ -166,8 +171,7 @@ private function processClassMethod(Class_ $class, ClassMethod $classMethod): vo
166171
167172 // remove empty methods
168173 if ($ classMethod ->stmts === [] || $ classMethod ->stmts === null ) {
169- $ stmtKey = $ classMethod ->getAttribute (AttributeKey::STMT_KEY );
170- unset($ class ->stmts [$ stmtKey ]);
174+ unset($ class ->stmts [$ classMethodPosition ]);
171175 } else {
172176 $ class ->implements [] = new FullyQualified ($ hookClassAndMethod [0 ]);
173177 $ classMethod ->name = new Identifier ($ hookClassAndMethod [1 ]);
0 commit comments