|
| 1 | +<?php |
| 2 | + |
| 3 | +declare (strict_types=1); |
| 4 | +namespace Rector\PHPUnit\PHPUnit120\Rector\CallLike; |
| 5 | + |
| 6 | +use PhpParser\Node; |
| 7 | +use PhpParser\Node\ArrayItem; |
| 8 | +use PhpParser\Node\Expr\BinaryOp\Coalesce; |
| 9 | +use PhpParser\Node\Expr\MethodCall; |
| 10 | +use PhpParser\Node\Expr\New_; |
| 11 | +use PhpParser\Node\Expr\StaticCall; |
| 12 | +use PhpParser\Node\Identifier; |
| 13 | +use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; |
| 14 | +use Rector\Rector\AbstractRector; |
| 15 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 16 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 17 | +/** |
| 18 | + * Related change in PHPUnit 12 https://phpunit.expert/articles/testing-with-and-without-dependencies.html |
| 19 | + * |
| 20 | + * @see \Rector\PHPUnit\Tests\PHPUnit120\Rector\CallLike\CreateStubInCoalesceArgRector\CreateStubInCoalesceArgRectorTest |
| 21 | + */ |
| 22 | +final class CreateStubInCoalesceArgRector extends AbstractRector |
| 23 | +{ |
| 24 | + /** |
| 25 | + * @readonly |
| 26 | + */ |
| 27 | + private TestsNodeAnalyzer $testsNodeAnalyzer; |
| 28 | + public function __construct(TestsNodeAnalyzer $testsNodeAnalyzer) |
| 29 | + { |
| 30 | + $this->testsNodeAnalyzer = $testsNodeAnalyzer; |
| 31 | + } |
| 32 | + public function getRuleDefinition(): RuleDefinition |
| 33 | + { |
| 34 | + return new RuleDefinition('Use createStub() over createMock() when used as argument/array item coalesce ?? fallback', [new CodeSample(<<<'CODE_SAMPLE' |
| 35 | +use PHPUnit\Framework\TestCase; |
| 36 | +
|
| 37 | +final class SomeTest extends TestCase |
| 38 | +{ |
| 39 | + public function test() |
| 40 | + { |
| 41 | + $mockObject = $this->>get('service'); |
| 42 | + $this->someMethod($mockObject ?? $this->createMock(SomeClass::class)); |
| 43 | + } |
| 44 | +
|
| 45 | + private function someMethod($someClass) |
| 46 | + { |
| 47 | + } |
| 48 | +} |
| 49 | +CODE_SAMPLE |
| 50 | +, <<<'CODE_SAMPLE' |
| 51 | +use PHPUnit\Framework\TestCase; |
| 52 | +
|
| 53 | +final class SomeTest extends TestCase |
| 54 | +{ |
| 55 | + public function test() |
| 56 | + { |
| 57 | + $mockObject = $this->>get('service'); |
| 58 | + $this->someMethod($mockObject ?? $this->createStub(SomeClass::class)); |
| 59 | + } |
| 60 | +
|
| 61 | + private function someMethod($someClass) |
| 62 | + { |
| 63 | +} |
| 64 | +CODE_SAMPLE |
| 65 | +)]); |
| 66 | + } |
| 67 | + /** |
| 68 | + * @return array<class-string<Node>> |
| 69 | + */ |
| 70 | + public function getNodeTypes(): array |
| 71 | + { |
| 72 | + return [StaticCall::class, MethodCall::class, New_::class, ArrayItem::class]; |
| 73 | + } |
| 74 | + /** |
| 75 | + * @param MethodCall|StaticCall|New_|ArrayItem $node |
| 76 | + * @return \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall|\PhpParser\Node\Expr\New_|\PhpParser\Node\ArrayItem|null |
| 77 | + */ |
| 78 | + public function refactor(Node $node) |
| 79 | + { |
| 80 | + if (!$this->testsNodeAnalyzer->isInTestClass($node)) { |
| 81 | + return null; |
| 82 | + } |
| 83 | + if ($node instanceof ArrayItem) { |
| 84 | + return $this->refactorArrayItem($node); |
| 85 | + } |
| 86 | + $hasChanges = \false; |
| 87 | + if ($node->isFirstClassCallable()) { |
| 88 | + return null; |
| 89 | + } |
| 90 | + foreach ($node->getArgs() as $arg) { |
| 91 | + if (!$arg->value instanceof Coalesce) { |
| 92 | + continue; |
| 93 | + } |
| 94 | + $coalesce = $arg->value; |
| 95 | + if (!$coalesce->right instanceof MethodCall) { |
| 96 | + continue; |
| 97 | + } |
| 98 | + $methodCall = $coalesce->right; |
| 99 | + if (!$this->isName($methodCall->name, 'createMock')) { |
| 100 | + continue; |
| 101 | + } |
| 102 | + $methodCall->name = new Identifier('createStub'); |
| 103 | + $hasChanges = \true; |
| 104 | + } |
| 105 | + if ($hasChanges) { |
| 106 | + return $node; |
| 107 | + } |
| 108 | + return null; |
| 109 | + } |
| 110 | + private function refactorArrayItem(ArrayItem $arrayItem): ?ArrayItem |
| 111 | + { |
| 112 | + if (!$arrayItem->value instanceof Coalesce) { |
| 113 | + return null; |
| 114 | + } |
| 115 | + $coalesce = $arrayItem->value; |
| 116 | + if (!$coalesce->right instanceof MethodCall) { |
| 117 | + return null; |
| 118 | + } |
| 119 | + $methodCall = $coalesce->right; |
| 120 | + if (!$this->isName($methodCall->name, 'createMock')) { |
| 121 | + return null; |
| 122 | + } |
| 123 | + $methodCall->name = new Identifier('createStub'); |
| 124 | + return $arrayItem; |
| 125 | + } |
| 126 | +} |
0 commit comments