|
19 | 19 | use PHPStan\Reflection\ClassReflection; |
20 | 20 | use PHPStan\Reflection\ReflectionProvider; |
21 | 21 | use Rector\Contract\Rector\ConfigurableRectorInterface; |
| 22 | +use Rector\DeadCode\NodeAnalyzer\ParentClassAnalyzer; |
22 | 23 | use Rector\NodeAnalyzer\ClassAnalyzer; |
23 | 24 | use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer; |
24 | 25 | use Rector\PhpParser\AstResolver; |
@@ -54,6 +55,7 @@ public function __construct( |
54 | 55 | private readonly PhpAttributeAnalyzer $phpAttributeAnalyzer, |
55 | 56 | private readonly AstResolver $astResolver, |
56 | 57 | private readonly ValueResolver $valueResolver, |
| 58 | + private readonly ParentClassAnalyzer $parentClassAnalyzer, |
57 | 59 | ) { |
58 | 60 | } |
59 | 61 |
|
@@ -135,8 +137,8 @@ public function refactor(Node $node): ?Node |
135 | 137 | return null; |
136 | 138 | } |
137 | 139 |
|
138 | | - // skip if no parents are involved |
139 | | - if ($node->extends === null) { |
| 140 | + // skip if no parents, nor traits, nor strinables are involved |
| 141 | + if ($node->extends === null && $node->getTraitUses() === [] && ! $this->implementsStringable($node)) { |
140 | 142 | return null; |
141 | 143 | } |
142 | 144 |
|
@@ -232,12 +234,18 @@ private function shouldSkipClassMethod(ClassMethod $classMethod): bool |
232 | 234 | return true; |
233 | 235 | } |
234 | 236 |
|
| 237 | + // nothing to override |
235 | 238 | if ($classMethod->isPrivate()) { |
236 | 239 | return true; |
237 | 240 | } |
238 | 241 |
|
239 | 242 | // ignore if it already uses the attribute |
240 | | - return $this->phpAttributeAnalyzer->hasPhpAttribute($classMethod, self::OVERRIDE_CLASS); |
| 243 | + if ($this->phpAttributeAnalyzer->hasPhpAttribute($classMethod, self::OVERRIDE_CLASS)) { |
| 244 | + return true; |
| 245 | + } |
| 246 | + |
| 247 | + // skip test setup method override, as rather clutters the code than helps |
| 248 | + return $this->isName($classMethod, 'setUp') && $this->parentClassAnalyzer->hasParentCall($classMethod); |
241 | 249 | } |
242 | 250 |
|
243 | 251 | private function shouldSkipParentClassMethod(ClassReflection $parentClassReflection, ClassMethod $classMethod): bool |
@@ -289,4 +297,15 @@ private function shouldSkipParentClassMethod(ClassReflection $parentClassReflect |
289 | 297 |
|
290 | 298 | return false; |
291 | 299 | } |
| 300 | + |
| 301 | + private function implementsStringable(Class_ $class): bool |
| 302 | + { |
| 303 | + foreach ($class->implements as $implement) { |
| 304 | + if ($this->isName($implement, 'Stringable')) { |
| 305 | + return true; |
| 306 | + } |
| 307 | + } |
| 308 | + |
| 309 | + return false; |
| 310 | + } |
292 | 311 | } |
0 commit comments