Skip to content

Commit 3363fa2

Browse files
authored
Read attribute-class from node attrGroups in RemoveEmptyClassMethodRector (#8705)
1 parent 2809e3f commit 3363fa2

1 file changed

Lines changed: 16 additions & 9 deletions

File tree

‎rules/DeadCode/Rector/ClassMethod/RemoveEmptyClassMethodRector.php‎

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use PhpParser\Node\Stmt\Class_;
1111
use PhpParser\Node\Stmt\ClassMethod;
1212
use PHPStan\PhpDocParser\Ast\PhpDoc\DeprecatedTagValueNode;
13-
use PHPStan\Reflection\ClassReflection;
1413
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
1514
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
1615
use Rector\Configuration\Parameter\FeatureFlags;
@@ -176,12 +175,7 @@ private function shouldSkipClassMethod(Class_ $class, ClassMethod $classMethod):
176175
return true;
177176
}
178177

179-
$classReflection = $scope->getClassReflection();
180-
if (! $classReflection instanceof ClassReflection) {
181-
return false;
182-
}
183-
184-
return $this->isAttributeMarkerConstructor($classMethod, $classReflection);
178+
return $this->isAttributeMarkerConstructor($classMethod, $class);
185179
}
186180

187181
private function hasDeprecatedAnnotation(ClassMethod $classMethod): bool
@@ -197,16 +191,29 @@ private function hasDeprecatedAnnotation(ClassMethod $classMethod): bool
197191
/**
198192
* Skip constructor in attributes as might be a marker parameter
199193
*/
200-
private function isAttributeMarkerConstructor(ClassMethod $classMethod, ClassReflection $classReflection): bool
194+
private function isAttributeMarkerConstructor(ClassMethod $classMethod, Class_ $class): bool
201195
{
202196
if (! $this->isName($classMethod, MethodName::CONSTRUCT)) {
203197
return false;
204198
}
205199

206-
if (! $classReflection->isAttributeClass()) {
200+
if (! $this->isAttributeClass($class)) {
207201
return false;
208202
}
209203

210204
return $classMethod->getDocComment() instanceof Doc;
211205
}
206+
207+
private function isAttributeClass(Class_ $class): bool
208+
{
209+
foreach ($class->attrGroups as $attrGroup) {
210+
foreach ($attrGroup->attrs as $attr) {
211+
if ($this->isName($attr->name, 'Attribute')) {
212+
return true;
213+
}
214+
}
215+
}
216+
217+
return false;
218+
}
212219
}

0 commit comments

Comments
 (0)