|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\PHPUnit\CodeQuality\Rector\Class_; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Expr\ClassConstFetch; |
| 9 | +use PhpParser\Node\Expr\MethodCall; |
| 10 | +use PhpParser\Node\Stmt\Class_; |
| 11 | +use PhpParser\Node\Stmt\ClassMethod; |
| 12 | +use PhpParser\Node\Stmt\Property; |
| 13 | +use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; |
| 14 | +use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; |
| 15 | +use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger; |
| 16 | +use Rector\BetterPhpDocParser\ValueObject\Type\BracketsAwareIntersectionTypeNode; |
| 17 | +use Rector\PHPUnit\CodeQuality\NodeAnalyser\MockObjectPropertyDetector; |
| 18 | +use Rector\PHPUnit\Enum\PHPUnitClassName; |
| 19 | +use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; |
| 20 | +use Rector\Rector\AbstractRector; |
| 21 | +use Rector\ValueObject\MethodName; |
| 22 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 23 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 24 | + |
| 25 | +/** |
| 26 | + * @see \Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\AddIntersectionVarToMockObjectPropertyRector\AddIntersectionVarToMockObjectPropertyRectorTest |
| 27 | + */ |
| 28 | +final class AddIntersectionVarToMockObjectPropertyRector extends AbstractRector |
| 29 | +{ |
| 30 | + public function __construct( |
| 31 | + private readonly TestsNodeAnalyzer $testsNodeAnalyzer, |
| 32 | + private readonly MockObjectPropertyDetector $mockObjectPropertyDetector, |
| 33 | + private readonly PhpDocInfoFactory $phpDocInfoFactory, |
| 34 | + private readonly PhpDocTypeChanger $phpDocTypeChanger, |
| 35 | + ) { |
| 36 | + } |
| 37 | + |
| 38 | + public function getNodeTypes(): array |
| 39 | + { |
| 40 | + return [Class_::class]; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * @param Class_ $node |
| 45 | + */ |
| 46 | + public function refactor(Node $node): ?Class_ |
| 47 | + { |
| 48 | + if (! $this->testsNodeAnalyzer->isInTestClass($node)) { |
| 49 | + return null; |
| 50 | + } |
| 51 | + |
| 52 | + $setUpClassMethod = $node->getMethod(MethodName::SET_UP); |
| 53 | + if (! $setUpClassMethod instanceof ClassMethod) { |
| 54 | + return null; |
| 55 | + } |
| 56 | + |
| 57 | + $propertyNamesToCreateMockMethodCalls = $this->mockObjectPropertyDetector->collectFromClassMethod( |
| 58 | + $setUpClassMethod |
| 59 | + ); |
| 60 | + if ($propertyNamesToCreateMockMethodCalls === []) { |
| 61 | + return null; |
| 62 | + } |
| 63 | + |
| 64 | + $hasChanged = false; |
| 65 | + |
| 66 | + foreach ($propertyNamesToCreateMockMethodCalls as $propertyName => $createMockMethodCall) { |
| 67 | + $property = $node->getProperty($propertyName); |
| 68 | + if (! $property instanceof Property) { |
| 69 | + continue; |
| 70 | + } |
| 71 | + |
| 72 | + // only properties typed as a bare native MockObject |
| 73 | + if (! $this->mockObjectPropertyDetector->detect($property)) { |
| 74 | + continue; |
| 75 | + } |
| 76 | + |
| 77 | + $mockedClass = $this->resolveMockedClass($createMockMethodCall); |
| 78 | + if ($mockedClass === null) { |
| 79 | + continue; |
| 80 | + } |
| 81 | + |
| 82 | + $intersectionTypeNode = new BracketsAwareIntersectionTypeNode([ |
| 83 | + new IdentifierTypeNode('\\' . PHPUnitClassName::MOCK_OBJECT), |
| 84 | + new IdentifierTypeNode('\\' . $mockedClass), |
| 85 | + ]); |
| 86 | + |
| 87 | + $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($property); |
| 88 | + $this->phpDocTypeChanger->changeVarTypeNode($property, $phpDocInfo, $intersectionTypeNode); |
| 89 | + |
| 90 | + $hasChanged = true; |
| 91 | + } |
| 92 | + |
| 93 | + if (! $hasChanged) { |
| 94 | + return null; |
| 95 | + } |
| 96 | + |
| 97 | + return $node; |
| 98 | + } |
| 99 | + |
| 100 | + public function getRuleDefinition(): RuleDefinition |
| 101 | + { |
| 102 | + return new RuleDefinition( |
| 103 | + 'Add a MockObject intersection @var docblock with the mocked class to a native MockObject property', |
| 104 | + [ |
| 105 | + new CodeSample( |
| 106 | + <<<'CODE_SAMPLE' |
| 107 | +use PHPUnit\Framework\TestCase; |
| 108 | +
|
| 109 | +final class SomeTest extends TestCase |
| 110 | +{ |
| 111 | + private \PHPUnit\Framework\MockObject\MockObject $someServiceMock; |
| 112 | +
|
| 113 | + protected function setUp(): void |
| 114 | + { |
| 115 | + $this->someServiceMock = $this->createMock(SomeService::class); |
| 116 | + } |
| 117 | +} |
| 118 | +CODE_SAMPLE |
| 119 | + , |
| 120 | + <<<'CODE_SAMPLE' |
| 121 | +use PHPUnit\Framework\TestCase; |
| 122 | +
|
| 123 | +final class SomeTest extends TestCase |
| 124 | +{ |
| 125 | + /** |
| 126 | + * @var \PHPUnit\Framework\MockObject\MockObject&\SomeService |
| 127 | + */ |
| 128 | + private \PHPUnit\Framework\MockObject\MockObject $someServiceMock; |
| 129 | +
|
| 130 | + protected function setUp(): void |
| 131 | + { |
| 132 | + $this->someServiceMock = $this->createMock(SomeService::class); |
| 133 | + } |
| 134 | +} |
| 135 | +CODE_SAMPLE |
| 136 | + ), |
| 137 | + ] |
| 138 | + ); |
| 139 | + } |
| 140 | + |
| 141 | + private function resolveMockedClass(MethodCall $methodCall): ?string |
| 142 | + { |
| 143 | + $firstArg = $methodCall->getArgs()[0] ?? null; |
| 144 | + if ($firstArg === null) { |
| 145 | + return null; |
| 146 | + } |
| 147 | + |
| 148 | + if (! $firstArg->value instanceof ClassConstFetch) { |
| 149 | + return null; |
| 150 | + } |
| 151 | + |
| 152 | + $className = $this->getName($firstArg->value->class); |
| 153 | + if (! is_string($className)) { |
| 154 | + return null; |
| 155 | + } |
| 156 | + |
| 157 | + return $className; |
| 158 | + } |
| 159 | +} |
0 commit comments