|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\PHPUnit\CodeQuality\Rector\Class_; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Expr\ArrowFunction; |
| 9 | +use PhpParser\Node\Expr\Closure; |
| 10 | +use PhpParser\Node\Expr\MethodCall; |
| 11 | +use PhpParser\Node\Expr\StaticCall; |
| 12 | +use PhpParser\Node\Stmt\Class_; |
| 13 | +use PhpParser\Node\Stmt\ClassMethod; |
| 14 | +use PhpParser\NodeVisitor; |
| 15 | +use Rector\PHPUnit\Enum\PHPUnitClassName; |
| 16 | +use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; |
| 17 | +use Rector\Rector\AbstractRector; |
| 18 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 19 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 20 | + |
| 21 | +/** |
| 22 | + * @see \Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\AssertClassToThisAssertRector\AssertClassToThisAssertRectorTest |
| 23 | + */ |
| 24 | +final class AssertClassToThisAssertRector extends AbstractRector |
| 25 | +{ |
| 26 | + public function __construct( |
| 27 | + private readonly TestsNodeAnalyzer $testsNodeAnalyzer |
| 28 | + ) { |
| 29 | + } |
| 30 | + |
| 31 | + public function getRuleDefinition(): RuleDefinition |
| 32 | + { |
| 33 | + return new RuleDefinition('Changes PHPUnit calls from Assert::assert*() to $this->assert*()', [ |
| 34 | + new CodeSample( |
| 35 | + <<<'CODE_SAMPLE' |
| 36 | +use PHPUnit\Framework\Assert; |
| 37 | +use PHPUnit\Framework\TestCase; |
| 38 | +
|
| 39 | +final class SomeClass extends TestCase |
| 40 | +{ |
| 41 | + public function run() |
| 42 | + { |
| 43 | + Assert::assertEquals('expected', $result); |
| 44 | + } |
| 45 | +} |
| 46 | +CODE_SAMPLE |
| 47 | + , |
| 48 | + <<<'CODE_SAMPLE' |
| 49 | +use PHPUnit\Framework\Assert; |
| 50 | +use PHPUnit\Framework\TestCase; |
| 51 | +
|
| 52 | +final class SomeClass extends TestCase |
| 53 | +{ |
| 54 | + public function run() |
| 55 | + { |
| 56 | + $this->assertEquals('expected', $result); |
| 57 | + } |
| 58 | +} |
| 59 | +CODE_SAMPLE |
| 60 | + ), |
| 61 | + ]); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @return array<class-string<Node>> |
| 66 | + */ |
| 67 | + public function getNodeTypes(): array |
| 68 | + { |
| 69 | + return [Class_::class]; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * @param Class_ $node |
| 74 | + */ |
| 75 | + public function refactor(Node $node): ?Node |
| 76 | + { |
| 77 | + if (! $this->testsNodeAnalyzer->isInTestClass($node)) { |
| 78 | + return null; |
| 79 | + } |
| 80 | + |
| 81 | + $hasChanged = false; |
| 82 | + |
| 83 | + $this->traverseNodesWithCallable($node, function (Node $node) use (&$hasChanged): int|null|MethodCall { |
| 84 | + $isInsideStaticFunctionLike = ($node instanceof ClassMethod && $node->isStatic()) || (($node instanceof Closure || $node instanceof ArrowFunction) && $node->static); |
| 85 | + if ($isInsideStaticFunctionLike) { |
| 86 | + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; |
| 87 | + } |
| 88 | + |
| 89 | + if (! $node instanceof StaticCall) { |
| 90 | + return null; |
| 91 | + } |
| 92 | + |
| 93 | + if ($node->isFirstClassCallable()) { |
| 94 | + return null; |
| 95 | + } |
| 96 | + |
| 97 | + if (! $this->isName($node->class, PHPUnitClassName::ASSERT)) { |
| 98 | + return null; |
| 99 | + } |
| 100 | + |
| 101 | + $methodName = $this->getName($node->name); |
| 102 | + if ($methodName === null) { |
| 103 | + return null; |
| 104 | + } |
| 105 | + |
| 106 | + $hasChanged = true; |
| 107 | + return $this->nodeFactory->createMethodCall('this', $methodName, $node->getArgs()); |
| 108 | + }); |
| 109 | + |
| 110 | + if ($hasChanged) { |
| 111 | + return $node; |
| 112 | + } |
| 113 | + |
| 114 | + return null; |
| 115 | + } |
| 116 | +} |
0 commit comments