From d0bbc495f11ab99a1e2cbf04699331b88b3c1293 Mon Sep 17 00:00:00 2001 From: Mathias Arlaud Date: Thu, 23 Nov 2023 08:57:08 +0100 Subject: [PATCH] [PropertyAccessor] Fix unexpected collection when generics --- Tests/Extractor/PhpDocExtractorTest.php | 5 +++++ Tests/Extractor/PhpStanExtractorTest.php | 7 ++++++- Tests/Extractor/ReflectionExtractorTest.php | 3 +++ Tests/Fixtures/Dummy.php | 5 +++++ Tests/Fixtures/DummyUnionType.php | 2 +- Util/PhpDocTypeHelper.php | 13 ++++++++++--- Util/PhpStanTypeHelper.php | 9 ++++++++- 7 files changed, 38 insertions(+), 6 deletions(-) diff --git a/Tests/Extractor/PhpDocExtractorTest.php b/Tests/Extractor/PhpDocExtractorTest.php index f71664d..57869e4 100644 --- a/Tests/Extractor/PhpDocExtractorTest.php +++ b/Tests/Extractor/PhpDocExtractorTest.php @@ -428,6 +428,11 @@ public function testUnknownPseudoType() $this->assertEquals([new Type(Type::BUILTIN_TYPE_OBJECT, false, 'scalar')], $this->extractor->getTypes(PseudoTypeDummy::class, 'unknownPseudoType')); } + public function testGenericInterface() + { + $this->assertNull($this->extractor->getTypes(Dummy::class, 'genericInterface')); + } + protected static function isPhpDocumentorV5() { if (class_exists(InvalidTag::class)) { diff --git a/Tests/Extractor/PhpStanExtractorTest.php b/Tests/Extractor/PhpStanExtractorTest.php index d8fa9b9..51e28ed 100644 --- a/Tests/Extractor/PhpStanExtractorTest.php +++ b/Tests/Extractor/PhpStanExtractorTest.php @@ -388,7 +388,7 @@ public static function unionTypesProvider(): array ['b', [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, [new Type(Type::BUILTIN_TYPE_INT)], [new Type(Type::BUILTIN_TYPE_STRING), new Type(Type::BUILTIN_TYPE_INT)])]], ['c', [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, [], [new Type(Type::BUILTIN_TYPE_STRING), new Type(Type::BUILTIN_TYPE_INT)])]], ['d', [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, [new Type(Type::BUILTIN_TYPE_STRING), new Type(Type::BUILTIN_TYPE_INT)], [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, [], [new Type(Type::BUILTIN_TYPE_STRING)])])]], - ['e', [new Type(Type::BUILTIN_TYPE_OBJECT, true, Dummy::class, true, [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, [], [new Type(Type::BUILTIN_TYPE_STRING)])], [new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, [new Type(Type::BUILTIN_TYPE_INT)], [new Type(Type::BUILTIN_TYPE_STRING, false, null, true, [], [new Type(Type::BUILTIN_TYPE_OBJECT, false, DefaultValue::class)])])]), new Type(Type::BUILTIN_TYPE_OBJECT, false, ParentDummy::class)]], + ['e', [new Type(Type::BUILTIN_TYPE_OBJECT, true, Dummy::class, false, [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, [], [new Type(Type::BUILTIN_TYPE_STRING)])], [new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, [new Type(Type::BUILTIN_TYPE_INT)], [new Type(Type::BUILTIN_TYPE_OBJECT, false, \Traversable::class, true, [], [new Type(Type::BUILTIN_TYPE_OBJECT, false, DefaultValue::class)])])]), new Type(Type::BUILTIN_TYPE_OBJECT, false, ParentDummy::class)]], ['f', null], ['g', [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, [], [new Type(Type::BUILTIN_TYPE_STRING), new Type(Type::BUILTIN_TYPE_INT)])]], ]; @@ -427,6 +427,11 @@ public static function intRangeTypeProvider(): array ['c', [new Type(Type::BUILTIN_TYPE_INT)]], ]; } + + public function testGenericInterface() + { + $this->assertNull($this->extractor->getTypes(Dummy::class, 'genericInterface')); + } } class PhpStanOmittedParamTagTypeDocBlock diff --git a/Tests/Extractor/ReflectionExtractorTest.php b/Tests/Extractor/ReflectionExtractorTest.php index d3d5751..06e8bc5 100644 --- a/Tests/Extractor/ReflectionExtractorTest.php +++ b/Tests/Extractor/ReflectionExtractorTest.php @@ -73,6 +73,7 @@ public function testGetProperties() 'arrayOfMixed', 'listOfStrings', 'parentAnnotation', + 'genericInterface', 'foo', 'foo2', 'foo3', @@ -137,6 +138,7 @@ public function testGetPropertiesWithCustomPrefixes() 'arrayOfMixed', 'listOfStrings', 'parentAnnotation', + 'genericInterface', 'foo', 'foo2', 'foo3', @@ -190,6 +192,7 @@ public function testGetPropertiesWithNoPrefixes() 'arrayOfMixed', 'listOfStrings', 'parentAnnotation', + 'genericInterface', 'foo', 'foo2', 'foo3', diff --git a/Tests/Fixtures/Dummy.php b/Tests/Fixtures/Dummy.php index 06c0783..cf0c791 100644 --- a/Tests/Fixtures/Dummy.php +++ b/Tests/Fixtures/Dummy.php @@ -165,6 +165,11 @@ class Dummy extends ParentDummy */ public $parentAnnotation; + /** + * @var \BackedEnum + */ + public $genericInterface; + public static function getStatic() { } diff --git a/Tests/Fixtures/DummyUnionType.php b/Tests/Fixtures/DummyUnionType.php index 86ddb8a..7e2e1aa 100644 --- a/Tests/Fixtures/DummyUnionType.php +++ b/Tests/Fixtures/DummyUnionType.php @@ -40,7 +40,7 @@ class DummyUnionType public $d; /** - * @var (Dummy, (int | (string)[])> | ParentDummy | null) + * @var (Dummy, (int | (\Traversable)[])> | ParentDummy | null) */ public $e; diff --git a/Util/PhpDocTypeHelper.php b/Util/PhpDocTypeHelper.php index 6020be0..dc8a941 100644 --- a/Util/PhpDocTypeHelper.php +++ b/Util/PhpDocTypeHelper.php @@ -102,9 +102,9 @@ public function getTypes(DocType $varType): array /** * Creates a {@see Type} from a PHPDoc type. */ - private function createType(DocType $type, bool $nullable, ?string $docType = null): ?Type + private function createType(DocType $type, bool $nullable): ?Type { - $docType = $docType ?? (string) $type; + $docType = (string) $type; if ($type instanceof Collection) { $fqsen = $type->getFqsen(); @@ -115,10 +115,17 @@ private function createType(DocType $type, bool $nullable, ?string $docType = nu [$phpType, $class] = $this->getPhpTypeAndClass((string) $fqsen); + $collection = \is_a($class, \Traversable::class, true) || \is_a($class, \ArrayAccess::class, true); + + // it's safer to fall back to other extractors if the generic type is too abstract + if (!$collection && !class_exists($class)) { + return null; + } + $keys = $this->getTypes($type->getKeyType()); $values = $this->getTypes($type->getValueType()); - return new Type($phpType, $nullable, $class, true, $keys, $values); + return new Type($phpType, $nullable, $class, $collection, $keys, $values); } // Cannot guess diff --git a/Util/PhpStanTypeHelper.php b/Util/PhpStanTypeHelper.php index 256122a..6171530 100644 --- a/Util/PhpStanTypeHelper.php +++ b/Util/PhpStanTypeHelper.php @@ -121,6 +121,13 @@ private function extractTypes(TypeNode $node, NameScope $nameScope): array return [$mainType]; } + $collection = $mainType->isCollection() || \in_array($mainType->getClassName(), [\Traversable::class, \Iterator::class, \IteratorAggregate::class, \ArrayAccess::class, \Generator::class], true); + + // it's safer to fall back to other extractors if the generic type is too abstract + if (!$collection && !class_exists($mainType->getClassName())) { + return []; + } + $collectionKeyTypes = $mainType->getCollectionKeyTypes(); $collectionKeyValues = []; if (1 === \count($node->genericTypes)) { @@ -136,7 +143,7 @@ private function extractTypes(TypeNode $node, NameScope $nameScope): array } } - return [new Type($mainType->getBuiltinType(), $mainType->isNullable(), $mainType->getClassName(), true, $collectionKeyTypes, $collectionKeyValues)]; + return [new Type($mainType->getBuiltinType(), $mainType->isNullable(), $mainType->getClassName(), $collection, $collectionKeyTypes, $collectionKeyValues)]; } if ($node instanceof ArrayShapeNode) { return [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true)];