From 3186869729b86b9ff808d0c1fd18660832e9f81b Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Fri, 10 Jul 2026 11:48:34 +0200 Subject: [PATCH 1/2] [CodeQuality] Add AssertClassToThisAssertRector --- config/sets/phpunit-code-quality.php | 2 + .../AssertClassToThisAssertRectorTest.php | 28 +++++ .../Fixture/basic.php.inc | 33 +++++ .../Fixture/fully_qualified.php.inc | 31 +++++ .../Fixture/skip_non_test_class.php.inc | 13 ++ .../Fixture/skip_self_call.php.inc | 13 ++ .../Fixture/skip_static_closure.php.inc | 14 +++ .../Fixture/skip_static_method.php.inc | 14 +++ .../config/configured_rule.php | 10 ++ .../Class_/AssertClassToThisAssertRector.php | 116 ++++++++++++++++++ 10 files changed, 274 insertions(+) create mode 100644 rules-tests/CodeQuality/Rector/Class_/AssertClassToThisAssertRector/AssertClassToThisAssertRectorTest.php create mode 100644 rules-tests/CodeQuality/Rector/Class_/AssertClassToThisAssertRector/Fixture/basic.php.inc create mode 100644 rules-tests/CodeQuality/Rector/Class_/AssertClassToThisAssertRector/Fixture/fully_qualified.php.inc create mode 100644 rules-tests/CodeQuality/Rector/Class_/AssertClassToThisAssertRector/Fixture/skip_non_test_class.php.inc create mode 100644 rules-tests/CodeQuality/Rector/Class_/AssertClassToThisAssertRector/Fixture/skip_self_call.php.inc create mode 100644 rules-tests/CodeQuality/Rector/Class_/AssertClassToThisAssertRector/Fixture/skip_static_closure.php.inc create mode 100644 rules-tests/CodeQuality/Rector/Class_/AssertClassToThisAssertRector/Fixture/skip_static_method.php.inc create mode 100644 rules-tests/CodeQuality/Rector/Class_/AssertClassToThisAssertRector/config/configured_rule.php create mode 100644 rules/CodeQuality/Rector/Class_/AssertClassToThisAssertRector.php diff --git a/config/sets/phpunit-code-quality.php b/config/sets/phpunit-code-quality.php index 449d3fe6..903bd979 100644 --- a/config/sets/phpunit-code-quality.php +++ b/config/sets/phpunit-code-quality.php @@ -7,6 +7,7 @@ use Rector\PHPUnit\CodeQuality\Rector\Class_\AddParamTypeFromDependsRector; use Rector\PHPUnit\CodeQuality\Rector\Class_\AddReturnTypeToDependedRector; use Rector\PHPUnit\CodeQuality\Rector\Class_\AddStubIntersectionVarToStubPropertyRector; +use Rector\PHPUnit\CodeQuality\Rector\Class_\AssertClassToThisAssertRector; use Rector\PHPUnit\CodeQuality\Rector\Class_\ConstructClassMethodToSetUpTestCaseRector; use Rector\PHPUnit\CodeQuality\Rector\Class_\InlineStubPropertyToCreateStubMethodCallRector; use Rector\PHPUnit\CodeQuality\Rector\Class_\NarrowUnusedSetUpDefinedPropertyRector; @@ -73,6 +74,7 @@ AssertEqualsToSameRector::class, PreferPHPUnitThisCallRector::class, + AssertClassToThisAssertRector::class, YieldDataProviderRector::class, RemoveEmptyTestMethodRector::class, ReplaceTestAnnotationWithPrefixedFunctionRector::class, diff --git a/rules-tests/CodeQuality/Rector/Class_/AssertClassToThisAssertRector/AssertClassToThisAssertRectorTest.php b/rules-tests/CodeQuality/Rector/Class_/AssertClassToThisAssertRector/AssertClassToThisAssertRectorTest.php new file mode 100644 index 00000000..0a94c042 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/Class_/AssertClassToThisAssertRector/AssertClassToThisAssertRectorTest.php @@ -0,0 +1,28 @@ +doTestFile($filePath); + } + + public static function provideData(): Iterator + { + return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/rules-tests/CodeQuality/Rector/Class_/AssertClassToThisAssertRector/Fixture/basic.php.inc b/rules-tests/CodeQuality/Rector/Class_/AssertClassToThisAssertRector/Fixture/basic.php.inc new file mode 100644 index 00000000..19f03b86 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/Class_/AssertClassToThisAssertRector/Fixture/basic.php.inc @@ -0,0 +1,33 @@ + +----- +assertSame(1, 1); + } +} + +?> diff --git a/rules-tests/CodeQuality/Rector/Class_/AssertClassToThisAssertRector/Fixture/fully_qualified.php.inc b/rules-tests/CodeQuality/Rector/Class_/AssertClassToThisAssertRector/Fixture/fully_qualified.php.inc new file mode 100644 index 00000000..7e0e6bdb --- /dev/null +++ b/rules-tests/CodeQuality/Rector/Class_/AssertClassToThisAssertRector/Fixture/fully_qualified.php.inc @@ -0,0 +1,31 @@ + +----- +assertEquals('expected', $result); + } +} + +?> diff --git a/rules-tests/CodeQuality/Rector/Class_/AssertClassToThisAssertRector/Fixture/skip_non_test_class.php.inc b/rules-tests/CodeQuality/Rector/Class_/AssertClassToThisAssertRector/Fixture/skip_non_test_class.php.inc new file mode 100644 index 00000000..b9a43220 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/Class_/AssertClassToThisAssertRector/Fixture/skip_non_test_class.php.inc @@ -0,0 +1,13 @@ + Assert::assertSame(1, 1); + } +} diff --git a/rules-tests/CodeQuality/Rector/Class_/AssertClassToThisAssertRector/Fixture/skip_static_method.php.inc b/rules-tests/CodeQuality/Rector/Class_/AssertClassToThisAssertRector/Fixture/skip_static_method.php.inc new file mode 100644 index 00000000..450febe4 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/Class_/AssertClassToThisAssertRector/Fixture/skip_static_method.php.inc @@ -0,0 +1,14 @@ +rule(AssertClassToThisAssertRector::class); +}; diff --git a/rules/CodeQuality/Rector/Class_/AssertClassToThisAssertRector.php b/rules/CodeQuality/Rector/Class_/AssertClassToThisAssertRector.php new file mode 100644 index 00000000..09d94fa6 --- /dev/null +++ b/rules/CodeQuality/Rector/Class_/AssertClassToThisAssertRector.php @@ -0,0 +1,116 @@ +assert*()', [ + new CodeSample( + <<<'CODE_SAMPLE' +use PHPUnit\Framework\Assert; +use PHPUnit\Framework\TestCase; + +final class SomeClass extends TestCase +{ + public function run() + { + Assert::assertEquals('expected', $result); + } +} +CODE_SAMPLE + , + <<<'CODE_SAMPLE' +use PHPUnit\Framework\Assert; +use PHPUnit\Framework\TestCase; + +final class SomeClass extends TestCase +{ + public function run() + { + $this->assertEquals('expected', $result); + } +} +CODE_SAMPLE + ), + ]); + } + + /** + * @return array> + */ + public function getNodeTypes(): array + { + return [Class_::class]; + } + + /** + * @param Class_ $node + */ + public function refactor(Node $node): ?Node + { + if (! $this->testsNodeAnalyzer->isInTestClass($node)) { + return null; + } + + $hasChanged = false; + + $this->traverseNodesWithCallable($node, function (Node $node) use (&$hasChanged): int|null|MethodCall { + $isInsideStaticFunctionLike = ($node instanceof ClassMethod && $node->isStatic()) || (($node instanceof Closure || $node instanceof ArrowFunction) && $node->static); + if ($isInsideStaticFunctionLike) { + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + } + + if (! $node instanceof StaticCall) { + return null; + } + + if ($node->isFirstClassCallable()) { + return null; + } + + if (! $this->isName($node->class, PHPUnitClassName::ASSERT)) { + return null; + } + + $methodName = $this->getName($node->name); + if ($methodName === null) { + return null; + } + + $hasChanged = true; + return $this->nodeFactory->createMethodCall('this', $methodName, $node->getArgs()); + }); + + if ($hasChanged) { + return $node; + } + + return null; + } +} From 6bb27402b1632d95ca76cc715d3b17c1ee4a6475 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Fri, 10 Jul 2026 11:51:07 +0200 Subject: [PATCH 2/2] remove rule from code-quality set --- config/sets/phpunit-code-quality.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/config/sets/phpunit-code-quality.php b/config/sets/phpunit-code-quality.php index 903bd979..449d3fe6 100644 --- a/config/sets/phpunit-code-quality.php +++ b/config/sets/phpunit-code-quality.php @@ -7,7 +7,6 @@ use Rector\PHPUnit\CodeQuality\Rector\Class_\AddParamTypeFromDependsRector; use Rector\PHPUnit\CodeQuality\Rector\Class_\AddReturnTypeToDependedRector; use Rector\PHPUnit\CodeQuality\Rector\Class_\AddStubIntersectionVarToStubPropertyRector; -use Rector\PHPUnit\CodeQuality\Rector\Class_\AssertClassToThisAssertRector; use Rector\PHPUnit\CodeQuality\Rector\Class_\ConstructClassMethodToSetUpTestCaseRector; use Rector\PHPUnit\CodeQuality\Rector\Class_\InlineStubPropertyToCreateStubMethodCallRector; use Rector\PHPUnit\CodeQuality\Rector\Class_\NarrowUnusedSetUpDefinedPropertyRector; @@ -74,7 +73,6 @@ AssertEqualsToSameRector::class, PreferPHPUnitThisCallRector::class, - AssertClassToThisAssertRector::class, YieldDataProviderRector::class, RemoveEmptyTestMethodRector::class, ReplaceTestAnnotationWithPrefixedFunctionRector::class,