|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\PHPUnit\PHPUnit120\Rector\Assign; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Expr\Assign; |
| 9 | +use PhpParser\Node\Expr\MethodCall; |
| 10 | +use PhpParser\Node\Expr\New_; |
| 11 | +use PhpParser\Node\Name\FullyQualified; |
| 12 | +use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; |
| 13 | +use Rector\Rector\AbstractRector; |
| 14 | +use Rector\VersionBonding\Contract\ComposerPackageConstraintInterface; |
| 15 | +use Rector\VersionBonding\ValueObject\ComposerPackageConstraint; |
| 16 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 17 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 18 | + |
| 19 | +/** |
| 20 | + * The TestCase::any() method was deprecated in PHPUnit 12.5 |
| 21 | + * |
| 22 | + * @see https://github.com/sebastianbergmann/phpunit/issues/6461 |
| 23 | + * |
| 24 | + * @see \Rector\PHPUnit\Tests\PHPUnit120\Rector\Assign\AnyMatcherToNewAnyInvokedCountRector\AnyMatcherToNewAnyInvokedCountRectorTest |
| 25 | + */ |
| 26 | +final class AnyMatcherToNewAnyInvokedCountRector extends AbstractRector implements ComposerPackageConstraintInterface |
| 27 | +{ |
| 28 | + private const string ANY_INVOKED_COUNT_CLASS = 'PHPUnit\Framework\MockObject\Rule\AnyInvokedCount'; |
| 29 | + |
| 30 | + public function __construct( |
| 31 | + private readonly TestsNodeAnalyzer $testsNodeAnalyzer |
| 32 | + ) { |
| 33 | + } |
| 34 | + |
| 35 | + public function provideComposerPackageConstraint(): ComposerPackageConstraint |
| 36 | + { |
| 37 | + return new ComposerPackageConstraint('phpunit/phpunit', '>=12.5'); |
| 38 | + } |
| 39 | + |
| 40 | + public function getRuleDefinition(): RuleDefinition |
| 41 | + { |
| 42 | + return new RuleDefinition( |
| 43 | + 'Change deprecated `$this->any()` matcher assign to direct `new AnyInvokedCount()`', |
| 44 | + [ |
| 45 | + new CodeSample( |
| 46 | + <<<'CODE_SAMPLE' |
| 47 | +use PHPUnit\Framework\TestCase; |
| 48 | +
|
| 49 | +final class SomeTest extends TestCase |
| 50 | +{ |
| 51 | + public function test(): void |
| 52 | + { |
| 53 | + $matcher = $this->any(); |
| 54 | + } |
| 55 | +} |
| 56 | +CODE_SAMPLE |
| 57 | + , |
| 58 | + <<<'CODE_SAMPLE' |
| 59 | +use PHPUnit\Framework\MockObject\Rule\AnyInvokedCount; |
| 60 | +use PHPUnit\Framework\TestCase; |
| 61 | +
|
| 62 | +final class SomeTest extends TestCase |
| 63 | +{ |
| 64 | + public function test(): void |
| 65 | + { |
| 66 | + $matcher = new AnyInvokedCount(); |
| 67 | + } |
| 68 | +} |
| 69 | +CODE_SAMPLE |
| 70 | + ), |
| 71 | + ] |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @return array<class-string<Node>> |
| 77 | + */ |
| 78 | + public function getNodeTypes(): array |
| 79 | + { |
| 80 | + return [Assign::class]; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @param Assign $node |
| 85 | + */ |
| 86 | + public function refactor(Node $node): ?Assign |
| 87 | + { |
| 88 | + if (! $node->expr instanceof MethodCall) { |
| 89 | + return null; |
| 90 | + } |
| 91 | + |
| 92 | + $methodCall = $node->expr; |
| 93 | + if ($methodCall->isFirstClassCallable()) { |
| 94 | + return null; |
| 95 | + } |
| 96 | + |
| 97 | + if (! $this->isName($methodCall->name, 'any')) { |
| 98 | + return null; |
| 99 | + } |
| 100 | + |
| 101 | + if ($methodCall->getArgs() !== []) { |
| 102 | + return null; |
| 103 | + } |
| 104 | + |
| 105 | + if (! $this->testsNodeAnalyzer->isPHPUnitTestCaseCall($methodCall)) { |
| 106 | + return null; |
| 107 | + } |
| 108 | + |
| 109 | + $node->expr = new New_(new FullyQualified(self::ANY_INVOKED_COUNT_CLASS)); |
| 110 | + |
| 111 | + return $node; |
| 112 | + } |
| 113 | +} |
0 commit comments