diff --git a/packages/BetterPhpDocParser/src/PhpDocInfo/PhpDocInfo.php b/packages/BetterPhpDocParser/src/PhpDocInfo/PhpDocInfo.php index 7da5920175c..2c5e3935514 100644 --- a/packages/BetterPhpDocParser/src/PhpDocInfo/PhpDocInfo.php +++ b/packages/BetterPhpDocParser/src/PhpDocInfo/PhpDocInfo.php @@ -15,6 +15,7 @@ use Rector\BetterPhpDocParser\Attributes\Ast\PhpDoc\AttributeAwarePhpDocNode; use Rector\BetterPhpDocParser\Attributes\Ast\PhpDoc\AttributeAwareReturnTagValueNode; use Rector\BetterPhpDocParser\Attributes\Ast\PhpDoc\AttributeAwareVarTagValueNode; use Rector\BetterPhpDocParser\Attributes\Contract\Ast\AttributeAwareNodeInterface; +use Rector\Exception\ShouldNotHappenException; use Rector\NodeTypeResolver\StaticTypeMapper; /** @@ -189,6 +190,8 @@ final class PhpDocInfo public function getByType(string $type): ?PhpDocTagValueNode { + $this->ensureTypeIsTagValueNode($type, __METHOD__); + foreach ($this->phpDocNode->children as $phpDocChildNode) { if ($phpDocChildNode instanceof PhpDocTagNode) { if (is_a($phpDocChildNode->value, $type, true)) { @@ -213,4 +216,18 @@ final class PhpDocInfo return null; } + + private function ensureTypeIsTagValueNode(string $type, string $location): void + { + if (is_a($type, PhpDocTagValueNode::class, true)) { + return; + } + + throw new ShouldNotHappenException(sprintf( + 'Type "%s" passed to "%s()" method must be child of "%s"', + $type, + $location, + PhpDocTagValueNode::class + )); + } } diff --git a/packages/DoctrinePhpDocParser/src/Contract/Ast/PhpDoc/DoctrineRelationTagValueNodeInterface.php b/packages/DoctrinePhpDocParser/src/Contract/Ast/PhpDoc/DoctrineRelationTagValueNodeInterface.php index 3047b47a8f4..2a4ce3b1d71 100644 --- a/packages/DoctrinePhpDocParser/src/Contract/Ast/PhpDoc/DoctrineRelationTagValueNodeInterface.php +++ b/packages/DoctrinePhpDocParser/src/Contract/Ast/PhpDoc/DoctrineRelationTagValueNodeInterface.php @@ -2,7 +2,9 @@ namespace Rector\DoctrinePhpDocParser\Contract\Ast\PhpDoc; -interface DoctrineRelationTagValueNodeInterface +use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagValueNode; + +interface DoctrineRelationTagValueNodeInterface extends PhpDocTagValueNode { public function getTargetEntity(): ?string; diff --git a/packages/TypeDeclaration/src/Rector/Property/PropertyTypeDeclarationRector.php b/packages/TypeDeclaration/src/Rector/Property/PropertyTypeDeclarationRector.php index 5fecf195f97..aaa93accf5a 100644 --- a/packages/TypeDeclaration/src/Rector/Property/PropertyTypeDeclarationRector.php +++ b/packages/TypeDeclaration/src/Rector/Property/PropertyTypeDeclarationRector.php @@ -57,7 +57,9 @@ final class PropertyTypeDeclarationRector extends AbstractRector return null; } - if ($this->docBlockManipulator->hasTag($node, '@var')) { + // is already set + $currentVarType = $this->docBlockManipulator->getVarType($node); + if (! $currentVarType instanceof MixedType) { return null; } diff --git a/src/Rector/Property/InjectAnnotationClassRector.php b/src/Rector/Property/InjectAnnotationClassRector.php index 3a306bcdf5c..5a7d3210ec1 100644 --- a/src/Rector/Property/InjectAnnotationClassRector.php +++ b/src/Rector/Property/InjectAnnotationClassRector.php @@ -165,7 +165,7 @@ CODE_SAMPLE return null; } - if (! $this->docBlockManipulator->hasTag($property, 'var')) { + if (! $this->docBlockManipulator->getVarType()) { $this->docBlockManipulator->changeVarTag($property, $type); }