From f9cdc9a4fd83e1e463851030f20047829e8db5fa Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sun, 1 Feb 2026 22:27:25 +0100 Subject: [PATCH] [code-quality] Add AssertThatToDirectAssertRector --- .../AssertThatToDirectAssertRectorTest.php | 28 +++++ .../Fixture/simple_assert_that.php.inc | 31 +++++ .../Fixture/that_assert_true.php.inc | 31 +++++ .../config/configured_rule.php | 9 ++ .../AssertThatToDirectAssertRector.php | 107 ++++++++++++++++++ 5 files changed, 206 insertions(+) create mode 100644 rules-tests/CodeQuality/Rector/MethodCall/AssertThatToDirectAssertRector/AssertThatToDirectAssertRectorTest.php create mode 100644 rules-tests/CodeQuality/Rector/MethodCall/AssertThatToDirectAssertRector/Fixture/simple_assert_that.php.inc create mode 100644 rules-tests/CodeQuality/Rector/MethodCall/AssertThatToDirectAssertRector/Fixture/that_assert_true.php.inc create mode 100644 rules-tests/CodeQuality/Rector/MethodCall/AssertThatToDirectAssertRector/config/configured_rule.php create mode 100644 rules/CodeQuality/Rector/MethodCall/AssertThatToDirectAssertRector.php diff --git a/rules-tests/CodeQuality/Rector/MethodCall/AssertThatToDirectAssertRector/AssertThatToDirectAssertRectorTest.php b/rules-tests/CodeQuality/Rector/MethodCall/AssertThatToDirectAssertRector/AssertThatToDirectAssertRectorTest.php new file mode 100644 index 00000000..45dd79c0 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/MethodCall/AssertThatToDirectAssertRector/AssertThatToDirectAssertRectorTest.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/MethodCall/AssertThatToDirectAssertRector/Fixture/simple_assert_that.php.inc b/rules-tests/CodeQuality/Rector/MethodCall/AssertThatToDirectAssertRector/Fixture/simple_assert_that.php.inc new file mode 100644 index 00000000..1f5fa74a --- /dev/null +++ b/rules-tests/CodeQuality/Rector/MethodCall/AssertThatToDirectAssertRector/Fixture/simple_assert_that.php.inc @@ -0,0 +1,31 @@ +assertThat('value', $this->equalTo('expectedValue')); + } +} + +?> +----- +assertEquals('expectedValue', 'value'); + } +} + +?> diff --git a/rules-tests/CodeQuality/Rector/MethodCall/AssertThatToDirectAssertRector/Fixture/that_assert_true.php.inc b/rules-tests/CodeQuality/Rector/MethodCall/AssertThatToDirectAssertRector/Fixture/that_assert_true.php.inc new file mode 100644 index 00000000..7fcea43f --- /dev/null +++ b/rules-tests/CodeQuality/Rector/MethodCall/AssertThatToDirectAssertRector/Fixture/that_assert_true.php.inc @@ -0,0 +1,31 @@ +assertThat('value', $this->isTrue()); + } +} + +?> +----- +assertTrue('value'); + } +} + +?> diff --git a/rules-tests/CodeQuality/Rector/MethodCall/AssertThatToDirectAssertRector/config/configured_rule.php b/rules-tests/CodeQuality/Rector/MethodCall/AssertThatToDirectAssertRector/config/configured_rule.php new file mode 100644 index 00000000..b28f167a --- /dev/null +++ b/rules-tests/CodeQuality/Rector/MethodCall/AssertThatToDirectAssertRector/config/configured_rule.php @@ -0,0 +1,9 @@ +withRules([AssertThatToDirectAssertRector::class]); diff --git a/rules/CodeQuality/Rector/MethodCall/AssertThatToDirectAssertRector.php b/rules/CodeQuality/Rector/MethodCall/AssertThatToDirectAssertRector.php new file mode 100644 index 00000000..4bdae872 --- /dev/null +++ b/rules/CodeQuality/Rector/MethodCall/AssertThatToDirectAssertRector.php @@ -0,0 +1,107 @@ +assertThat($value, $this->*()) to direct $this->assert*() method', + [ + new CodeSample( + <<<'CODE_SAMPLE' +use PHPUnit\Framework\TestCase; + +final class SomeTest extends TestCase +{ + public function test() + { + $this->assertThat($value, $this->isTrue()); + } +} +CODE_SAMPLE + + , + <<<'CODE_SAMPLE' +use PHPUnit\Framework\TestCase; + +final class SomeTest extends TestCase +{ + public function test() + { + $this->assertTrue($value); + } +} +CODE_SAMPLE + ), + ] + ); + } + + /** + * @return array> + */ + public function getNodeTypes(): array + { + return [MethodCall::class]; + } + + /** + * @param MethodCall $node + */ + public function refactor(Node $node): ?Node + { + if ($node->isFirstClassCallable()) { + return null; + } + + if (! $this->testsNodeAnalyzer->isPHPUnitMethodCallNames($node, ['assertThat'])) { + return null; + } + + $secondArg = $node->getArgs()[1]; + if (! $secondArg->value instanceof MethodCall) { + return null; + } + + $exactAssertMethodCall = $secondArg->value; + $exactAssertName = $this->getName($exactAssertMethodCall->name); + + if ($exactAssertName === 'equalTo') { + $node->name = new Identifier('assertEquals'); + + $node->args[1] = $node->args[0]; + $node->args[0] = $exactAssertMethodCall->args[0]; + + return $node; + } + + if ($exactAssertName === 'isTrue') { + $node->name = new Identifier('assertTrue'); + unset($node->args[1]); + + return $node; + } + + return null; + } +}