Skip to content

Commit 1b11e78

Browse files
author
Rokas Mikalkėnas
committed
[PropertyInfo] PhpStan extractor nested object fix
1 parent 74a6936 commit 1b11e78

File tree

5 files changed

+58
-7
lines changed

5 files changed

+58
-7
lines changed

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

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,36 @@ 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+
[, $classUses] = $this->extractFromFullClassName($reflection);
38+
$uses = [$classUses];
39+
40+
foreach (class_uses($reflection->name) as $traitFullClassName) {
41+
[, $traitUses] = $this->extractFromFullClassName(new \ReflectionClass($traitFullClassName));
42+
$uses[] = $traitUses;
43+
}
44+
45+
$parentClass = $reflection->getParentClass();
46+
if (false !== $parentClass) {
47+
$uses[] = $this->collectUses($parentClass);
48+
}
49+
50+
return $uses ? array_merge(...$uses) : [];
51+
}
52+
53+
private function extractFromFullClassName(\ReflectionClass $reflection): array
3854
{
39-
$reflection = new \ReflectionClass($fullClassName);
4055
$namespace = trim($reflection->getNamespaceName(), '\\');
4156
$fileName = $reflection->getFileName();
4257

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ public function typesProvider()
116116
['arrayOfMixed', [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_STRING), null)]],
117117
['listOfStrings', [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING))]],
118118
['self', [new Type(Type::BUILTIN_TYPE_OBJECT, false, Dummy::class)]],
119+
['rootDummyItems', [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Symfony\Component\PropertyInfo\Tests\Fixtures\RootDummy\RootDummyItem'))]],
120+
['rootDummyItem', [new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Symfony\Component\PropertyInfo\Tests\Fixtures\RootDummy\RootDummyItem')]],
119121
];
120122
}
121123

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)