Skip to content

Commit 1ed06ec

Browse files
bug symfony#61752 [PropertyInfo] Fix getting type from constructor of parent class in PhpStanExtractor (wuchen90)
This PR was merged into the 7.3 branch. Discussion ---------- [PropertyInfo] Fix getting type from constructor of parent class in `PhpStanExtractor` | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | yes | New feature? | no <!-- if yes, also update src/**/CHANGELOG.md --> | Deprecations? | no <!-- if yes, also update UPGRADE-*.md and src/**/CHANGELOG.md --> | License | MIT The PhpStanExtractor fails when we try to resolve the type of a constructor argument that is declared in parent class. It is mainly because the TypeContextFactory doesn't get all the `uses` of parent classes. Commits ------- 63532f5 [PropertyInfo] Fix getting type from constructor of parent class in PhpStanExtractor
2 parents a72def9 + 63532f5 commit 1ed06ec

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

src/Symfony/Component/PropertyInfo/Extractor/PhpStanExtractor.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,11 +287,11 @@ public function getLongDescription(string $class, string $property, array $conte
287287
}
288288

289289
/**
290-
* A docblock is splitted into a template marker, a short description, an optional long description and a tags section.
290+
* A docblock is split into a template marker, a short description, an optional long description and a tags section.
291291
*
292-
* - The template marker is either empty, or #@+ or #@-.
292+
* - The template marker is either empty, #@+ or #@-.
293293
* - The short description is started from a non-tag character, and until one or multiple newlines.
294-
* - The long description (optional), is started from a non-tag character, and until a new line is encountered followed by a tag.
294+
* - The long description (optional) is started from a non-tag character, and until a new line is encountered followed by a tag.
295295
* - Tags, and the remaining characters
296296
*
297297
* This method returns the short and the long descriptions.
@@ -374,7 +374,7 @@ private function getDescriptionsFromDocNode(PhpDocNode $docNode): array
374374
];
375375
}
376376

377-
private function getDocBlockFromConstructor(string $class, string $property): ?ParamTagValueNode
377+
private function getDocBlockFromConstructor(string &$class, string $property): ?ParamTagValueNode
378378
{
379379
try {
380380
$reflectionClass = new \ReflectionClass($class);
@@ -389,6 +389,7 @@ private function getDocBlockFromConstructor(string $class, string $property): ?P
389389
if (!$rawDocNode = $reflectionConstructor->getDocComment()) {
390390
return null;
391391
}
392+
$class = $reflectionConstructor->class;
392393

393394
$phpDocNode = $this->getPhpDocNode($rawDocNode);
394395

src/Symfony/Component/PropertyInfo/Tests/Extractor/PhpStanExtractorTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,6 +1143,11 @@ public static function descriptionsProvider(): iterable
11431143
yield ['bal', 'A short description ignoring template.', "A long description...\n\n...over several lines."];
11441144
yield ['foo2', null, null];
11451145
}
1146+
1147+
public function testGetTypeFromConstructorOfParentClass()
1148+
{
1149+
$this->assertEquals(Type::nullable(Type::object(RootDummyItem::class)), $this->extractor->getTypeFromConstructor(Dummy::class, 'rootDummyItem'));
1150+
}
11461151
}
11471152

11481153
class PhpStanOmittedParamTagTypeDocBlock

src/Symfony/Component/PropertyInfo/Tests/Fixtures/ParentDummy.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ class ParentDummy
7070
*/
7171
public $rootDummyItem;
7272

73+
/**
74+
* @param RootDummyItem|null $rootDummyItem
75+
*/
76+
public function __construct(?RootDummyItem $rootDummyItem = null)
77+
{
78+
$this->rootDummyItem = $rootDummyItem;
79+
}
80+
7381
/**
7482
* @return bool|null
7583
*/

0 commit comments

Comments
 (0)