Skip to content

[PropertyInfo] Support the list pseudo-type #43916

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

Merged
merged 1 commit into from
Nov 4, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class PhpDocExtractor implements PropertyDescriptionExtractorInterface, Property
public const MUTATOR = 2;

/**
* @var DocBlock[]
* @var array<string, array{DocBlock|null, int|null, string|null}>
*/
private $docBlocks = [];

Expand Down Expand Up @@ -238,6 +238,9 @@ private function filterDocBlockParams(DocBlock $docBlock, string $allowedParam):
$docBlock->getLocation(), $docBlock->isTemplateStart(), $docBlock->isTemplateEnd());
}

/**
* @return array{DocBlock|null, int|null, string|null}
*/
private function getDocBlock(string $class, string $property): array
{
$propertyHash = sprintf('%s::%s', $class, $property);
Expand Down Expand Up @@ -287,13 +290,14 @@ private function getDocBlockFromProperty(string $class, string $property): ?DocB

try {
return $this->docBlockFactory->create($reflectionProperty, $this->createFromReflector($reflector));
} catch (\InvalidArgumentException $e) {
return null;
} catch (\RuntimeException $e) {
} catch (\InvalidArgumentException|\RuntimeException $e) {
return null;
}
}

/**
* @return array{DocBlock, string}|null
*/
private function getDocBlockFromMethod(string $class, string $ucFirstProperty, int $type): ?array
{
$prefixes = self::ACCESSOR === $type ? $this->accessorPrefixes : $this->mutatorPrefixes;
Expand Down Expand Up @@ -333,9 +337,7 @@ private function getDocBlockFromMethod(string $class, string $ucFirstProperty, i

try {
return [$this->docBlockFactory->create($reflectionMethod, $this->createFromReflector($reflector)), $prefix];
} catch (\InvalidArgumentException $e) {
return null;
} catch (\RuntimeException $e) {
} catch (\InvalidArgumentException|\RuntimeException $e) {
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
*/
final class PhpStanExtractor implements PropertyTypeExtractorInterface, ConstructorArgumentTypeExtractorInterface
{
public const PROPERTY = 0;
public const ACCESSOR = 1;
public const MUTATOR = 2;
private const PROPERTY = 0;
private const ACCESSOR = 1;
private const MUTATOR = 2;

/** @var PhpDocParser */
private $phpDocParser;
Expand All @@ -45,12 +45,18 @@ final class PhpStanExtractor implements PropertyTypeExtractorInterface, Construc
/** @var NameScopeFactory */
private $nameScopeFactory;

/** @var array<string, array{PhpDocNode|null, int|null, string|null}> */
private $docBlocks = [];
private $phpStanTypeHelper;
private $mutatorPrefixes;
private $accessorPrefixes;
private $arrayMutatorPrefixes;

/**
* @param list<string>|null $mutatorPrefixes
* @param list<string>|null $accessorPrefixes
* @param list<string>|null $arrayMutatorPrefixes
*/
public function __construct(array $mutatorPrefixes = null, array $accessorPrefixes = null, array $arrayMutatorPrefixes = null)
{
$this->phpStanTypeHelper = new PhpStanTypeHelper();
Expand All @@ -65,7 +71,7 @@ public function __construct(array $mutatorPrefixes = null, array $accessorPrefix

public function getTypes(string $class, string $property, array $context = []): ?array
{
/** @var $docNode PhpDocNode */
/** @var PhpDocNode|null $docNode */
[$docNode, $source, $prefix] = $this->getDocBlock($class, $property);
$nameScope = $this->nameScopeFactory->create($class);
if (null === $docNode) {
Expand Down Expand Up @@ -177,6 +183,9 @@ private function filterDocBlockParams(PhpDocNode $docNode, string $allowedParam)
return $tags[0]->value;
}

/**
* @return array{PhpDocNode|null, int|null, string|null}
*/
private function getDocBlock(string $class, string $property): array
{
$propertyHash = $class.'::'.$property;
Expand Down Expand Up @@ -220,6 +229,9 @@ private function getDocBlockFromProperty(string $class, string $property): ?PhpD
return $phpDocNode;
}

/**
* @return array{PhpDocNode, string}|null
*/
private function getDocBlockFromMethod(string $class, string $ucFirstProperty, int $type): ?array
{
$prefixes = self::ACCESSOR === $type ? $this->accessorPrefixes : $this->mutatorPrefixes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,14 @@ public function typesProvider()
null,
null,
],
[
'listOfStrings',
$this->isPhpDocumentorV5() ? [
new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING)),
] : null,
null,
null,
],
['self', [new Type(Type::BUILTIN_TYPE_OBJECT, false, Dummy::class)], null, null],
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public function typesProvider()
['emptyVar', null],
['arrayWithKeys', [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_STRING), new Type(Type::BUILTIN_TYPE_STRING))]],
['arrayOfMixed', [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_STRING), null)]],
['listOfStrings', [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING))]],
['self', [new Type(Type::BUILTIN_TYPE_OBJECT, false, Dummy::class)]],
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public function testGetProperties()
'arrayWithKeys',
'arrayWithKeysAndComplexValue',
'arrayOfMixed',
'listOfStrings',
'parentAnnotation',
'foo',
'foo2',
Expand Down Expand Up @@ -127,6 +128,7 @@ public function testGetPropertiesWithCustomPrefixes()
'arrayWithKeys',
'arrayWithKeysAndComplexValue',
'arrayOfMixed',
'listOfStrings',
'parentAnnotation',
'foo',
'foo2',
Expand Down Expand Up @@ -175,6 +177,7 @@ public function testGetPropertiesWithNoPrefixes()
'arrayWithKeys',
'arrayWithKeysAndComplexValue',
'arrayOfMixed',
'listOfStrings',
'parentAnnotation',
'foo',
'foo2',
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/PropertyInfo/Tests/Fixtures/Dummy.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ class Dummy extends ParentDummy
*/
public $arrayOfMixed;

