Skip to content

Commit 13f7abd

Browse files
authored
skip marker attribute in RemoveEmptyClassMethodRector (#7813)
* skip marker attribute in RemoveEmptyClassMethodRector * handle ctor only
1 parent bb32c62 commit 13f7abd

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveEmptyClassMethodRector\Fixture;
4+
5+
#[\Attribute()]
6+
final class NonConstructorAttribute
7+
{
8+
/**
9+
* @param class-string $className
10+
*/
11+
public function run(string $className)
12+
{
13+
}
14+
}
15+
16+
?>
17+
-----
18+
<?php
19+
20+
namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveEmptyClassMethodRector\Fixture;
21+
22+
#[\Attribute()]
23+
final class NonConstructorAttribute
24+
{
25+
}
26+
27+
?>
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: 28 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,16 @@ 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+
return $this->isAttributeMarkerConstructor($classMethod, $classReflection);
164175
}
165176

166177
private function hasDeprecatedAnnotation(ClassMethod $classMethod): bool
@@ -172,4 +183,20 @@ private function hasDeprecatedAnnotation(ClassMethod $classMethod): bool
172183

173184
return $phpDocInfo->hasByType(DeprecatedTagValueNode::class);
174185
}
186+
187+
/**
188+
* Skip constructor in attributes as might be a marker parameter
189+
*/
190+
private function isAttributeMarkerConstructor(ClassMethod $classMethod, ClassReflection $classReflection): bool
191+
{
192+
if (! $this->isName($classMethod, MethodName::CONSTRUCT)) {
193+
return false;
194+
}
195+
196+
if (! $classReflection->isAttributeClass()) {
197+
return false;
198+
}
199+
200+
return $classMethod->getDocComment() instanceof Doc;
201+
}
175202
}

0 commit comments

Comments
 (0)