Skip to content

Commit e660218

Browse files
committed
skip marker attribute in RemoveUnusedConstructorParamRector
1 parent 44a4d0b commit e660218

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

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

rules/DeadCode/Rector/ClassMethod/RemoveUnusedConstructorParamRector.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
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;
@@ -91,11 +92,17 @@ public function refactor(Node $node): ?Node
9192

9293
$interfaces = $classReflection->getInterfaces();
9394
foreach ($interfaces as $interface) {
95+
// parameters are contract required → skip
9496
if ($interface->hasNativeMethod(MethodName::CONSTRUCT)) {
9597
return null;
9698
}
9799
}
98100

101+
// attributes can be used as markers
102+
if ($classReflection->isAttributeClass() && $constructorClassMethod->getDocComment() instanceof Doc) {
103+
return null;
104+
}
105+
99106
$changedConstructorClassMethod = $this->classMethodParamRemover->processRemoveParams($constructorClassMethod);
100107
if (! $changedConstructorClassMethod instanceof ClassMethod) {
101108
return null;

0 commit comments

Comments
 (0)