|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\PHPUnit\CodeQuality\Rector\MethodCall; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Expr\MethodCall; |
| 9 | +use PhpParser\Node\Identifier; |
| 10 | +use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; |
| 11 | +use Rector\Rector\AbstractRector; |
| 12 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 13 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 14 | + |
| 15 | +/** |
| 16 | + * @see \Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertThatToDirectAssertRector\AssertThatToDirectAssertRectorTest |
| 17 | + */ |
| 18 | +final class AssertThatToDirectAssertRector extends AbstractRector |
| 19 | +{ |
| 20 | + public function __construct( |
| 21 | + private readonly TestsNodeAnalyzer $testsNodeAnalyzer, |
| 22 | + ) { |
| 23 | + } |
| 24 | + |
| 25 | + public function getRuleDefinition(): RuleDefinition |
| 26 | + { |
| 27 | + return new RuleDefinition( |
| 28 | + 'Change $this->assertThat($value, $this->*()) to direct $this->assert*() method', |
| 29 | + [ |
| 30 | + new CodeSample( |
| 31 | + <<<'CODE_SAMPLE' |
| 32 | +use PHPUnit\Framework\TestCase; |
| 33 | +
|
| 34 | +final class SomeTest extends TestCase |
| 35 | +{ |
| 36 | + public function test() |
| 37 | + { |
| 38 | + $this->assertThat($value, $this->isTrue()); |
| 39 | + } |
| 40 | +} |
| 41 | +CODE_SAMPLE |
| 42 | + |
| 43 | + , |
| 44 | + <<<'CODE_SAMPLE' |
| 45 | +use PHPUnit\Framework\TestCase; |
| 46 | +
|
| 47 | +final class SomeTest extends TestCase |
| 48 | +{ |
| 49 | + public function test() |
| 50 | + { |
| 51 | + $this->assertTrue($value); |
| 52 | + } |
| 53 | +} |
| 54 | +CODE_SAMPLE |
| 55 | + ), |
| 56 | + ] |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @return array<class-string<Node>> |
| 62 | + */ |
| 63 | + public function getNodeTypes(): array |
| 64 | + { |
| 65 | + return [MethodCall::class]; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @param MethodCall $node |
| 70 | + */ |
| 71 | + public function refactor(Node $node): ?Node |
| 72 | + { |
| 73 | + if ($node->isFirstClassCallable()) { |
| 74 | + return null; |
| 75 | + } |
| 76 | + |
| 77 | + if (! $this->testsNodeAnalyzer->isPHPUnitMethodCallNames($node, ['assertThat'])) { |
| 78 | + return null; |
| 79 | + } |
| 80 | + |
| 81 | + $secondArg = $node->getArgs()[1]; |
| 82 | + if (! $secondArg->value instanceof MethodCall) { |
| 83 | + return null; |
| 84 | + } |
| 85 | + |
| 86 | + $exactAssertMethodCall = $secondArg->value; |
| 87 | + $exactAssertName = $this->getName($exactAssertMethodCall->name); |
| 88 | + |
| 89 | + if ($exactAssertName === 'equalTo') { |
| 90 | + $node->name = new Identifier('assertEquals'); |
| 91 | + |
| 92 | + $node->args[1] = $node->args[0]; |
| 93 | + $node->args[0] = $exactAssertMethodCall->args[0]; |
| 94 | + |
| 95 | + return $node; |
| 96 | + } |
| 97 | + |
| 98 | + if ($exactAssertName === 'isTrue') { |
| 99 | + $node->name = new Identifier('assertTrue'); |
| 100 | + unset($node->args[1]); |
| 101 | + |
| 102 | + return $node; |
| 103 | + } |
| 104 | + |
| 105 | + return null; |
| 106 | + } |
| 107 | +} |
0 commit comments