Skip to content

Commit 32f0013

Browse files
committed
Optimize RequiredAutowiringExtension
1 parent 5a7ab53 commit 32f0013

2 files changed

Lines changed: 26 additions & 7 deletions

File tree

phpstan-baseline.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ parameters:
1515
-
1616
message: '#^Although PHPStan\\Reflection\\Php\\PhpPropertyReflection is covered by backward compatibility promise, this instanceof assumption might break because it''s not guaranteed to always stay the same\.$#'
1717
identifier: phpstanApi.instanceofAssumption
18-
count: 1
18+
count: 2
1919
path: src/Symfony/RequiredAutowiringExtension.php
2020

2121
-

src/Symfony/RequiredAutowiringExtension.php

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,17 @@ public function isInitialized(PropertyReflection $property, string $propertyName
3737
return false;
3838
}
3939

40-
if ($property->getDocComment() !== null && $this->isRequiredFromDocComment($property->getDocComment())) {
40+
$declaringClass = $property->getDeclaringClass();
41+
$declaringTrait = null;
42+
if ($property instanceof PhpPropertyReflection && $property->getDeclaringTrait() !== null) {
43+
$declaringTrait = $property->getDeclaringTrait()->getName();
44+
}
45+
46+
if (
47+
$property->getDocComment() !== null
48+
&& $declaringClass->getFileName() !== null
49+
&& $this->isRequiredFromDocComment($declaringClass->getFileName(), $declaringClass->getName(), $declaringTrait, null, $property->getDocComment())
50+
) {
4151
return true;
4252
}
4353

@@ -54,16 +64,25 @@ public function getAdditionalConstructors(ClassReflection $classReflection): arr
5464
$additionalConstructors = [];
5565
$nativeReflection = $classReflection->getNativeReflection();
5666

57-
foreach ($nativeReflection->getMethods() as $method) {
67+
foreach ($nativeReflection->getBetterReflection()->getImmediateMethods() as $method) {
5868
if (!$method->isPublic()) {
5969
continue;
6070
}
6171

62-
if ($method->getDocComment() !== false && $this->isRequiredFromDocComment($method->getDocComment())) {
72+
$declaringTrait = null;
73+
if ($method->getDeclaringClass()->isTrait()) {
74+
$declaringTrait = $method->getDeclaringClass()->getName();
75+
}
76+
77+
if (
78+
$method->getDocComment() !== null
79+
&& $method->getFileName() !== null
80+
&& $this->isRequiredFromDocComment($method->getFileName(), $nativeReflection->getName(), $declaringTrait, $method->getName(), $method->getDocComment())
81+
) {
6382
$additionalConstructors[] = $method->getName();
6483
}
6584

66-
if (count($method->getAttributes('Symfony\Contracts\Service\Attribute\Required')) === 0) {
85+
if (count($method->getAttributesByName('Symfony\Contracts\Service\Attribute\Required')) === 0) {
6786
continue;
6887
}
6988

@@ -73,9 +92,9 @@ public function getAdditionalConstructors(ClassReflection $classReflection): arr
7392
return $additionalConstructors;
7493
}
7594

76-
private function isRequiredFromDocComment(string $docComment): bool
95+
private function isRequiredFromDocComment(string $fileName, string $className, ?string $traitName, ?string $functionName, string $docComment): bool
7796
{
78-
$phpDoc = $this->fileTypeMapper->getResolvedPhpDoc(null, null, null, null, $docComment);
97+
$phpDoc = $this->fileTypeMapper->getResolvedPhpDoc($fileName, $className, $traitName, $functionName, $docComment);
7998

8099
foreach ($phpDoc->getPhpDocNodes() as $node) {
81100
// @required tag is available, meaning this property is always initialized

0 commit comments

Comments
 (0)