|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\PHPUnit\PHPUnit120\Rector\Property; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Identifier; |
| 9 | +use PhpParser\Node\IntersectionType; |
| 10 | +use PhpParser\Node\Name; |
| 11 | +use PhpParser\Node\Stmt\Property; |
| 12 | +use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode; |
| 13 | +use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; |
| 14 | +use PHPStan\PhpDocParser\Ast\Type\IntersectionTypeNode; |
| 15 | +use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode; |
| 16 | +use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo; |
| 17 | +use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; |
| 18 | +use Rector\Comments\NodeDocBlock\DocBlockUpdater; |
| 19 | +use Rector\PHPUnit\Enum\PHPUnitClassName; |
| 20 | +use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; |
| 21 | +use Rector\Rector\AbstractRector; |
| 22 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 23 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 24 | + |
| 25 | +/** |
| 26 | + * @see \Rector\PHPUnit\Tests\PHPUnit120\Rector\Property\MockObjectVarToStubRector\MockObjectVarToStubRectorTest |
| 27 | + */ |
| 28 | +final class MockObjectVarToStubRector extends AbstractRector |
| 29 | +{ |
| 30 | + public function __construct( |
| 31 | + private readonly PhpDocInfoFactory $phpDocInfoFactory, |
| 32 | + private readonly DocBlockUpdater $docBlockUpdater, |
| 33 | + private readonly TestsNodeAnalyzer $testsNodeAnalyzer, |
| 34 | + ) { |
| 35 | + } |
| 36 | + |
| 37 | + public function getNodeTypes(): array |
| 38 | + { |
| 39 | + return [Property::class]; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @param Property $node |
| 44 | + */ |
| 45 | + public function refactor(Node $node): ?Property |
| 46 | + { |
| 47 | + // only inside PHPUnit TestCase scope |
| 48 | + if (! $this->testsNodeAnalyzer->isInTestClass($node)) { |
| 49 | + return null; |
| 50 | + } |
| 51 | + |
| 52 | + // only properties already converted to a Stub native type |
| 53 | + if (! $this->isStubNativeType($node->type)) { |
| 54 | + return null; |
| 55 | + } |
| 56 | + |
| 57 | + $phpDocInfo = $this->phpDocInfoFactory->createFromNode($node); |
| 58 | + if (! $phpDocInfo instanceof PhpDocInfo) { |
| 59 | + return null; |
| 60 | + } |
| 61 | + |
| 62 | + $varTagValueNode = $phpDocInfo->getVarTagValueNode(); |
| 63 | + if (! $varTagValueNode instanceof VarTagValueNode) { |
| 64 | + return null; |
| 65 | + } |
| 66 | + |
| 67 | + if (! $this->replaceMockObjectWithStub($varTagValueNode)) { |
| 68 | + return null; |
| 69 | + } |
| 70 | + |
| 71 | + $this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node); |
| 72 | + |
| 73 | + return $node; |
| 74 | + } |
| 75 | + |
| 76 | + public function getRuleDefinition(): RuleDefinition |
| 77 | + { |
| 78 | + return new RuleDefinition( |
| 79 | + 'Update the @var docblock of a property changed to a Stub native type, from MockObject to Stub', |
| 80 | + [ |
| 81 | + new CodeSample( |
| 82 | + <<<'CODE_SAMPLE' |
| 83 | +/** |
| 84 | + * @var FieldModel|MockObject |
| 85 | + */ |
| 86 | +private \PHPUnit\Framework\MockObject\Stub $leadFieldModel; |
| 87 | +CODE_SAMPLE |
| 88 | + , |
| 89 | + <<<'CODE_SAMPLE' |
| 90 | +/** |
| 91 | + * @var FieldModel|Stub |
| 92 | + */ |
| 93 | +private \PHPUnit\Framework\MockObject\Stub $leadFieldModel; |
| 94 | +CODE_SAMPLE |
| 95 | + ), |
| 96 | + ] |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + private function isStubNativeType(?Node $typeNode): bool |
| 101 | + { |
| 102 | + if ($typeNode instanceof IntersectionType) { |
| 103 | + return array_any($typeNode->types, fn (Identifier|Name $innerType): bool => $this->isStubName($innerType)); |
| 104 | + } |
| 105 | + |
| 106 | + return $this->isStubName($typeNode); |
| 107 | + } |
| 108 | + |
| 109 | + private function isStubName(?Node $node): bool |
| 110 | + { |
| 111 | + return $node instanceof Node && $this->getName($node) === PHPUnitClassName::STUB; |
| 112 | + } |
| 113 | + |
| 114 | + private function replaceMockObjectWithStub(VarTagValueNode $varTagValueNode): bool |
| 115 | + { |
| 116 | + $typeNode = $varTagValueNode->type; |
| 117 | + |
| 118 | + // fresh nodes (without original token positions) are re-printed, mutating in place is not |
| 119 | + if ($typeNode instanceof UnionTypeNode || $typeNode instanceof IntersectionTypeNode) { |
| 120 | + $hasChanged = false; |
| 121 | + foreach ($typeNode->types as $key => $innerType) { |
| 122 | + if ($innerType instanceof IdentifierTypeNode && $this->isMockObjectIdentifier($innerType)) { |
| 123 | + $typeNode->types[$key] = new IdentifierTypeNode($this->resolveStubName($innerType->name)); |
| 124 | + $hasChanged = true; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + return $hasChanged; |
| 129 | + } |
| 130 | + |
| 131 | + if ($typeNode instanceof IdentifierTypeNode && $this->isMockObjectIdentifier($typeNode)) { |
| 132 | + $varTagValueNode->type = new IdentifierTypeNode($this->resolveStubName($typeNode->name)); |
| 133 | + return true; |
| 134 | + } |
| 135 | + |
| 136 | + return false; |
| 137 | + } |
| 138 | + |
| 139 | + private function isMockObjectIdentifier(IdentifierTypeNode $identifierTypeNode): bool |
| 140 | + { |
| 141 | + $lastBackslashPosition = strrpos($identifierTypeNode->name, '\\'); |
| 142 | + $shortName = $lastBackslashPosition === false |
| 143 | + ? $identifierTypeNode->name |
| 144 | + : substr($identifierTypeNode->name, $lastBackslashPosition + 1); |
| 145 | + |
| 146 | + return $shortName === 'MockObject'; |
| 147 | + } |
| 148 | + |
| 149 | + private function resolveStubName(string $name): string |
| 150 | + { |
| 151 | + $lastBackslashPosition = strrpos($name, '\\'); |
| 152 | + |
| 153 | + return $lastBackslashPosition === false |
| 154 | + ? 'Stub' |
| 155 | + : substr($name, 0, $lastBackslashPosition + 1) . 'Stub'; |
| 156 | + } |
| 157 | +} |
0 commit comments