Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"php": ">=8.2"
},
"require-dev": {
"phpecs/phpecs": "^2.0",
"phpecs/phpecs": "^2.1.1",
"phpstan/extension-installer": "^1.4",
"phpstan/phpstan": "^2.1.8",
"phpstan/phpstan-deprecation-rules": "^2.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

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

use PHPUnit\Framework\TestCase;

final class IncludeExceptions extends TestCase
{
public function testMe()
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('foo');
$this->expectExceptionCode(123);
}
}

?>
-----
<?php

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

use PHPUnit\Framework\TestCase;

final class IncludeExceptions extends TestCase
{
public function testMe()
{
self::expectException(\RuntimeException::class);
self::expectExceptionMessage('foo');
self::expectExceptionCode(123);
}
}

?>
23 changes: 23 additions & 0 deletions rules/CodeQuality/Enum/NonAssertStaticableMethods.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\CodeQuality\Enum;

final class NonAssertStaticableMethods
{
/**
* @var string[]
*/
public const ALL = [
'createMock',
'atLeast',
'atLeastOnce',
'once',
'never',
'expectException',
'expectExceptionMessage',
'expectExceptionCode',
'expectExceptionMessageMatches',
];
}
50 changes: 50 additions & 0 deletions rules/CodeQuality/NodeAnalyser/AssertMethodAnalyzer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\CodeQuality\NodeAnalyser;

use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PHPStan\Reflection\ClassReflection;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\PHPUnit\CodeQuality\Enum\NonAssertStaticableMethods;
use Rector\PHPUnit\Enum\PHPUnitClassName;
use Rector\Reflection\ReflectionResolver;

final readonly class AssertMethodAnalyzer
{
public function __construct(
private NodeNameResolver $nodeNameResolver,
private ReflectionResolver $reflectionResolver,
) {
}

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
)) {
return false;
}

$classReflection = $this->reflectionResolver->resolveClassReflection($call);
if (! $classReflection instanceof ClassReflection) {
return false;
}

if ($call instanceof StaticCall && ! $this->nodeNameResolver->isNames($call->class, ['static', 'self'])) {
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]);
}
}
39 changes: 8 additions & 31 deletions rules/CodeQuality/Rector/Class_/PreferPHPUnitSelfCallRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,20 @@
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Stmt\Class_;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\ObjectType;
use Rector\PHPUnit\CodeQuality\NodeAnalyser\AssertMethodAnalyzer;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\Rector\AbstractRector;
use Rector\Reflection\ReflectionResolver;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\PHPUnit\Tests\Rector\Class_\PreferPHPUnitSelfCallRector\PreferPHPUnitSelfCallRectorTest
* @see \Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\PreferPHPUnitSelfCallRector\PreferPHPUnitSelfCallRectorTest
*/
final class PreferPHPUnitSelfCallRector extends AbstractRector
{
public function __construct(
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
private readonly ReflectionResolver $reflectionResolver,
private readonly AssertMethodAnalyzer $assertMethodAnalyzer,
) {
}

Expand Down Expand Up @@ -81,39 +79,18 @@ public function refactor(Node $node): ?Node
return null;
}

$methodName = $this->getName($node->name);
if (! is_string($methodName)) {
return null;
}

if (! str_starts_with($methodName, 'assert')) {
return null;
}

if (! $this->isName($node->var, 'this')) {
if ($node->isFirstClassCallable()) {
return null;
}

if (! $this->isObjectType($node->var, new ObjectType('PHPUnit\Framework\TestCase'))) {
if (! $this->assertMethodAnalyzer->detectTestCaseCall($node)) {
return null;
}

$classReflection = $this->reflectionResolver->resolveClassReflection($node);
if ($classReflection instanceof ClassReflection && $classReflection->hasNativeMethod($methodName)) {
$method = $classReflection->getNativeMethod($methodName);

if ($node->isFirstClassCallable()) {
return null;
}

if ($method->isStatic()) {
$hasChanged = true;

return $this->nodeFactory->createStaticCall('self', $methodName, $node->getArgs());
}
}
$methodName = $this->getName($node->name);

return null;
$hasChanged = true;
return $this->nodeFactory->createStaticCall('self', $methodName, $node->getArgs());
});

if ($hasChanged) {
Expand Down
28 changes: 4 additions & 24 deletions rules/CodeQuality/Rector/Class_/PreferPHPUnitThisCallRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\NodeVisitor;
use Rector\PHPUnit\CodeQuality\NodeAnalyser\AssertMethodAnalyzer;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand All @@ -21,23 +22,9 @@
*/
final class PreferPHPUnitThisCallRector extends AbstractRector
{
/**
* @var string[]
*/
private const NON_ASSERT_STATIC_METHODS = [
'createMock',
'atLeast',
'atLeastOnce',
'once',
'never',
'expectException',
'expectExceptionMessage',
'expectExceptionCode',
'expectExceptionMessageMatches',
];

public function __construct(
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
private readonly AssertMethodAnalyzer $assertMethodAnalyzer
) {
}

Expand Down Expand Up @@ -105,18 +92,11 @@ public function refactor(Node $node): ?Node
return null;
}

$methodName = $this->getName($node->name);
if (! is_string($methodName)) {
return null;
}

if (! $this->isNames($node->class, ['static', 'self'])) {
if (! $this->assertMethodAnalyzer->detectTestCaseCall($node)) {
return null;
}

if (! str_starts_with($methodName, 'assert') && ! in_array($methodName, self::NON_ASSERT_STATIC_METHODS)) {
return null;
}
$methodName = $this->getName($node->name);

$hasChanged = true;
return $this->nodeFactory->createMethodCall('this', $methodName, $node->getArgs());
Expand Down
Loading