Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,17 @@ private function getDocBlockFromMethod(string $class, string $ucFirstProperty, i
}

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,23 @@ public function propertiesDefinedByTraitsProvider(): array
];
}

/**
* @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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}