Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1b358fe

Browse files
committed
bug #40175 [PropertyInfo]  use the right context for properties defined in traits (xabbuh)
This PR was merged into the 4.4 branch. Discussion ---------- [PropertyInfo]  use the right context for properties defined in traits | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #28732, #34191 | License | MIT | Doc PR | Commits ------- 1572491 use the right context for properties defined in traits
2 parents aa21944 + 1572491 commit 1b358fe

File tree

5 files changed

+96
-1
lines changed

5 files changed

+96
-1
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,17 @@ private function getDocBlockFromProperty(string $class, string $property): ?DocB
201201
}
202202

203203
try {
204-
return $this->docBlockFactory->create($reflectionProperty, $this->createFromReflector($reflectionProperty->getDeclaringClass()));
204+
$reflector = $reflectionProperty->getDeclaringClass();
205+
206+
foreach ($reflector->getTraits() as $trait) {
207+
if ($trait->hasProperty($property)) {
208+
$reflector = $trait;
209+
210+
break;
211+
}
212+
}
213+
214+
return $this->docBlockFactory->create($reflectionProperty, $this->createFromReflector($reflector));
205215
} catch (\InvalidArgumentException $e) {
206216
return null;
207217
} catch (\RuntimeException $e) {

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
use phpDocumentor\Reflection\Types\Collection;
1717
use PHPUnit\Framework\TestCase;
1818
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
19+
use Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy;
20+
use Symfony\Component\PropertyInfo\Tests\Fixtures\TraitUsage\DummyUsedInTrait;
21+
use Symfony\Component\PropertyInfo\Tests\Fixtures\TraitUsage\DummyUsingTrait;
1922
use Symfony\Component\PropertyInfo\Type;
2023

2124
/**
@@ -273,6 +276,23 @@ public function testDocBlockFallback($property, $types)
273276
$this->assertEquals($types, $this->extractor->getTypes('Symfony\Component\PropertyInfo\Tests\Fixtures\DockBlockFallback', $property));
274277
}
275278

279+
/**
280+
* @dataProvider propertiesDefinedByTraitsProvider
281+
*/
282+
public function testPropertiesDefinedByTraits(string $property, Type $type)
283+
{
284+
$this->assertEquals([$type], $this->extractor->getTypes(DummyUsingTrait::class, $property));
285+
}
286+
287+
public function propertiesDefinedByTraitsProvider(): array
288+
{
289+
return [
290+
['propertyInTraitPrimitiveType', new Type(Type::BUILTIN_TYPE_STRING)],
291+
['propertyInTraitObjectSameNamespace', new Type(Type::BUILTIN_TYPE_OBJECT, false, DummyUsedInTrait::class)],
292+
['propertyInTraitObjectDifferentNamespace', new Type(Type::BUILTIN_TYPE_OBJECT, false, Dummy::class)],
293+
];
294+
}
295+
276296
protected function isPhpDocumentorV5()
277297
{
278298
if (class_exists(InvalidTag::class)) {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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\TraitUsage;
13+
14+
use Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy;
15+
16+
trait DummyTrait
17+
{
18+
/**
19+
* @var string
20+
*/
21+
private $propertyInTraitPrimitiveType;
22+
23+
/**
24+
* @var DummyUsedInTrait
25+
*/
26+
private $propertyInTraitObjectSameNamespace;
27+
28+
/**
29+
* @var Dummy
30+
*/
31+
private $propertyInTraitObjectDifferentNamespace;
32+
}
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 <[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\TraitUsage;
13+
14+
class DummyUsedInTrait
15+
{
16+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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\TraitUsage;
13+
14+
class DummyUsingTrait
15+
{
16+
use DummyTrait;
17+
}

0 commit comments

Comments
 (0)