Skip to content
Merged

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\PreferPHPUnitSelfCallRector\Fixture;

use PHPUnit\Framework\TestCase;

final class SkipExceptions extends TestCase
{
public function testMe()
{
// this calls should be preserved with $this, because methods are not static
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('foo');
$this->expectExceptionCode(123);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Rector\PHPUnit\CodeQuality\Enum;

final class NonAssertStaticableMethods
final class NonAssertNonStaticMethods
{
/**
* @var string[]
Expand Down
37 changes: 30 additions & 7 deletions rules/CodeQuality/NodeAnalyser/AssertMethodAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function refactor(Node $node): ?Node
return null;
}

if (! $this->assertMethodAnalyzer->detectTestCaseCall($node)) {
if (! $this->assertMethodAnalyzer->detectTestCaseCallForStatic($node)) {
return null;
}

Expand Down
Loading