Skip to content

Commit eb93cbb

Browse files
committed
skip marker attribute in RemoveEmptyClassMethodRector
1 parent bb32c62 commit eb93cbb

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveEmptyClassMethodRector\Fixture;
4+
5+
#[\Attribute()]
6+
final class SkipAttributeMarker
7+
{
8+
/**
9+
* @param class-string $className
10+
*/
11+
public function __construct(string $className)
12+
{
13+
}
14+
}

rules/DeadCode/Rector/ClassMethod/RemoveEmptyClassMethodRector.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
namespace Rector\DeadCode\Rector\ClassMethod;
66

7+
use PhpParser\Comment\Doc;
78
use PhpParser\Node;
89
use PhpParser\Node\Stmt\Class_;
910
use PhpParser\Node\Stmt\ClassMethod;
1011
use PHPStan\PhpDocParser\Ast\PhpDoc\DeprecatedTagValueNode;
12+
use PHPStan\Reflection\ClassReflection;
1113
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
1214
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
1315
use Rector\Configuration\Parameter\FeatureFlags;
@@ -160,7 +162,17 @@ private function shouldSkipClassMethod(Class_ $class, ClassMethod $classMethod):
160162
return ! $classMethod->isPublic();
161163
}
162164

163-
return $this->isName($classMethod, MethodName::INVOKE);
165+
if ($this->isName($classMethod, MethodName::INVOKE)) {
166+
return true;
167+
}
168+
169+
$classReflection = $scope->getClassReflection();
170+
if (! $classReflection instanceof ClassReflection) {
171+
return false;
172+
}
173+
174+
// skip constructor in attributes as might be a marker parameter
175+
return $classReflection->isAttributeClass() && $classMethod->getDocComment() instanceof Doc;
164176
}
165177

166178
private function hasDeprecatedAnnotation(ClassMethod $classMethod): bool

0 commit comments

Comments
 (0)