Closed
Description
Symfony version(s) affected: 4.4.7
Description
If a class uses a trait which defines a property with a PHPDoc type hint, the property will have an incorrect namespace when extracted with $extractor->getTypes()
How to reproduce
/*
* A/SomeTrait.php
*/
namespace A;
use A\B\TypedClass;
trait SomeTrait
{
/** @var TypedClass */
public $typedClass;
}
/*
* A/SomeClass.php
*/
namespace A;
final class SomeClass
{
use SomeTrait;
}
/*
* A/B/TypedClass.php
*/
namespace A\B;
final class TypedClass
{
}
/*
* Usage
*/
$extractor = new PropertyInfoExtractor([], [new PhpDocExtractor()]);
$types = $extractor->getTypes(SomeClass::class, 'typedClass');
/*
* This will output 'A\TypedClass' but no such class exists.
* It should be 'A\B\TypedClass'.
*/
echo $types[0]->getClassName();
Possible Solution
Looks like the property name is simply appended to the namespace of the class where the trait is used, instead of taking into account the use
statement inside the trait.
Additional context
A workaround for this is typehinting the property in the trait using FQCN instead of relying on use
.
The same problem also happens with the following setup:
A/
B/
SomeTrait.php
TypedClass.php
SomeClass.php
As long as the namespace of SomeClass
is different than TypedClass
.