Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1572491

Browse filesBrowse files
committed
use the right context for properties defined in traits
1 parent aad1d09 commit 1572491
Copy full SHA for 1572491

File tree

5 files changed

+96
-1
lines changed
Filter options

5 files changed

+96
-1
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/PropertyInfo/Extractor/PhpDocExtractor.php
+11-1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/PropertyInfo/Tests/Extractor/PhpDocExtractorTest.php
+20Lines 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)) {
+32Lines 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 <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\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+
}
+16Lines 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\TraitUsage;
13+
14+
class DummyUsedInTrait
15+
{
16+
}
+17Lines 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 <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\TraitUsage;
13+
14+
class DummyUsingTrait
15+
{
16+
use DummyTrait;
17+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.