varTagRemover = $varTagRemover; $this->staticTypeMapper = $staticTypeMapper; $this->phpDocInfoFactory = $phpDocInfoFactory; } public function getRuleDefinition() : RuleDefinition { return new RuleDefinition('Remove @var annotation for PHPUnit\\Framework\\MockObject\\MockObject combined with native object type', [new CodeSample(<<<'CODE_SAMPLE' use PHPUnit\Framework\TestCase; use PHPUnit\Framework\MockObject\MockObject; final class SomeTest extends TestCase { /** * @var SomeClass|MockObject */ private SomeClass $someProperty; } CODE_SAMPLE , <<<'CODE_SAMPLE' use PHPUnit\Framework\TestCase; use PHPUnit\Framework\MockObject\MockObject; final class SomeTest extends TestCase { private SomeClass $someProperty; } CODE_SAMPLE )]); } public function getNodeTypes() : array { return [Class_::class]; } /** * @param Class_ $node */ public function refactor(Node $node) : ?Node { if (!$this->isObjectType($node, new ObjectType(ClassName::TEST_CASE_CLASS))) { return null; } $hasChanged = \false; foreach ($node->getProperties() as $property) { // not yet typed if (!$property->type instanceof Node) { continue; } if (\count($property->props) !== 1) { continue; } if (!$property->type instanceof FullyQualified) { continue; } if ($this->isObjectType($property->type, new ObjectType(self::MOCK_OBJECT_CLASS))) { continue; } $propertyDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($property); if (!$this->isVarTagUnionTypeMockObject($propertyDocInfo, $property)) { continue; } // clear var docblock if ($this->varTagRemover->removeVarTag($property)) { $hasChanged = \true; } } if (!$hasChanged) { return null; } return $node; } public function provideMinPhpVersion() : int { return PhpVersionFeature::TYPED_PROPERTIES; } private function isVarTagUnionTypeMockObject(PhpDocInfo $phpDocInfo, Property $property) : bool { $varTagValueNode = $phpDocInfo->getVarTagValueNode(); if (!$varTagValueNode instanceof VarTagValueNode) { return \false; } if (!$varTagValueNode->type instanceof UnionTypeNode) { return \false; } $varTagType = $this->staticTypeMapper->mapPHPStanPhpDocTypeNodeToPHPStanType($varTagValueNode->type, $property); if (!$varTagType instanceof UnionType) { return \false; } foreach ($varTagType->getTypes() as $unionedType) { if ($unionedType->isSuperTypeOf(new ObjectType(self::MOCK_OBJECT_CLASS))->yes()) { return \true; } } return \false; } }