|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of the RegexParser package. |
| 7 | + * |
| 8 | + * (c) Younes ENNAJI <[email protected]> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view the LICENSE |
| 11 | + * file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace RegexParser\Rector\Rule\PHPUnit; |
| 15 | + |
| 16 | +use PhpParser\Node; |
| 17 | +use PhpParser\Node\Expr\MethodCall; |
| 18 | +use PhpParser\Node\Expr\StaticCall; |
| 19 | +use PHPUnit\Framework\Assert; |
| 20 | +use Rector\PHPUnit\CodeQuality\NodeAnalyser\AssertMethodAnalyzer; |
| 21 | +use Rector\Rector\AbstractRector; |
| 22 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 23 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 24 | + |
| 25 | +/** |
| 26 | + * Converts PHPUnit assertions from $this->assert*() and static::assert*() to explicit Assert::assert*() static calls. |
| 27 | + */ |
| 28 | +class PreferStaticPHPUnitAssertRector extends AbstractRector |
| 29 | +{ |
| 30 | + public function __construct(private AssertMethodAnalyzer $assertMethodAnalyzer) {} |
| 31 | + |
| 32 | + public function getRuleDefinition(): RuleDefinition |
| 33 | + { |
| 34 | + return new RuleDefinition( |
| 35 | + 'Changes PHPUnit assertion calls from $this->assert*() or static::assert*() to explicit Assert::assert*() static calls.', |
| 36 | + [ |
| 37 | + new CodeSample( |
| 38 | + <<<'CODE_SAMPLE' |
| 39 | + use PHPUnit\Framework\TestCase; |
| 40 | +
|
| 41 | + final class SomeTest extends TestCase { |
| 42 | + public function testSomething() { |
| 43 | + $this->assertEquals(1, 2); |
| 44 | + static::assertSame('foo', 'bar'); |
| 45 | + } |
| 46 | + } |
| 47 | + CODE_SAMPLE, |
| 48 | + <<<'CODE_SAMPLE' |
| 49 | + use PHPUnit\Framework\TestCase; |
| 50 | + use PHPUnit\Framework\Assert; |
| 51 | +
|
| 52 | + final class SomeTest extends TestCase { |
| 53 | + public function testSomething() { |
| 54 | + Assert::assertEquals(1, 2); |
| 55 | + Assert::assertSame('foo', 'bar'); |
| 56 | + } |
| 57 | + } |
| 58 | + CODE_SAMPLE |
| 59 | + ), |
| 60 | + ], |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + public function getNodeTypes(): array |
| 65 | + { |
| 66 | + return [MethodCall::class, StaticCall::class]; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @param MethodCall|StaticCall $node |
| 71 | + */ |
| 72 | + public function refactor(Node $node): ?Node |
| 73 | + { |
| 74 | + if ($node->isFirstClassCallable()) { |
| 75 | + return null; |
| 76 | + } |
| 77 | + |
| 78 | + if ($node instanceof MethodCall && !$this->assertMethodAnalyzer->detectTestCaseCallForStatic($node)) { |
| 79 | + return null; |
| 80 | + } |
| 81 | + |
| 82 | + if ($node instanceof StaticCall && !$this->assertMethodAnalyzer->detectTestCaseCall($node)) { |
| 83 | + return null; |
| 84 | + } |
| 85 | + |
| 86 | + $methodName = $this->getName($node->name); |
| 87 | + if (null === $methodName || !str_starts_with($methodName, 'assert')) { |
| 88 | + return null; |
| 89 | + } |
| 90 | + |
| 91 | + return $this->nodeFactory->createStaticCall( |
| 92 | + Assert::class, |
| 93 | + $methodName, |
| 94 | + $node->getArgs(), |
| 95 | + ); |
| 96 | + } |
| 97 | +} |
0 commit comments