Skip to content

Commit 9eba8c5

Browse files
committed
exclude test case setup from override attribute, as not helpful
1 parent a65318b commit 9eba8c5

4 files changed

Lines changed: 94 additions & 1 deletion

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector\Fixture;
4+
5+
use Rector\Tests\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector\Source\SomeAbstractTest;
6+
7+
final class SkipSetupPHPUnitAsClutter extends SomeAbstractTest
8+
{
9+
protected function setUp(): void
10+
{
11+
parent::setUp();
12+
13+
$someValue = 1;
14+
}
15+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector\Source;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
abstract class SomeAbstractTest extends TestCase
8+
{
9+
protected function setUp(): void
10+
{
11+
$value = 1000;
12+
}
13+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\DeadCode\NodeAnalyzer;
6+
7+
use PhpParser\Node\Stmt\ClassMethod;
8+
use Rector\NodeNameResolver\NodeNameResolver;
9+
10+
final class ParentClassAnalyzer
11+
{
12+
public function __construct(
13+
private readonly NodeNameResolver $nodeNameResolver,
14+
) {
15+
16+
}
17+
18+
public function hasParentCall(ClassMethod $classMethod): bool
19+
{
20+
if ($classMethod->isAbstract()) {
21+
return false;
22+
}
23+
24+
if ($classMethod->isPrivate()) {
25+
return false;
26+
}
27+
28+
$classMethodName = $classMethod->name->name;
29+
30+
foreach ($classMethod->stmts as $stmt) {
31+
if (! $stmt instanceof \PhpParser\Node\Stmt\Expression) {
32+
continue;
33+
}
34+
35+
$expr = $stmt->expr;
36+
if (! $expr instanceof \PhpParser\Node\Expr\StaticCall) {
37+
continue;
38+
}
39+
40+
if (! $this->nodeNameResolver->isName($expr->class, 'parent')) {
41+
continue;
42+
}
43+
44+
if ($this->nodeNameResolver->isName($expr->name, $classMethodName)) {
45+
return true;
46+
}
47+
}
48+
49+
return false;
50+
}
51+
}

rules/Php83/Rector/ClassMethod/AddOverrideAttributeToOverriddenMethodsRector.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use PHPStan\Reflection\ClassReflection;
2020
use PHPStan\Reflection\ReflectionProvider;
2121
use Rector\Contract\Rector\ConfigurableRectorInterface;
22+
use Rector\DeadCode\NodeAnalyzer\ParentClassAnalyzer;
2223
use Rector\NodeAnalyzer\ClassAnalyzer;
2324
use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer;
2425
use Rector\PhpParser\AstResolver;
@@ -54,6 +55,7 @@ public function __construct(
5455
private readonly PhpAttributeAnalyzer $phpAttributeAnalyzer,
5556
private readonly AstResolver $astResolver,
5657
private readonly ValueResolver $valueResolver,
58+
private readonly ParentClassAnalyzer $parentClassAnalyzer,
5759
) {
5860
}
5961

@@ -232,12 +234,24 @@ private function shouldSkipClassMethod(ClassMethod $classMethod): bool
232234
return true;
233235
}
234236

237+
// nothing to override
235238
if ($classMethod->isPrivate()) {
236239
return true;
237240
}
238241

239242
// 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+
if ($this->isName($classMethod, 'setUp')) {
249+
if ($this->parentClassAnalyzer->hasParentCall($classMethod)) {
250+
return true;
251+
}
252+
}
253+
254+
return false;
241255
}
242256

243257
private function shouldSkipParentClassMethod(ClassReflection $parentClassReflection, ClassMethod $classMethod): bool

0 commit comments

Comments
 (0)