From 3420d2a3737adb427ca1827391d02a1f2ce505da Mon Sep 17 00:00:00 2001 From: Simon Podlipsky Date: Tue, 16 Jul 2024 13:34:55 +0200 Subject: [PATCH] feat: support imported array shapes (`@phpstan-import-type`) --- .../DocBlockDriver/DocBlockTypeResolver.php | 9 +++++++ .../Phpstan/PhpstanArrayShapeImported.php | 24 +++++++++++++++++++ .../Phpstan/Sub/PhpstanArrayShapeToImport.php | 19 +++++++++++++++ tests/Metadata/Driver/DocBlockDriverTest.php | 16 +++++++++++++ 4 files changed, 68 insertions(+) create mode 100644 tests/Fixtures/DocBlockType/Phpstan/PhpstanArrayShapeImported.php create mode 100644 tests/Fixtures/DocBlockType/Phpstan/Sub/PhpstanArrayShapeToImport.php diff --git a/src/Metadata/Driver/DocBlockDriver/DocBlockTypeResolver.php b/src/Metadata/Driver/DocBlockDriver/DocBlockTypeResolver.php index 056269828..794237c5e 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\TypeAliasImportTagValueNode; use PHPStan\PhpDocParser\Ast\PhpDoc\TypeAliasTagValueNode; use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode; use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode; @@ -425,6 +426,14 @@ private function getPhpstanType(\ReflectionClass $declaringClass, string $typeHi array_map(static fn (string $type) => $self->resolveType(trim($type), $reflector), $types), )); } + } elseif ($node instanceof PhpDocTagNode && $node->value instanceof TypeAliasImportTagValueNode) { + $importedFromFqn = $this->resolveType($node->value->importedFrom->name, $reflector); + + return $this->getPhpstanType( + new \ReflectionClass($importedFromFqn), + $node->value->importedAlias, + $reflector, + ); } } diff --git a/tests/Fixtures/DocBlockType/Phpstan/PhpstanArrayShapeImported.php b/tests/Fixtures/DocBlockType/Phpstan/PhpstanArrayShapeImported.php new file mode 100644 index 000000000..d0df2cfa4 --- /dev/null +++ b/tests/Fixtures/DocBlockType/Phpstan/PhpstanArrayShapeImported.php @@ -0,0 +1,24 @@ +resolve(PhpstanArrayShapeImported::class); + + self::assertEquals( + ['name' => 'array', 'params' => []], + $m->propertyMetadata['data']->type, + ); + + self::assertEquals( + ['name' => 'array', 'params' => []], + $m->propertyMetadata['dataAliased']->type, + ); + } + public function testInferTypeForPhpstanNestedArrayShape() { $m = $this->resolve(PhpstanNestedArrayShape::class);