Skip to content

Commit 3251eb7

Browse files
committed
🐛 [9138] Ensure that rule PreferPHPUnitSelfCall refactor call only for static methods, not any
rectorphp/rector#9138
1 parent a3a4def commit 3251eb7

File tree

4 files changed

+33
-11
lines changed

4 files changed

+33
-11
lines changed

rules-tests/CodeQuality/Rector/Class_/PreferPHPUnitSelfCallRector/Fixture/include_exceptions.php.inc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ final class IncludeExceptions extends TestCase
2626
{
2727
public function testMe()
2828
{
29-
self::expectException(\RuntimeException::class);
30-
self::expectExceptionMessage('foo');
31-
self::expectExceptionCode(123);
29+
$this->expectException(\RuntimeException::class);
30+
$this->expectExceptionMessage('foo');
31+
$this->expectExceptionCode(123);
3232
}
3333
}
3434

rules/CodeQuality/Enum/NonAssertStaticableMethods.php renamed to rules/CodeQuality/Enum/NonAssertPrefixedAssertionMethods.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Rector\PHPUnit\CodeQuality\Enum;
66

7-
final class NonAssertStaticableMethods
7+
final class NonAssertPrefixedAssertionMethods
88
{
99
/**
1010
* @var string[]

rules/CodeQuality/NodeAnalyser/AssertMethodAnalyzer.php

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
use PhpParser\Node\Expr\MethodCall;
88
use PhpParser\Node\Expr\StaticCall;
99
use PHPStan\Reflection\ClassReflection;
10+
use PHPStan\Reflection\ExtendedMethodReflection;
1011
use PHPStan\Type\ObjectType;
1112
use Rector\NodeNameResolver\NodeNameResolver;
1213
use Rector\NodeTypeResolver\NodeTypeResolver;
13-
use Rector\PHPUnit\CodeQuality\Enum\NonAssertStaticableMethods;
14+
use Rector\PHPUnit\CodeQuality\Enum\NonAssertPrefixedAssertionMethods;
1415
use Rector\PHPUnit\Enum\PHPUnitClassName;
1516
use Rector\Reflection\ReflectionResolver;
1617

@@ -36,7 +37,7 @@ public function detectTestCaseCall(MethodCall|StaticCall $call): bool
3637
$methodName = $this->nodeNameResolver->getName($call->name);
3738
if (! str_starts_with((string) $methodName, 'assert') && ! in_array(
3839
$methodName,
39-
NonAssertStaticableMethods::ALL
40+
NonAssertPrefixedAssertionMethods::ALL
4041
)) {
4142
return false;
4243
}
@@ -45,17 +46,38 @@ public function detectTestCaseCall(MethodCall|StaticCall $call): bool
4546
return false;
4647
}
4748

48-
$classReflection = $this->reflectionResolver->resolveClassReflection($call);
49-
if (! $classReflection instanceof ClassReflection) {
49+
$extendedMethodReflection = $this->resolveMethodReflection($call);
50+
if (! $extendedMethodReflection instanceof ExtendedMethodReflection) {
5051
return false;
5152
}
5253

53-
$extendedMethodReflection = $classReflection->getNativeMethod($methodName);
54-
5554
// only handle methods in TestCase or Assert class classes
5655
$declaringClassName = $extendedMethodReflection->getDeclaringClass()
5756
->getName();
5857

5958
return in_array($declaringClassName, [PHPUnitClassName::TEST_CASE, PHPUnitClassName::ASSERT]);
6059
}
60+
61+
public function detectTestCaseCallForStatic(MethodCall $methodCall): bool
62+
{
63+
if (! $this->detectTestCaseCall($methodCall)) {
64+
return false;
65+
}
66+
67+
$extendedMethodReflection = $this->resolveMethodReflection($methodCall);
68+
69+
return $extendedMethodReflection instanceof ExtendedMethodReflection && $extendedMethodReflection->isStatic();
70+
}
71+
72+
private function resolveMethodReflection(MethodCall|StaticCall $call): ?ExtendedMethodReflection
73+
{
74+
$methodName = $this->nodeNameResolver->getName($call->name);
75+
76+
$classReflection = $this->reflectionResolver->resolveClassReflection($call);
77+
if (! $classReflection instanceof ClassReflection) {
78+
return null;
79+
}
80+
81+
return $classReflection->getNativeMethod($methodName);
82+
}
6183
}

rules/CodeQuality/Rector/Class_/PreferPHPUnitSelfCallRector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function refactor(Node $node): ?Node
8383
return null;
8484
}
8585

86-
if (! $this->assertMethodAnalyzer->detectTestCaseCall($node)) {
86+
if (! $this->assertMethodAnalyzer->detectTestCaseCallForStatic($node)) {
8787
return null;
8888
}
8989

0 commit comments

Comments
 (0)