From d75391e98d2906bd6225f0903438227c702c60d0 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Wed, 7 May 2025 10:00:30 +0200 Subject: [PATCH 1/2] [code-quality] Skip ctor to setup in ConstructClassMethodToSetUpTestCaseRector for non-test classes, e.g. helpers --- .../Fixture/skip_non_test_but_helper.php.inc | 15 +++++++++++++++ ...nstructClassMethodToSetUpTestCaseRector.php | 18 +++++++++++++++--- 2 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 rules-tests/CodeQuality/Rector/Class_/ConstructClassMethodToSetUpTestCaseRector/Fixture/skip_non_test_but_helper.php.inc 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..7dc83ffb 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($className, 'Test') && ! str_ends_with($className, 'TestCase')) { + return true; + } + + return $this->classAnalyzer->isAnonymousClass($class); + } } From 465a561a40c747c711d8ddf859c60a99a2d7888a Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Wed, 7 May 2025 08:01:40 +0000 Subject: [PATCH 2/2] [ci-review] Rector Rectify --- .../Rector/Class_/ConstructClassMethodToSetUpTestCaseRector.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rules/CodeQuality/Rector/Class_/ConstructClassMethodToSetUpTestCaseRector.php b/rules/CodeQuality/Rector/Class_/ConstructClassMethodToSetUpTestCaseRector.php index 7dc83ffb..dc3a9dca 100644 --- a/rules/CodeQuality/Rector/Class_/ConstructClassMethodToSetUpTestCaseRector.php +++ b/rules/CodeQuality/Rector/Class_/ConstructClassMethodToSetUpTestCaseRector.php @@ -224,7 +224,7 @@ private function shouldSkipClass(Class_ $class): bool $className = $this->getName($class); // probably helper class with access to protected methods like createMock() - if (! str_ends_with($className, 'Test') && ! str_ends_with($className, 'TestCase')) { + if (! str_ends_with((string) $className, 'Test') && ! str_ends_with((string) $className, 'TestCase')) { return true; }