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