Skip to content

Commit 3ef2157

Browse files
committed
remove stmt key from TestListenerToHooksRector
1 parent 30b389a commit 3ef2157

2 files changed

Lines changed: 16 additions & 7 deletions

File tree

rules/PHPUnit90/Rector/Class_/TestListenerToHooksRector.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PhpParser\Node\Stmt\ClassMethod;
1212
use PHPStan\Type\ObjectType;
1313
use Rector\NodeTypeResolver\Node\AttributeKey;
14+
use Rector\PHPUnit\Enum\PHPUnitClassName;
1415
use Rector\Rector\AbstractRector;
1516
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1617
use 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]);

src/Enum/PHPUnitClassName.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,9 @@ final class PHPUnitClassName
2525
* @var string
2626
*/
2727
public const INVOCATION_ORDER = 'PHPUnit\Framework\MockObject\Rule\InvocationOrder';
28+
29+
/**
30+
* @var string
31+
*/
32+
public const TEST_LISTENER = 'PHPUnit\Framework\TestListener';
2833
}

0 commit comments

Comments
 (0)