|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\PHPUnit\CodeQuality\Rector\CallLike; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Arg; |
| 9 | +use PhpParser\Node\ArrayItem; |
| 10 | +use PhpParser\Node\Expr; |
| 11 | +use PhpParser\Node\Expr\MethodCall; |
| 12 | +use PhpParser\Node\Expr\New_; |
| 13 | +use PhpParser\Node\Expr\StaticCall; |
| 14 | +use PhpParser\Node\Name\FullyQualified; |
| 15 | +use Rector\PhpParser\Node\Value\ValueResolver; |
| 16 | +use Rector\PHPStan\ScopeFetcher; |
| 17 | +use Rector\PHPUnit\Enum\PHPUnitClassName; |
| 18 | +use Rector\Rector\AbstractRector; |
| 19 | +use Rector\Symfony\Enum\SymfonyClass; |
| 20 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 21 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 22 | + |
| 23 | +/** |
| 24 | + * @see \Rector\PHPUnit\Tests\CodeQuality\Rector\CallLike\DirectInstanceOverMockArgRector\DirectInstanceOverMockArgRectorTest |
| 25 | + */ |
| 26 | +final class DirectInstanceOverMockArgRector extends AbstractRector |
| 27 | +{ |
| 28 | + public function __construct( |
| 29 | + private readonly ValueResolver $valueResolver |
| 30 | + ) { |
| 31 | + } |
| 32 | + |
| 33 | + public function getRuleDefinition(): RuleDefinition |
| 34 | + { |
| 35 | + return new RuleDefinition( |
| 36 | + 'Use direct object instance over mock for specific objects in arg of PHPUnit tests', |
| 37 | + [ |
| 38 | + new CodeSample( |
| 39 | + <<<'CODE_SAMPLE' |
| 40 | +use PHPUnit\Framework\TestCase; |
| 41 | +use Symfony\Component\HttpFoundation\Request; |
| 42 | +
|
| 43 | +final class SomeTest extends TestCase |
| 44 | +{ |
| 45 | + public function test() |
| 46 | + { |
| 47 | + $this->someMethod($this->createMock(Request::class)); |
| 48 | + } |
| 49 | +
|
| 50 | + private function someMethod($someClass) |
| 51 | + { |
| 52 | + } |
| 53 | +} |
| 54 | +CODE_SAMPLE |
| 55 | + , |
| 56 | + <<<'CODE_SAMPLE' |
| 57 | +use PHPUnit\Framework\TestCase; |
| 58 | +use Symfony\Component\HttpFoundation\Request; |
| 59 | +
|
| 60 | +final class SomeTest extends TestCase |
| 61 | +{ |
| 62 | + public function test() |
| 63 | + { |
| 64 | + $this->someMethod(new Request()); |
| 65 | + } |
| 66 | +
|
| 67 | + private function someMethod($someClass) |
| 68 | + { |
| 69 | + } |
| 70 | +} |
| 71 | +CODE_SAMPLE |
| 72 | + ), |
| 73 | + |
| 74 | + ] |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @return array<class-string<Node>> |
| 80 | + */ |
| 81 | + public function getNodeTypes(): array |
| 82 | + { |
| 83 | + return [StaticCall::class, MethodCall::class, New_::class, ArrayItem::class]; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @param MethodCall|StaticCall|New_|ArrayItem $node |
| 88 | + */ |
| 89 | + public function refactor(Node $node): MethodCall|StaticCall|New_|ArrayItem|null |
| 90 | + { |
| 91 | + $scope = ScopeFetcher::fetch($node); |
| 92 | + if (! $scope->isInClass()) { |
| 93 | + return null; |
| 94 | + } |
| 95 | + |
| 96 | + $classReflection = $scope->getClassReflection(); |
| 97 | + if (! $classReflection->is(PHPUnitClassName::TEST_CASE)) { |
| 98 | + return null; |
| 99 | + } |
| 100 | + |
| 101 | + $hasChanged = false; |
| 102 | + |
| 103 | + if ($node instanceof ArrayItem) { |
| 104 | + |
| 105 | + return $this->refactorArrayItem($node); |
| 106 | + } |
| 107 | + |
| 108 | + foreach ($node->getArgs() as $arg) { |
| 109 | + $firstArg = $this->matchCreateMockMethodCallArg($arg->value); |
| 110 | + if (! $firstArg instanceof Arg) { |
| 111 | + continue; |
| 112 | + } |
| 113 | + |
| 114 | + $className = $this->valueResolver->getValue($firstArg->value); |
| 115 | + if (! in_array($className, [SymfonyClass::REQUEST, SymfonyClass::REQUEST_STACK])) { |
| 116 | + continue; |
| 117 | + } |
| 118 | + |
| 119 | + $arg->value = new New_(new FullyQualified($className)); |
| 120 | + $hasChanged = true; |
| 121 | + } |
| 122 | + |
| 123 | + if ($hasChanged) { |
| 124 | + return $node; |
| 125 | + } |
| 126 | + |
| 127 | + return null; |
| 128 | + } |
| 129 | + |
| 130 | + private function matchCreateMockMethodCallArg(Expr $expr): ?Arg |
| 131 | + { |
| 132 | + if (! $expr instanceof MethodCall) { |
| 133 | + return null; |
| 134 | + } |
| 135 | + |
| 136 | + $methodCall = $expr; |
| 137 | + if (! $this->isName($methodCall->name, 'createMock')) { |
| 138 | + return null; |
| 139 | + } |
| 140 | + |
| 141 | + if ($methodCall->isFirstClassCallable()) { |
| 142 | + return null; |
| 143 | + } |
| 144 | + |
| 145 | + return $methodCall->getArgs()[0]; |
| 146 | + } |
| 147 | + |
| 148 | + private function refactorArrayItem(ArrayItem $arrayItem): ?ArrayItem |
| 149 | + { |
| 150 | + $mockedCallArg = $this->matchCreateMockMethodCallArg($arrayItem->value); |
| 151 | + if (! $mockedCallArg instanceof Arg) { |
| 152 | + return null; |
| 153 | + } |
| 154 | + |
| 155 | + $className = $this->valueResolver->getValue($mockedCallArg->value); |
| 156 | + |
| 157 | + if (! in_array($className, [SymfonyClass::REQUEST, SymfonyClass::REQUEST_STACK])) { |
| 158 | + return null; |
| 159 | + } |
| 160 | + |
| 161 | + $arrayItem->value = new New_(new FullyQualified($className)); |
| 162 | + |
| 163 | + return $arrayItem; |
| 164 | + } |
| 165 | +} |
0 commit comments