diff --git a/config/set/downgrade-php84.php b/config/set/downgrade-php84.php index a5d80aad..d3d914cc 100644 --- a/config/set/downgrade-php84.php +++ b/config/set/downgrade-php84.php @@ -3,6 +3,7 @@ declare(strict_types=1); use Rector\Config\RectorConfig; +use Rector\DowngradePhp84\Rector\FuncCall\DowngradeRoundingModeEnumRector; use Rector\DowngradePhp84\Rector\MethodCall\DowngradeNewMethodCallWithoutParenthesesRector; use Rector\ValueObject\PhpVersion; @@ -10,5 +11,6 @@ $rectorConfig->phpVersion(PhpVersion::PHP_83); $rectorConfig->rules([ DowngradeNewMethodCallWithoutParenthesesRector::class, + DowngradeRoundingModeEnumRector::class, ]); }; diff --git a/rules-tests/DowngradePhp84/Rector/FuncCall/DowngradeRoundingModeEnumRector/DowngradeRoundingModeEnumRectorTest.php b/rules-tests/DowngradePhp84/Rector/FuncCall/DowngradeRoundingModeEnumRector/DowngradeRoundingModeEnumRectorTest.php new file mode 100644 index 00000000..312fbddf --- /dev/null +++ b/rules-tests/DowngradePhp84/Rector/FuncCall/DowngradeRoundingModeEnumRector/DowngradeRoundingModeEnumRectorTest.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/DowngradePhp84/Rector/FuncCall/DowngradeRoundingModeEnumRector/Fixture/fixture.php.inc b/rules-tests/DowngradePhp84/Rector/FuncCall/DowngradeRoundingModeEnumRector/Fixture/fixture.php.inc new file mode 100644 index 00000000..7aa68729 --- /dev/null +++ b/rules-tests/DowngradePhp84/Rector/FuncCall/DowngradeRoundingModeEnumRector/Fixture/fixture.php.inc @@ -0,0 +1,37 @@ + +----- + diff --git a/rules-tests/DowngradePhp84/Rector/FuncCall/DowngradeRoundingModeEnumRector/Fixture/from_use.php.inc b/rules-tests/DowngradePhp84/Rector/FuncCall/DowngradeRoundingModeEnumRector/Fixture/from_use.php.inc new file mode 100644 index 00000000..7a4178a4 --- /dev/null +++ b/rules-tests/DowngradePhp84/Rector/FuncCall/DowngradeRoundingModeEnumRector/Fixture/from_use.php.inc @@ -0,0 +1,19 @@ + +----- + diff --git a/rules-tests/DowngradePhp84/Rector/FuncCall/DowngradeRoundingModeEnumRector/Fixture/no_namespace.php.inc b/rules-tests/DowngradePhp84/Rector/FuncCall/DowngradeRoundingModeEnumRector/Fixture/no_namespace.php.inc new file mode 100644 index 00000000..47d17f3b --- /dev/null +++ b/rules-tests/DowngradePhp84/Rector/FuncCall/DowngradeRoundingModeEnumRector/Fixture/no_namespace.php.inc @@ -0,0 +1,11 @@ + +----- + diff --git a/rules-tests/DowngradePhp84/Rector/FuncCall/DowngradeRoundingModeEnumRector/Fixture/skip_indirect_use_of_roundin_mode.php.inc b/rules-tests/DowngradePhp84/Rector/FuncCall/DowngradeRoundingModeEnumRector/Fixture/skip_indirect_use_of_roundin_mode.php.inc new file mode 100644 index 00000000..3d095cef --- /dev/null +++ b/rules-tests/DowngradePhp84/Rector/FuncCall/DowngradeRoundingModeEnumRector/Fixture/skip_indirect_use_of_roundin_mode.php.inc @@ -0,0 +1,10 @@ + diff --git a/rules-tests/DowngradePhp84/Rector/FuncCall/DowngradeRoundingModeEnumRector/config/configured_rule.php b/rules-tests/DowngradePhp84/Rector/FuncCall/DowngradeRoundingModeEnumRector/config/configured_rule.php new file mode 100644 index 00000000..27680d6a --- /dev/null +++ b/rules-tests/DowngradePhp84/Rector/FuncCall/DowngradeRoundingModeEnumRector/config/configured_rule.php @@ -0,0 +1,10 @@ +rule(DowngradeRoundingModeEnumRector::class); +}; diff --git a/rules/DowngradePhp84/Rector/FuncCall/DowngradeRoundingModeEnumRector.php b/rules/DowngradePhp84/Rector/FuncCall/DowngradeRoundingModeEnumRector.php new file mode 100644 index 00000000..f6786555 --- /dev/null +++ b/rules/DowngradePhp84/Rector/FuncCall/DowngradeRoundingModeEnumRector.php @@ -0,0 +1,102 @@ +isName($node, 'round')) { + return null; + } + + if ($node->isFirstClassCallable()) { + return null; + } + + $args = $node->getArgs(); + + if (count($args) !== 3) { + return null; + } + + if (! isset($args[2])) { + return null; + } + + $modeArg = $args[2]->value; + $hasChanged = false; + if ($modeArg instanceof ClassConstFetch && $modeArg->class instanceof FullyQualified && $this->isName( + $modeArg->class, + 'RoundingMode' + )) { + if (! $modeArg->name instanceof Identifier) { + return null; + } + + $constantName = match ($modeArg->name->name) { + 'HalfAwayFromZero' => 'PHP_ROUND_HALF_UP', + 'HalfTowardsZero' => 'PHP_ROUND_HALF_DOWN', + 'HalfEven' => 'PHP_ROUND_HALF_EVEN', + 'HalfOdd' => 'PHP_ROUND_HALF_ODD', + default => null, + }; + if ($constantName === null) { + return null; + } + + $args[2]->value = new ConstFetch(new FullyQualified($constantName)); + $hasChanged = true; + } + + if ($hasChanged) { + return $node; + } + + return null; + } +}