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 236e61b

Browse filesBrowse files
bug #40811 [PropertyInfo] Use the right context for methods defined in traits (colinodell)
This PR was merged into the 4.4 branch. Discussion ---------- [PropertyInfo] Use the right context for methods defined in traits | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #34191 | License | MIT | Doc PR | Pull request #40175 only partially fixed #34191 - it solved the problem for properties used in traits but it did not address the same issue with methods. I have therefore applied the same style of fix and confirmed it works properly with tests. Commits ------- c7e9493 [PropertyInfo] Use the right context for methods defined in traits
2 parents 094b459 + c7e9493 commit 236e61b
Copy full SHA for 236e61b

File tree

3 files changed

+52
-1
lines changed
Filter options

3 files changed

+52
-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
@@ -269,7 +269,17 @@ private function getDocBlockFromMethod(string $class, string $ucFirstProperty, i
269269
}
270270

271271
try {
272-
return [$this->docBlockFactory->create($reflectionMethod, $this->createFromReflector($reflectionMethod->getDeclaringClass())), $prefix];
272+
$reflector = $reflectionMethod->getDeclaringClass();
273+
274+
foreach ($reflector->getTraits() as $trait) {
275+
if ($trait->hasMethod($methodName)) {
276+
$reflector = $trait;
277+
278+
break;
279+
}
280+
}
281+
282+
return [$this->docBlockFactory->create($reflectionMethod, $this->createFromReflector($reflector)), $prefix];
273283
} catch (\InvalidArgumentException $e) {
274284
return null;
275285
} 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
+17Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,23 @@ public function propertiesDefinedByTraitsProvider(): array
295295
];
296296
}
297297

298+
/**
299+
* @dataProvider methodsDefinedByTraitsProvider
300+
*/
301+
public function testMethodsDefinedByTraits(string $property, Type $type)
302+
{
303+
$this->assertEquals([$type], $this->extractor->getTypes(DummyUsingTrait::class, $property));
304+
}
305+
306+
public function methodsDefinedByTraitsProvider(): array
307+
{
308+
return [
309+
['methodInTraitPrimitiveType', new Type(Type::BUILTIN_TYPE_STRING)],
310+
['methodInTraitObjectSameNamespace', new Type(Type::BUILTIN_TYPE_OBJECT, false, DummyUsedInTrait::class)],
311+
['methodInTraitObjectDifferentNamespace', new Type(Type::BUILTIN_TYPE_OBJECT, false, Dummy::class)],
312+
];
313+
}
314+
298315
/**
299316
* @dataProvider propertiesStaticTypeProvider
300317
*/

‎src/Symfony/Component/PropertyInfo/Tests/Fixtures/TraitUsage/DummyTrait.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/PropertyInfo/Tests/Fixtures/TraitUsage/DummyTrait.php
+24Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,28 @@ trait DummyTrait
2929
* @var Dummy
3030
*/
3131
private $propertyInTraitObjectDifferentNamespace;
32+
33+
/**
34+
* @return string
35+
*/
36+
public function getMethodInTraitPrimitiveType()
37+
{
38+
return 'value';
39+
}
40+
41+
/**
42+
* @return DummyUsedInTrait
43+
*/
44+
public function getMethodInTraitObjectSameNamespace()
45+
{
46+
return new DummyUsedInTrait();
47+
}
48+
49+
/**
50+
* @return Dummy
51+
*/
52+
public function getMethodInTraitObjectDifferentNamespace()
53+
{
54+
return new Dummy();
55+
}
3256
}

0 commit comments

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