Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support imported array shapes (@phpstan-import-type) #1551

Merged
merged 2 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions src/Metadata/Driver/DocBlockDriver/DocBlockTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
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;
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
Expand Down Expand Up @@ -265,7 +267,7 @@ private function expandClassNameUsingUseStatements(string $typeHint, \Reflection
}

if ($declaringClass->getDocComment()) {
$phpstanArrayType = $this->getPhpstanType($declaringClass, $typeHint, $reflector);
$phpstanArrayType = $this->getPhpstanArrayType($declaringClass, $typeHint, $reflector);

if ($phpstanArrayType) {
return $phpstanArrayType;
Expand Down Expand Up @@ -401,29 +403,41 @@ private function resolveTypeFromDocblock($reflector): array
/**
* @param \ReflectionMethod|\ReflectionProperty $reflector
*/
private function getPhpstanType(\ReflectionClass $declaringClass, string $typeHint, $reflector): ?string
private function getPhpstanArrayType(\ReflectionClass $declaringClass, string $typeHint, $reflector): ?string
{
$tokens = $this->lexer->tokenize($declaringClass->getDocComment());
$phpDocNode = $this->phpDocParser->parse(new TokenIterator($tokens));
$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(
',',
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->getPhpstanArrayType(
new \ReflectionClass($importedFromFqn),
$node->value->importedAlias,
$reflector,
);
}
}

Expand Down
24 changes: 24 additions & 0 deletions tests/Fixtures/DocBlockType/Phpstan/PhpstanArrayShapeImported.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?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
* @phpstan-import-type Settings from PhpstanArrayShapeToImport as AliasedSettings
*/
final class PhpstanArrayShapeImported
{
/**
* @var Settings
*/
public $data;

/**
* @var AliasedSettings
*/
public $dataAliased;
}
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;
}
16 changes: 16 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,21 @@ public function testInferTypeForPhpstanArray()
);
}

public function testInferImportedTypeForPhpstanArray()
{
$m = $this->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);
Expand Down
Loading