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; + } +}