Skip to content

Commit bb32c62

Browse files
authored
tv skip ctor attribute (#7812)
* cs * skip marker attribute in RemoveUnusedConstructorParamRector
1 parent 540fbfa commit bb32c62

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed
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;

tests/Issues/IssuePropertyPromoRemoveDelegatingParent/config/configured_rule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
->withRules([
99
ClassPropertyAssignToConstructorPromotionRector::class,
1010
RemoveParentDelegatingConstructorRector::class,
11-
]);
11+
]);

0 commit comments

Comments
 (0)