Skip to content

Commit bbadfb3

Browse files
soullivaneuhnicolas-grekas
authored andcommitted
[PropertyInfo] fix attribute namespace with recursive traits
1 parent 2dd8445 commit bbadfb3

File tree

4 files changed

+77
-16
lines changed

4 files changed

+77
-16
lines changed

src/Symfony/Component/PropertyInfo/Extractor/PhpDocExtractor.php

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -220,17 +220,15 @@ private function getDocBlockFromProperty(string $class, string $property): ?DocB
220220
return null;
221221
}
222222

223-
try {
224-
$reflector = $reflectionProperty->getDeclaringClass();
225-
226-
foreach ($reflector->getTraits() as $trait) {
227-
if ($trait->hasProperty($property)) {
228-
$reflector = $trait;
223+
$reflector = $reflectionProperty->getDeclaringClass();
229224

230-
break;
231-
}
225+
foreach ($reflector->getTraits() as $trait) {
226+
if ($trait->hasProperty($property)) {
227+
return $this->getDocBlockFromProperty($trait->getName(), $property);
232228
}
229+
}
233230

231+
try {
234232
return $this->docBlockFactory->create($reflectionProperty, $this->createFromReflector($reflector));
235233
} catch (\InvalidArgumentException $e) {
236234
return null;
@@ -268,17 +266,15 @@ private function getDocBlockFromMethod(string $class, string $ucFirstProperty, i
268266
return null;
269267
}
270268

271-
try {
272-
$reflector = $reflectionMethod->getDeclaringClass();
273-
274-
foreach ($reflector->getTraits() as $trait) {
275-
if ($trait->hasMethod($methodName)) {
276-
$reflector = $trait;
269+
$reflector = $reflectionMethod->getDeclaringClass();
277270

278-
break;
279-
}
271+
foreach ($reflector->getTraits() as $trait) {
272+
if ($trait->hasMethod($methodName)) {
273+
return $this->getDocBlockFromMethod($trait->getName(), $ucFirstProperty, $type);
280274
}
275+
}
281276

277+
try {
282278
return [$this->docBlockFactory->create($reflectionMethod, $this->createFromReflector($reflector)), $prefix];
283279
} catch (\InvalidArgumentException $e) {
284280
return null;

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,9 @@ public function propertiesDefinedByTraitsProvider(): array
292292
['propertyInTraitPrimitiveType', new Type(Type::BUILTIN_TYPE_STRING)],
293293
['propertyInTraitObjectSameNamespace', new Type(Type::BUILTIN_TYPE_OBJECT, false, DummyUsedInTrait::class)],
294294
['propertyInTraitObjectDifferentNamespace', new Type(Type::BUILTIN_TYPE_OBJECT, false, Dummy::class)],
295+
['propertyInExternalTraitPrimitiveType', new Type(Type::BUILTIN_TYPE_STRING)],
296+
['propertyInExternalTraitObjectSameNamespace', new Type(Type::BUILTIN_TYPE_OBJECT, false, Dummy::class)],
297+
['propertyInExternalTraitObjectDifferentNamespace', new Type(Type::BUILTIN_TYPE_OBJECT, false, DummyUsedInTrait::class)],
295298
];
296299
}
297300

@@ -309,6 +312,9 @@ public function methodsDefinedByTraitsProvider(): array
309312
['methodInTraitPrimitiveType', new Type(Type::BUILTIN_TYPE_STRING)],
310313
['methodInTraitObjectSameNamespace', new Type(Type::BUILTIN_TYPE_OBJECT, false, DummyUsedInTrait::class)],
311314
['methodInTraitObjectDifferentNamespace', new Type(Type::BUILTIN_TYPE_OBJECT, false, Dummy::class)],
315+
['methodInExternalTraitPrimitiveType', new Type(Type::BUILTIN_TYPE_STRING)],
316+
['methodInExternalTraitObjectSameNamespace', new Type(Type::BUILTIN_TYPE_OBJECT, false, Dummy::class)],
317+
['methodInExternalTraitObjectDifferentNamespace', new Type(Type::BUILTIN_TYPE_OBJECT, false, DummyUsedInTrait::class)],
312318
];
313319
}
314320

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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+
use Symfony\Component\PropertyInfo\Tests\Fixtures\TraitUsage\DummyUsedInTrait;
15+
16+
trait DummyTraitExternal
17+
{
18+
/**
19+
* @var string
20+
*/
21+
private $propertyInExternalTraitPrimitiveType;
22+
23+
/**
24+
* @var Dummy
25+
*/
26+
private $propertyInExternalTraitObjectSameNamespace;
27+
28+
/**
29+
* @var DummyUsedInTrait
30+
*/
31+
private $propertyInExternalTraitObjectDifferentNamespace;
32+
33+
/**
34+
* @return string
35+
*/
36+
public function getMethodInExternalTraitPrimitiveType()
37+
{
38+
return 'value';
39+
}
40+
41+
/**
42+
* @return Dummy
43+
*/
44+
public function getMethodInExternalTraitObjectSameNamespace()
45+
{
46+
return new Dummy();
47+
}
48+
49+
/**
50+
* @return DummyUsedInTrait
51+
*/
52+
public function getMethodInExternalTraitObjectDifferentNamespace()
53+
{
54+
return new DummyUsedInTrait();
55+
}
56+
}

src/Symfony/Component/PropertyInfo/Tests/Fixtures/TraitUsage/DummyTrait.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@
1212
namespace Symfony\Component\PropertyInfo\Tests\Fixtures\TraitUsage;
1313

1414
use Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy;
15+
use Symfony\Component\PropertyInfo\Tests\Fixtures\DummyTraitExternal;
1516

1617
trait DummyTrait
1718
{
19+
use DummyTraitExternal;
20+
1821
/**
1922
* @var string
2023
*/

0 commit comments

Comments
 (0)