Skip to content

Commit

Permalink
refactor(driver): use instanceof checks for correct phpdoc node
Browse files Browse the repository at this point in the history
  • Loading branch information
simPod committed Jul 22, 2024
1 parent d0a24b7 commit f877119
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/Metadata/Driver/DocBlockDriver/DocBlockTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
',',
Expand Down

0 comments on commit f877119

Please sign in to comment.