diff --git a/rules-tests/CodeQuality/Rector/Class_/AddIntersectionVarToMockObjectPropertyRector/Fixture/skip_existing_intersection_var.php.inc b/rules-tests/CodeQuality/Rector/Class_/AddIntersectionVarToMockObjectPropertyRector/Fixture/skip_existing_intersection_var.php.inc new file mode 100644 index 00000000..1c221ad3 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/Class_/AddIntersectionVarToMockObjectPropertyRector/Fixture/skip_existing_intersection_var.php.inc @@ -0,0 +1,18 @@ +someServiceMock = $this->createMock(\stdClass::class); + } +} diff --git a/rules/CodeQuality/Rector/Class_/AddIntersectionVarToMockObjectPropertyRector.php b/rules/CodeQuality/Rector/Class_/AddIntersectionVarToMockObjectPropertyRector.php index bc7864a0..5745ddf0 100644 --- a/rules/CodeQuality/Rector/Class_/AddIntersectionVarToMockObjectPropertyRector.php +++ b/rules/CodeQuality/Rector/Class_/AddIntersectionVarToMockObjectPropertyRector.php @@ -10,7 +10,9 @@ use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Property; +use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode; use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; +use PHPStan\PhpDocParser\Ast\Type\IntersectionTypeNode; use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger; use Rector\BetterPhpDocParser\ValueObject\Type\BracketsAwareIntersectionTypeNode; @@ -85,6 +87,13 @@ public function refactor(Node $node): ?Class_ ]); $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($property); + + // already has an intersection @var, skip + $varTagValueNode = $phpDocInfo->getVarTagValueNode(); + if ($varTagValueNode instanceof VarTagValueNode && $varTagValueNode->type instanceof IntersectionTypeNode) { + continue; + } + $this->phpDocTypeChanger->changeVarTypeNode($property, $phpDocInfo, $intersectionTypeNode); $hasChanged = true;