Skip to content

Commit 78d51de

Browse files
author
Rokas Mikalkėnas
committed
[PropertyInfo] PhpStan extractor nested object fix
1 parent d17ae34 commit 78d51de

File tree

5 files changed

+56
-7
lines changed

5 files changed

+56
-7
lines changed

src/Symfony/Component/PropertyInfo/PhpStan/NameScopeFactory.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,33 @@ final class NameScopeFactory
2222
{
2323
public function create(string $fullClassName): NameScope
2424
{
25+
$reflection = new \ReflectionClass($fullClassName);
2526
$path = explode('\\', $fullClassName);
2627
$className = array_pop($path);
27-
[$namespace, $uses] = $this->extractFromFullClassName($fullClassName);
28+
[$namespace, $uses] = $this->extractFromFullClassName($reflection);
2829

29-
foreach (class_uses($fullClassName) as $traitFullClassName) {
30-
[, $traitUses] = $this->extractFromFullClassName($traitFullClassName);
31-
$uses = array_merge($uses, $traitUses);
32-
}
30+
$uses = array_merge($uses, $this->collectUses($reflection));
3331

3432
return new NameScope($className, $namespace, $uses);
3533
}
3634

37-
private function extractFromFullClassName(string $fullClassName): array
35+
private function collectUses(\ReflectionClass $reflection): array
36+
{
37+
$uses = [$this->extractFromFullClassName($reflection)[1]];
38+
39+
foreach ($reflection->getTraits() as $traitReflection) {
40+
$uses[] = $this->extractFromFullClassName($traitReflection)[1];
41+
}
42+
43+
if (false !== $parentClass = $reflection->getParentClass()) {
44+
$uses[] = $this->collectUses($parentClass);
45+
}
46+
47+
return $uses ? array_merge(...$uses) : [];
48+
}
49+
50+
private function extractFromFullClassName(\ReflectionClass $reflection): array
3851
{
39-
$reflection = new \ReflectionClass($fullClassName);
4052
$namespace = trim($reflection->getNamespaceName(), '\\');
4153
$fileName = $reflection->getFileName();
4254

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\PropertyInfo\Tests\Fixtures\DefaultValue;
1717
use Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy;
1818
use Symfony\Component\PropertyInfo\Tests\Fixtures\ParentDummy;
19+
use Symfony\Component\PropertyInfo\Tests\Fixtures\RootDummy\RootDummyItem;
1920
use Symfony\Component\PropertyInfo\Tests\Fixtures\TraitUsage\DummyUsedInTrait;
2021
use Symfony\Component\PropertyInfo\Tests\Fixtures\TraitUsage\DummyUsingTrait;
2122
use Symfony\Component\PropertyInfo\Type;
@@ -116,6 +117,8 @@ public function typesProvider()
116117
['arrayOfMixed', [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_STRING), null)]],
117118
['listOfStrings', [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING))]],
118119
['self', [new Type(Type::BUILTIN_TYPE_OBJECT, false, Dummy::class)]],
120+
['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))]],
121+
['rootDummyItem', [new Type(Type::BUILTIN_TYPE_OBJECT, false, RootDummyItem::class)]],
119122
];
120123
}
121124

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ public function testGetProperties()
7878
'files',
7979
'propertyTypeStatic',
8080
'parentAnnotationNoParent',
81+
'rootDummyItems',
82+
'rootDummyItem',
8183
'a',
8284
'DOB',
8385
'Id',
@@ -138,6 +140,8 @@ public function testGetPropertiesWithCustomPrefixes()
138140
'files',
139141
'propertyTypeStatic',
140142
'parentAnnotationNoParent',
143+
'rootDummyItems',
144+
'rootDummyItem',
141145
'date',
142146
'c',
143147
'ct',
@@ -187,6 +191,8 @@ public function testGetPropertiesWithNoPrefixes()
187191
'files',
188192
'propertyTypeStatic',
189193
'parentAnnotationNoParent',
194+
'rootDummyItems',
195+
'rootDummyItem',
190196
],
191197
$noPrefixExtractor->getProperties('Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy')
192198
);

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\PropertyInfo\Tests\Fixtures;
1313

14+
use Symfony\Component\PropertyInfo\Tests\Fixtures\RootDummy\RootDummyItem;
15+
1416
/**
1517
* @author Kévin Dunglas <dunglas@gmail.com>
1618
*/
@@ -58,6 +60,16 @@ class ParentDummy
5860
*/
5961
public $parentAnnotationNoParent;
6062

63+
/**
64+
* @var RootDummyItem[]
65+
*/
66+
public $rootDummyItems;
67+
68+
/**
69+
* @var \Symfony\Component\PropertyInfo\Tests\Fixtures\RootDummy\RootDummyItem
70+
*/
71+
public $rootDummyItem;
72+
6173
/**
6274
* @return bool|null
6375
*/
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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\RootDummy;
13+
14+
class RootDummyItem
15+
{
16+
}

0 commit comments

Comments
 (0)