[PropertyInfo] Use the right context for methods defined in traits

This commit is contained in:
Colin O'Dell 2021-04-13 18:18:08 -04:00
parent 3f42c08abd
commit c7e9493c5b
No known key found for this signature in database
GPG Key ID: 60A4DAD272A01CB8
3 changed files with 52 additions and 1 deletions

View File

@ -269,7 +269,17 @@ class PhpDocExtractor implements PropertyDescriptionExtractorInterface, Property
}
try {
return [$this->docBlockFactory->create($reflectionMethod, $this->createFromReflector($reflectionMethod->getDeclaringClass())), $prefix];
$reflector = $reflectionMethod->getDeclaringClass();
foreach ($reflector->getTraits() as $trait) {
if ($trait->hasMethod($methodName)) {
$reflector = $trait;
break;
}
}
return [$this->docBlockFactory->create($reflectionMethod, $this->createFromReflector($reflector)), $prefix];
} catch (\InvalidArgumentException $e) {
return null;
} catch (\RuntimeException $e) {

View File

@ -295,6 +295,23 @@ class PhpDocExtractorTest extends TestCase
];
}
/**
* @dataProvider methodsDefinedByTraitsProvider
*/
public function testMethodsDefinedByTraits(string $property, Type $type)
{
$this->assertEquals([$type], $this->extractor->getTypes(DummyUsingTrait::class, $property));
}
public function methodsDefinedByTraitsProvider(): array
{
return [
['methodInTraitPrimitiveType', new Type(Type::BUILTIN_TYPE_STRING)],
['methodInTraitObjectSameNamespace', new Type(Type::BUILTIN_TYPE_OBJECT, false, DummyUsedInTrait::class)],
['methodInTraitObjectDifferentNamespace', new Type(Type::BUILTIN_TYPE_OBJECT, false, Dummy::class)],
];
}
/**
* @dataProvider propertiesStaticTypeProvider
*/

View File

@ -29,4 +29,28 @@ trait DummyTrait
* @var Dummy
*/
private $propertyInTraitObjectDifferentNamespace;
/**
* @return string
*/
public function getMethodInTraitPrimitiveType()
{
return 'value';
}
/**
* @return DummyUsedInTrait
*/
public function getMethodInTraitObjectSameNamespace()
{
return new DummyUsedInTrait();
}
/**
* @return Dummy
*/
public function getMethodInTraitObjectDifferentNamespace()
{
return new Dummy();
}
}