diff --git a/rules-tests/CodeQuality/Rector/Class_/ConstructClassMethodToSetUpTestCaseRector/Fixture/skip_non_test_but_helper.php.inc b/rules-tests/CodeQuality/Rector/Class_/ConstructClassMethodToSetUpTestCaseRector/Fixture/skip_non_test_but_helper.php.inc new file mode 100644 index 00000000..cc95e625 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/Class_/ConstructClassMethodToSetUpTestCaseRector/Fixture/skip_non_test_but_helper.php.inc @@ -0,0 +1,15 @@ +someValue = 1000; + } +} diff --git a/rules/CodeQuality/Rector/Class_/ConstructClassMethodToSetUpTestCaseRector.php b/rules/CodeQuality/Rector/Class_/ConstructClassMethodToSetUpTestCaseRector.php index b04113ee..dc3a9dca 100644 --- a/rules/CodeQuality/Rector/Class_/ConstructClassMethodToSetUpTestCaseRector.php +++ b/rules/CodeQuality/Rector/Class_/ConstructClassMethodToSetUpTestCaseRector.php @@ -101,12 +101,12 @@ public function refactor(Node $node): ?Node return null; } - $constructClassMethod = $node->getMethod(MethodName::CONSTRUCT); - if (! $constructClassMethod instanceof ClassMethod) { + if ($this->shouldSkipClass($node)) { return null; } - if ($this->classAnalyzer->isAnonymousClass($node)) { + $constructClassMethod = $node->getMethod(MethodName::CONSTRUCT); + if (! $constructClassMethod instanceof ClassMethod) { return null; } @@ -218,4 +218,16 @@ private function isParentCallNamed(Node $node, string $desiredMethodName): bool return $this->nodeNameResolver->isName($node->name, $desiredMethodName); } + + private function shouldSkipClass(Class_ $class): bool + { + $className = $this->getName($class); + + // probably helper class with access to protected methods like createMock() + if (! str_ends_with((string) $className, 'Test') && ! str_ends_with((string) $className, 'TestCase')) { + return true; + } + + return $this->classAnalyzer->isAnonymousClass($class); + } }