From f8771193e29a1642cd836e274120dbcc5cf12976 Mon Sep 17 00:00:00 2001 From: Simon Podlipsky Date: Tue, 16 Jul 2024 13:11:31 +0200 Subject: [PATCH] refactor(driver): use instanceof checks for correct phpdoc node --- .../DocBlockDriver/DocBlockTypeResolver.php | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/Metadata/Driver/DocBlockDriver/DocBlockTypeResolver.php b/src/Metadata/Driver/DocBlockDriver/DocBlockTypeResolver.php index d5fad5b64..9dbf81453 100644 --- a/src/Metadata/Driver/DocBlockDriver/DocBlockTypeResolver.php +++ b/src/Metadata/Driver/DocBlockDriver/DocBlockTypeResolver.php @@ -7,6 +7,7 @@ use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode; use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode; use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode; +use PHPStan\PhpDocParser\Ast\PhpDoc\TypeAliasTagValueNode; use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode; use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode; use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode; @@ -408,16 +409,20 @@ private function getPhpstanType(\ReflectionClass $declaringClass, string $typeHi $self = $this; foreach ($phpDocNode->children as $node) { - if ($node instanceof PhpDocTagNode && '@phpstan-type' === $node->name) { - $phpstanType = (string) $node->value; - preg_match_all(self::PHPSTAN_ARRAY_SHAPE, $phpstanType, $foundPhpstanArray); - if (isset($foundPhpstanArray[1][0]) && $foundPhpstanArray[1][0] === $typeHint) { + if ( + $node instanceof PhpDocTagNode + && $node->value instanceof TypeAliasTagValueNode + && $node->value->alias === $typeHint + ) { + $phpstanType = $node->value->__toString(); + preg_match(self::PHPSTAN_ARRAY_SHAPE, $phpstanType, $foundPhpstanArray); + if (isset($foundPhpstanArray[0])) { return 'array'; } - preg_match_all(self::PHPSTAN_ARRAY_TYPE, $phpstanType, $foundPhpstanArray); - if (isset($foundPhpstanArray[2][0]) && $foundPhpstanArray[1][0] === $typeHint) { - $types = explode(',', $foundPhpstanArray[2][0]); + preg_match(self::PHPSTAN_ARRAY_TYPE, $phpstanType, $foundPhpstanArray); + if (isset($foundPhpstanArray[0])) { + $types = explode(',', $foundPhpstanArray[2]); return sprintf('array<%s>', implode( ',',