/**
* @var list<string>
*/
public $listOfStrings;

/**
* @var parent
*/
Expand Down
15 changes: 13 additions & 2 deletions src/Symfony/Component/PropertyInfo/Util/PhpDocTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\PropertyInfo\Util;

use phpDocumentor\Reflection\PseudoTypes\List_;
use phpDocumentor\Reflection\Type as DocType;
use phpDocumentor\Reflection\Types\Array_;
use phpDocumentor\Reflection\Types\Collection;
Expand All @@ -19,6 +20,10 @@
use phpDocumentor\Reflection\Types\Nullable;
use Symfony\Component\PropertyInfo\Type;

// Workaround for phpdocumentor/type-resolver < 1.6
// We trigger the autoloader here, so we don't need to trigger it inside the loop later.
class_exists(List_::class);

/**
* Transforms a php doc type to a {@link Type} instance.
*
Expand Down Expand Up @@ -91,7 +96,13 @@ private function createType(DocType $type, bool $nullable, string $docType = nul
$docType = $docType ?? (string) $type;

if ($type instanceof Collection) {
[$phpType, $class] = $this->getPhpTypeAndClass((string) $type->getFqsen());
$fqsen = $type->getFqsen();
if ($fqsen && 'list' === $fqsen->getName() && !class_exists(List_::class, false) && !class_exists((string) $fqsen)) {
// Workaround for phpdocumentor/type-resolver < 1.6
return new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true, new Type(Type::BUILTIN_TYPE_INT), $this->getTypes($type->getValueType()));
}

[$phpType, $class] = $this->getPhpTypeAndClass((string) $fqsen);

$key = $this->getTypes($type->getKeyType());
$value = $this->getTypes($type->getValueType());
Expand All @@ -116,7 +127,7 @@ private function createType(DocType $type, bool $nullable, string $docType = nul
return new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true, $collectionKeyType, $collectionValueType);
}

if (str_starts_with($docType, 'array<') && $type instanceof Array_) {
if ((str_starts_with($docType, 'list<') || str_starts_with($docType, 'array<')) && $type instanceof Array_) {
// array<value> is converted to x[] which is handled above
// so it's only necessary to handle array<key, value> here
$collectionKeyType = $this->getTypes($type->getKeyType())[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ private function extractTypes(TypeNode $node, NameScope $nameScope): array
return $this->compressNullableType($types);
}
if ($node instanceof GenericTypeNode) {
$mainTypes = $this->extractTypes($node->type, $nameScope);
[$mainType] = $this->extractTypes($node->type, $nameScope);

$collectionKeyTypes = [];
$collectionKeyTypes = $mainType->getCollectionKeyTypes();
$collectionKeyValues = [];
if (1 === \count($node->genericTypes)) {
foreach ($this->extractTypes($node->genericTypes[0], $nameScope) as $subType) {
Expand All @@ -127,7 +127,7 @@ private function extractTypes(TypeNode $node, NameScope $nameScope): array
}
}

return [new Type($mainTypes[0]->getBuiltinType(), $mainTypes[0]->isNullable(), $mainTypes[0]->getClassName(), true, $collectionKeyTypes, $collectionKeyValues)];
return [new Type($mainType->getBuiltinType(), $mainType->isNullable(), $mainType->getClassName(), true, $collectionKeyTypes, $collectionKeyValues)];
}
if ($node instanceof ArrayShapeNode) {
return [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true)];
Expand Down Expand Up @@ -159,6 +159,8 @@ private function extractTypes(TypeNode $node, NameScope $nameScope): array
switch ($node->name) {
case 'integer':
return [new Type(Type::BUILTIN_TYPE_INT)];
case 'list':
return [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT))];
case 'mixed':
return []; // mixed seems to be ignored in all other extractors
case 'parent':
Expand Down