Skip to content

Commit

Permalink
feat: support imported array shapes (@phpstan-import-type)
Browse files Browse the repository at this point in the history
  • Loading branch information
simPod committed Jul 19, 2024
1 parent 13292e1 commit 05dd486
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
5 changes: 5 additions & 0 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\TypeAliasImportTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\TypeAliasTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode;
Expand Down Expand Up @@ -425,6 +426,10 @@ 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), $typeHint, $reflector);
}
}

Expand Down
18 changes: 18 additions & 0 deletions tests/Fixtures/DocBlockType/Phpstan/PhpstanArrayShapeImported.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace JMS\Serializer\Tests\Fixtures\DocBlockType\Phpstan;

use JMS\Serializer\Tests\Fixtures\DocBlockType\Phpstan\Sub\PhpstanArrayShapeToImport;

/**
* @phpstan-import-type Settings from PhpstanArrayShapeToImport
*/
final class PhpstanArrayShapeImported
{
/**
* @var Settings
*/
public $data;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace JMS\Serializer\Tests\Fixtures\DocBlockType\Phpstan\Sub;

/**
* @phpstan-type Settings array{
* method: string,
* amount: array{type: string, value: string|null}
* }
*/
final class PhpstanArrayShapeToImport
{
/**
* @var Settings
*/
public $data;
}
11 changes: 11 additions & 0 deletions tests/Metadata/Driver/DocBlockDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
use JMS\Serializer\Tests\Fixtures\DocBlockType\Collection\Vehicle;
use JMS\Serializer\Tests\Fixtures\DocBlockType\Phpstan\PhpstanArrayCollectionShape;
use JMS\Serializer\Tests\Fixtures\DocBlockType\Phpstan\PhpstanArrayShape;
use JMS\Serializer\Tests\Fixtures\DocBlockType\Phpstan\PhpstanArrayShapeImported;
use JMS\Serializer\Tests\Fixtures\DocBlockType\Phpstan\PhpstanMultipleArrayShapes;
use JMS\Serializer\Tests\Fixtures\DocBlockType\Phpstan\PhpstanNestedArrayShape;
use JMS\Serializer\Tests\Fixtures\DocBlockType\Phpstan\ProductType;
Expand Down Expand Up @@ -349,6 +350,16 @@ public function testInferTypeForPhpstanArray()
);
}

public function testInferImportedTypeForPhpstanArray()
{
$m = $this->resolve(PhpstanArrayShapeImported::class);

self::assertEquals(
['name' => 'array', 'params' => []],
$m->propertyMetadata['data']->type,
);
}

public function testInferTypeForPhpstanNestedArrayShape()
{
$m = $this->resolve(PhpstanNestedArrayShape::class);
Expand Down

0 comments on commit 05dd486

Please sign in to comment.