From 0e64280d22faa076843f195910bd9877841d5c9e Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 29 Jun 2026 23:39:32 +0200 Subject: [PATCH] [CodeQuality] Skip AddIntersectionVarToMockObjectPropertyRector when @var already has intersection type --- .../skip_existing_intersection_var.php.inc | 18 ++++++++++++++++++ ...tersectionVarToMockObjectPropertyRector.php | 9 +++++++++ 2 files changed, 27 insertions(+) create mode 100644 rules-tests/CodeQuality/Rector/Class_/AddIntersectionVarToMockObjectPropertyRector/Fixture/skip_existing_intersection_var.php.inc 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;