|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\PHPUnit\CodeQuality\Rector\ClassMethod; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Stmt\ClassMethod; |
| 9 | +use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode; |
| 10 | +use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; |
| 11 | +use PHPStan\PhpDocParser\Ast\Type\TypeNode; |
| 12 | +use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode; |
| 13 | +use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo; |
| 14 | +use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; |
| 15 | +use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger; |
| 16 | +use Rector\BetterPhpDocParser\ValueObject\Type\BracketsAwareIntersectionTypeNode; |
| 17 | +use Rector\PHPUnit\Enum\PHPUnitClassName; |
| 18 | +use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; |
| 19 | +use Rector\Rector\AbstractRector; |
| 20 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 21 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 22 | + |
| 23 | +/** |
| 24 | + * @see \Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\ChangeMockObjectReturnUnionToIntersectionRector\ChangeMockObjectReturnUnionToIntersectionRectorTest |
| 25 | + */ |
| 26 | +final class ChangeMockObjectReturnUnionToIntersectionRector extends AbstractRector |
| 27 | +{ |
| 28 | + public function __construct( |
| 29 | + private readonly TestsNodeAnalyzer $testsNodeAnalyzer, |
| 30 | + private readonly PhpDocInfoFactory $phpDocInfoFactory, |
| 31 | + private readonly PhpDocTypeChanger $phpDocTypeChanger, |
| 32 | + ) { |
| 33 | + } |
| 34 | + |
| 35 | + public function getNodeTypes(): array |
| 36 | + { |
| 37 | + return [ClassMethod::class]; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * @param ClassMethod $node |
| 42 | + */ |
| 43 | + public function refactor(Node $node): ?ClassMethod |
| 44 | + { |
| 45 | + if (! $this->testsNodeAnalyzer->isInTestClass($node)) { |
| 46 | + return null; |
| 47 | + } |
| 48 | + |
| 49 | + $phpDocInfo = $this->phpDocInfoFactory->createFromNode($node); |
| 50 | + if (! $phpDocInfo instanceof PhpDocInfo) { |
| 51 | + return null; |
| 52 | + } |
| 53 | + |
| 54 | + $returnTagValueNode = $phpDocInfo->getReturnTagValue(); |
| 55 | + if (! $returnTagValueNode instanceof ReturnTagValueNode) { |
| 56 | + return null; |
| 57 | + } |
| 58 | + |
| 59 | + $returnTypeNode = $returnTagValueNode->type; |
| 60 | + if (! $returnTypeNode instanceof UnionTypeNode) { |
| 61 | + return null; |
| 62 | + } |
| 63 | + |
| 64 | + // must contain a MockObject or Stub member to be a mock union |
| 65 | + if (! $this->hasMockObjectOrStubType($returnTypeNode)) { |
| 66 | + return null; |
| 67 | + } |
| 68 | + |
| 69 | + $bracketsAwareIntersectionTypeNode = new BracketsAwareIntersectionTypeNode($returnTypeNode->types); |
| 70 | + $this->phpDocTypeChanger->changeReturnTypeNode($node, $phpDocInfo, $bracketsAwareIntersectionTypeNode); |
| 71 | + |
| 72 | + return $node; |
| 73 | + } |
| 74 | + |
| 75 | + public function getRuleDefinition(): RuleDefinition |
| 76 | + { |
| 77 | + return new RuleDefinition( |
| 78 | + 'Change a MockObject @return union docblock to an intersection type', |
| 79 | + [ |
| 80 | + new CodeSample( |
| 81 | + <<<'CODE_SAMPLE' |
| 82 | +use PHPUnit\Framework\TestCase; |
| 83 | +
|
| 84 | +final class SomeTest extends TestCase |
| 85 | +{ |
| 86 | + /** |
| 87 | + * @return Event|\PHPUnit\Framework\MockObject\MockObject |
| 88 | + */ |
| 89 | + private function createEvent(): \PHPUnit\Framework\MockObject\MockObject |
| 90 | + { |
| 91 | + return $this->createMock(Event::class); |
| 92 | + } |
| 93 | +} |
| 94 | +CODE_SAMPLE |
| 95 | + , |
| 96 | + <<<'CODE_SAMPLE' |
| 97 | +use PHPUnit\Framework\TestCase; |
| 98 | +
|
| 99 | +final class SomeTest extends TestCase |
| 100 | +{ |
| 101 | + /** |
| 102 | + * @return Event&\PHPUnit\Framework\MockObject\MockObject |
| 103 | + */ |
| 104 | + private function createEvent(): \PHPUnit\Framework\MockObject\MockObject |
| 105 | + { |
| 106 | + return $this->createMock(Event::class); |
| 107 | + } |
| 108 | +} |
| 109 | +CODE_SAMPLE |
| 110 | + ), |
| 111 | + ] |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + private function hasMockObjectOrStubType(UnionTypeNode $unionTypeNode): bool |
| 116 | + { |
| 117 | + return array_any($unionTypeNode->types, fn (TypeNode $typeNode): bool => $this->isMockObjectOrStubType( |
| 118 | + $typeNode |
| 119 | + )); |
| 120 | + } |
| 121 | + |
| 122 | + private function isMockObjectOrStubType(TypeNode $typeNode): bool |
| 123 | + { |
| 124 | + if (! $typeNode instanceof IdentifierTypeNode) { |
| 125 | + return false; |
| 126 | + } |
| 127 | + |
| 128 | + $typeName = ltrim($typeNode->name, '\\'); |
| 129 | + |
| 130 | + return in_array( |
| 131 | + $typeName, |
| 132 | + [PHPUnitClassName::MOCK_OBJECT, 'MockObject', PHPUnitClassName::STUB, 'Stub'], |
| 133 | + true |
| 134 | + ); |
| 135 | + } |
| 136 | +} |
0 commit comments