|
| 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\Expr\MethodCall; |
| 9 | +use PhpParser\Node\Expr\New_; |
| 10 | +use PhpParser\Node\Expr\StaticCall; |
| 11 | +use PhpParser\Node\Identifier; |
| 12 | +use Rector\PHPStan\ScopeFetcher; |
| 13 | +use Rector\PHPUnit\Enum\PHPUnitClassName; |
| 14 | +use Rector\Rector\AbstractRector; |
| 15 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 16 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 17 | + |
| 18 | +/** |
| 19 | + * Related change in PHPUnit 12 https://phpunit.expert/articles/testing-with-and-without-dependencies.html |
| 20 | + * |
| 21 | + * @see \Rector\PHPUnit\Tests\PHPUnit120\Rector\CallLike\CreateStubOverCreateMockArgRector\CreateStubOverCreateMockArgRectorTest |
| 22 | + */ |
| 23 | +final class CreateStubOverCreateMockArgRector extends AbstractRector |
| 24 | +{ |
| 25 | + public function getRuleDefinition(): RuleDefinition |
| 26 | + { |
| 27 | + return new RuleDefinition( |
| 28 | + 'Use createStub() over createMock() when used as argument and does not add any mock requirements', |
| 29 | + [ |
| 30 | + new CodeSample( |
| 31 | + <<<'CODE_SAMPLE' |
| 32 | +use PHPUnit\Framework\TestCase; |
| 33 | +final class SomeTest extends TestCase |
| 34 | +{ |
| 35 | + public function test() |
| 36 | + { |
| 37 | + $this->someMethod($this->createMock(SomeClass::class)); |
| 38 | + } |
| 39 | +
|
| 40 | + private function someMethod($someClass) |
| 41 | + { |
| 42 | + } |
| 43 | +} |
| 44 | +CODE_SAMPLE |
| 45 | + , |
| 46 | + <<<'CODE_SAMPLE' |
| 47 | +use PHPUnit\Framework\TestCase; |
| 48 | +
|
| 49 | +final class SomeTest extends TestCase |
| 50 | +{ |
| 51 | + public function test() |
| 52 | + { |
| 53 | + $this->someMethod($this->createStub(SomeClass::class)); |
| 54 | + } |
| 55 | +
|
| 56 | + private function someMethod($someClass) |
| 57 | + { |
| 58 | + } |
| 59 | +} |
| 60 | +CODE_SAMPLE |
| 61 | + ), |
| 62 | + |
| 63 | + ] |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @return array<class-string<Node>> |
| 69 | + */ |
| 70 | + public function getNodeTypes(): array |
| 71 | + { |
| 72 | + return [StaticCall::class, MethodCall::class, New_::class]; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @param MethodCall|StaticCall|New_ $node |
| 77 | + */ |
| 78 | + public function refactor(Node $node): MethodCall|StaticCall|New_|null |
| 79 | + { |
| 80 | + $scope = ScopeFetcher::fetch($node); |
| 81 | + if (! $scope->isInClass()) { |
| 82 | + return null; |
| 83 | + } |
| 84 | + |
| 85 | + $classReflection = $scope->getClassReflection(); |
| 86 | + if (! $classReflection->is(PHPUnitClassName::TEST_CASE)) { |
| 87 | + return null; |
| 88 | + } |
| 89 | + |
| 90 | + $hasChanges = false; |
| 91 | + |
| 92 | + foreach ($node->getArgs() as $arg) { |
| 93 | + if (! $arg->value instanceof MethodCall) { |
| 94 | + continue; |
| 95 | + } |
| 96 | + |
| 97 | + $methodCall = $arg->value; |
| 98 | + if (! $this->isName($methodCall->name, 'createMock')) { |
| 99 | + continue; |
| 100 | + } |
| 101 | + |
| 102 | + $methodCall->name = new Identifier('createStub'); |
| 103 | + $hasChanges = true; |
| 104 | + } |
| 105 | + |
| 106 | + if ($hasChanges) { |
| 107 | + return $node; |
| 108 | + } |
| 109 | + |
| 110 | + return null; |
| 111 | + } |
| 112 | +} |
0 commit comments