diff --git a/rules-tests/CodeQuality/Rector/Class_/PreferPHPUnitSelfCallRector/Fixture/include_exceptions.php.inc b/rules-tests/CodeQuality/Rector/Class_/PreferPHPUnitSelfCallRector/Fixture/include_exceptions.php.inc deleted file mode 100644 index eab2cb99..00000000 --- a/rules-tests/CodeQuality/Rector/Class_/PreferPHPUnitSelfCallRector/Fixture/include_exceptions.php.inc +++ /dev/null @@ -1,35 +0,0 @@ -expectException(\RuntimeException::class); - $this->expectExceptionMessage('foo'); - $this->expectExceptionCode(123); - } -} - -?> ------ - diff --git a/rules-tests/CodeQuality/Rector/Class_/PreferPHPUnitSelfCallRector/Fixture/skip_exceptions.php.inc b/rules-tests/CodeQuality/Rector/Class_/PreferPHPUnitSelfCallRector/Fixture/skip_exceptions.php.inc new file mode 100644 index 00000000..f400697e --- /dev/null +++ b/rules-tests/CodeQuality/Rector/Class_/PreferPHPUnitSelfCallRector/Fixture/skip_exceptions.php.inc @@ -0,0 +1,16 @@ +expectException(\RuntimeException::class); + $this->expectExceptionMessage('foo'); + $this->expectExceptionCode(123); + } +} diff --git a/rules/CodeQuality/Enum/NonAssertStaticableMethods.php b/rules/CodeQuality/Enum/NonAssertNonStaticMethods.php similarity index 90% rename from rules/CodeQuality/Enum/NonAssertStaticableMethods.php rename to rules/CodeQuality/Enum/NonAssertNonStaticMethods.php index 608cf520..a8813de5 100644 --- a/rules/CodeQuality/Enum/NonAssertStaticableMethods.php +++ b/rules/CodeQuality/Enum/NonAssertNonStaticMethods.php @@ -4,7 +4,7 @@ namespace Rector\PHPUnit\CodeQuality\Enum; -final class NonAssertStaticableMethods +final class NonAssertNonStaticMethods { /** * @var string[] diff --git a/rules/CodeQuality/NodeAnalyser/AssertMethodAnalyzer.php b/rules/CodeQuality/NodeAnalyser/AssertMethodAnalyzer.php index 9d29a191..7c0ef823 100644 --- a/rules/CodeQuality/NodeAnalyser/AssertMethodAnalyzer.php +++ b/rules/CodeQuality/NodeAnalyser/AssertMethodAnalyzer.php @@ -7,10 +7,11 @@ use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\StaticCall; use PHPStan\Reflection\ClassReflection; +use PHPStan\Reflection\ExtendedMethodReflection; use PHPStan\Type\ObjectType; use Rector\NodeNameResolver\NodeNameResolver; use Rector\NodeTypeResolver\NodeTypeResolver; -use Rector\PHPUnit\CodeQuality\Enum\NonAssertStaticableMethods; +use Rector\PHPUnit\CodeQuality\Enum\NonAssertNonStaticMethods; use Rector\PHPUnit\Enum\PHPUnitClassName; use Rector\Reflection\ReflectionResolver; @@ -36,26 +37,48 @@ public function detectTestCaseCall(MethodCall|StaticCall $call): bool $methodName = $this->nodeNameResolver->getName($call->name); if (! str_starts_with((string) $methodName, 'assert') && ! in_array( $methodName, - NonAssertStaticableMethods::ALL + NonAssertNonStaticMethods::ALL, + true )) { return false; } - $classReflection = $this->reflectionResolver->resolveClassReflection($call); - if (! $classReflection instanceof ClassReflection) { + if ($call instanceof StaticCall && ! $this->nodeNameResolver->isNames($call->class, ['static', 'self'])) { return false; } - if ($call instanceof StaticCall && ! $this->nodeNameResolver->isNames($call->class, ['static', 'self'])) { + $extendedMethodReflection = $this->resolveMethodReflection($call); + if (! $extendedMethodReflection instanceof ExtendedMethodReflection) { return false; } - $extendedMethodReflection = $classReflection->getNativeMethod($methodName); - // only handle methods in TestCase or Assert class classes $declaringClassName = $extendedMethodReflection->getDeclaringClass() ->getName(); return in_array($declaringClassName, [PHPUnitClassName::TEST_CASE, PHPUnitClassName::ASSERT]); } + + public function detectTestCaseCallForStatic(MethodCall $methodCall): bool + { + if (! $this->detectTestCaseCall($methodCall)) { + return false; + } + + $extendedMethodReflection = $this->resolveMethodReflection($methodCall); + + return $extendedMethodReflection instanceof ExtendedMethodReflection && $extendedMethodReflection->isStatic(); + } + + private function resolveMethodReflection(MethodCall|StaticCall $call): ?ExtendedMethodReflection + { + $methodName = $this->nodeNameResolver->getName($call->name); + + $classReflection = $this->reflectionResolver->resolveClassReflection($call); + if (! $classReflection instanceof ClassReflection) { + return null; + } + + return $classReflection->getNativeMethod($methodName); + } } diff --git a/rules/CodeQuality/Rector/Class_/PreferPHPUnitSelfCallRector.php b/rules/CodeQuality/Rector/Class_/PreferPHPUnitSelfCallRector.php index fe9e38ab..2f6b1a25 100644 --- a/rules/CodeQuality/Rector/Class_/PreferPHPUnitSelfCallRector.php +++ b/rules/CodeQuality/Rector/Class_/PreferPHPUnitSelfCallRector.php @@ -83,7 +83,7 @@ public function refactor(Node $node): ?Node return null; } - if (! $this->assertMethodAnalyzer->detectTestCaseCall($node)) { + if (! $this->assertMethodAnalyzer->detectTestCaseCallForStatic($node)) { return null; }