Skip to content

Commit ec777ee

Browse files
[code-quality] Skip ctor to setup in ConstructClassMethodToSetUpTestCaseRector for non-test classes, e.g. helpers (#485)
* [code-quality] Skip ctor to setup in ConstructClassMethodToSetUpTestCaseRector for non-test classes, e.g. helpers * [ci-review] Rector Rectify --------- Co-authored-by: GitHub Action <actions@github.com>
1 parent 4820350 commit ec777ee

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\ConstructClassMethodToSetUpTestCaseRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class SkipNonTestButHelper extends TestCase
8+
{
9+
private $someValue;
10+
11+
public function __construct()
12+
{
13+
$this->someValue = 1000;
14+
}
15+
}

rules/CodeQuality/Rector/Class_/ConstructClassMethodToSetUpTestCaseRector.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,12 @@ public function refactor(Node $node): ?Node
101101
return null;
102102
}
103103

104-
$constructClassMethod = $node->getMethod(MethodName::CONSTRUCT);
105-
if (! $constructClassMethod instanceof ClassMethod) {
104+
if ($this->shouldSkipClass($node)) {
106105
return null;
107106
}
108107

109-
if ($this->classAnalyzer->isAnonymousClass($node)) {
108+
$constructClassMethod = $node->getMethod(MethodName::CONSTRUCT);
109+
if (! $constructClassMethod instanceof ClassMethod) {
110110
return null;
111111
}
112112

@@ -218,4 +218,16 @@ private function isParentCallNamed(Node $node, string $desiredMethodName): bool
218218

219219
return $this->nodeNameResolver->isName($node->name, $desiredMethodName);
220220
}
221+
222+
private function shouldSkipClass(Class_ $class): bool
223+
{
224+
$className = $this->getName($class);
225+
226+
// probably helper class with access to protected methods like createMock()
227+
if (! str_ends_with((string) $className, 'Test') && ! str_ends_with((string) $className, 'TestCase')) {
228+
return true;
229+
}
230+
231+
return $this->classAnalyzer->isAnonymousClass($class);
232+
}
221233
}

0 commit comments

Comments
 (0)