8000 [PropertyInfo] Handle collection in PhpStan same as PhpDoc · symfony/symfony@f098683 · GitHub
[go: up one dir, main page]

Skip to content

Commit f098683

Browse files
committed
[PropertyInfo] Handle collection in PhpStan same as PhpDoc
1 parent 4e57b84 commit f098683

File tree

6 files changed

+33
-1
lines changed

6 files changed

+33
-1
lines changed

src/Symfony/Component/PropertyInfo/Tests/Extractor/PhpDocExtractorTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use PHPUnit\Framework\TestCase;
1818
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
1919
use Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy;
20+
use Symfony\Component\PropertyInfo\Tests\Fixtures\DummyCollection;
2021
use Symfony\Component\PropertyInfo\Tests\Fixtures\ParentDummy;
2122
use Symfony\Component\PropertyInfo\Tests\Fixtures\PseudoTypeDummy;
2223
use Symfony\Component\PropertyInfo\Tests\Fixtures\TraitUsage\DummyUsedInTrait;
@@ -160,6 +161,7 @@ public static function typesProvider()
160161
null,
161162
],
162163
['self', [new Type(Type::BUILTIN_TYPE_OBJECT, false, Dummy::class)], null, null],
164+
['collectionAsObject', [new Type(Type::BUILTIN_TYPE_OBJECT, false, DummyCollection::class, true, [new Type(Type::BUILTIN_TYPE_INT)], [new Type(Type::BUILTIN_TYPE_STRING)])], null, null],
163165
];
164166
}
165167

src/Symfony/Component/PropertyInfo/Tests/Extractor/PhpStanExtractorTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\PropertyInfo\Tests\Fixtures\ConstructorDummyWithoutDocBlock;
1818
use Symfony\Component\PropertyInfo\Tests\Fixtures\DefaultValue;
1919
use Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy;
20+
use Symfony\Component\PropertyInfo\Tests\Fixtures\DummyCollection;
2021
use Symfony\Component\PropertyInfo\Tests\Fixtures\ParentDummy;
2122
use Symfony\Component\PropertyInfo\Tests\Fixtures\RootDummy\RootDummyItem;
2223
use Symfony\Component\PropertyInfo\Tests\Fixtures\TraitUsage\AnotherNamespace\DummyInAnotherNamespace;
@@ -130,6 +131,7 @@ public static function typesProvider()
130131
['self', [new Type(Type::BUILTIN_TYPE_OBJECT, false, Dummy::class)]],
131132
['rootDummyItems', [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_OBJECT, false, RootDummyItem::class))]],
132133
['rootDummyItem', [new Type(Type::BUILTIN_TYPE_OBJECT, false, RootDummyItem::class)]],
134+
['collectionAsObject', [new Type(Type::BUILTIN_TYPE_OBJECT, false, DummyCollection::class, true, [new Type(Type::BUILTIN_TYPE_INT)], [new Type(Type::BUILTIN_TYPE_STRING)])]],
133135
];
134136
}
135137

src/Symfony/Component/PropertyInfo/Tests/Extractor/ReflectionExtractorTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public function testGetProperties()
5353
'bal',
5454
'parent',
5555
'collection',
56+
'collectionAsObject',
5657
'nestedCollection',
5758
'mixedCollection',
5859
'B',
@@ -118,6 +119,7 @@ public function testGetPropertiesWithCustomPrefixes()
118119
'bal',
119120
'parent',
120121
'collection',
122+
'collectionAsObject',
121123
'nestedCollection',
122124
'mixedCollection',
123125
'B',
@@ -172,6 +174,7 @@ public function testGetPropertiesWithNoPrefixes()
172174
'bal',
173175
'parent',
174176
'collection',
177+
'collectionAsObject',
175178
'nestedCollection',
176179
'mixedCollection',
177180
'B',

src/Symfony/Component/PropertyInfo/Tests/Fixtures/Dummy.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ class Dummy extends ParentDummy
4646
*/
4747
public $collection;
4848

49+
/**
50+
* @var DummyCollection<int, string>
51+
*/
52+
public $collectionAsObject;
53+
4954
/**
5055
* @var string[][]
5156
*/
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\PropertyInfo\Tests\Fixtures;
13+
14+
final class DummyCollection implements \IteratorAggregate
15+
{
16+
public function getIterator(): \Traversable
17+
{
18+
return [];
19+
}
20+
}

src/Symfony/Component/PropertyInfo/Util/PhpStanTypeHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ private function extractTypes(TypeNode $node, NameScope $nameScope): array
121121
return [$mainType];
122122
}
123123

124-
$collection = $mainType->isCollection() || \in_array($mainType->getClassName(), [\Traversable::class, \Iterator::class, \IteratorAggregate::class, \ArrayAccess::class, \Generator::class], true);
124+
$collection = $mainType->isCollection() || \is_a($mainType->getClassName(), \Traversable::class, true) || \is_a($mainType->getClassName(), \ArrayAccess::class, true);
125125

126126
// it's safer to fall back to other extractors if the generic type is too abstract
127127
if (!$collection && !class_exists($mainType->getClassName())) {

0 commit comments

Comments
 (0)
0