diff --git a/config/set/php84.php b/config/set/php84.php index 8a015ad0d4c..3231419d58a 100644 --- a/config/set/php84.php +++ b/config/set/php84.php @@ -3,8 +3,9 @@ declare(strict_types=1); use Rector\Config\RectorConfig; +use Rector\Php84\Rector\FuncCall\RoundingModeEnumRector; use Rector\Php84\Rector\Param\ExplicitNullableParamTypeRector; return static function (RectorConfig $rectorConfig): void { - $rectorConfig->rules([ExplicitNullableParamTypeRector::class]); + $rectorConfig->rules([ExplicitNullableParamTypeRector::class, RoundingModeEnumRector::class]); }; diff --git a/rules-tests/Php84/Rector/FuncCall/RoundingModeEnumRector/Fixture/rounding_constants.php.inc b/rules-tests/Php84/Rector/FuncCall/RoundingModeEnumRector/Fixture/rounding_constants.php.inc new file mode 100644 index 00000000000..2a70b9d247d --- /dev/null +++ b/rules-tests/Php84/Rector/FuncCall/RoundingModeEnumRector/Fixture/rounding_constants.php.inc @@ -0,0 +1,29 @@ + +----- + diff --git a/rules-tests/Php84/Rector/FuncCall/RoundingModeEnumRector/Fixture/skip_indirect_use_of_constants.php.inc b/rules-tests/Php84/Rector/FuncCall/RoundingModeEnumRector/Fixture/skip_indirect_use_of_constants.php.inc new file mode 100644 index 00000000000..be3441a4fa3 --- /dev/null +++ b/rules-tests/Php84/Rector/FuncCall/RoundingModeEnumRector/Fixture/skip_indirect_use_of_constants.php.inc @@ -0,0 +1,10 @@ + diff --git a/rules-tests/Php84/Rector/FuncCall/RoundingModeEnumRector/RoundingModeEnumRectorTest.php b/rules-tests/Php84/Rector/FuncCall/RoundingModeEnumRector/RoundingModeEnumRectorTest.php new file mode 100644 index 00000000000..51c2e48eb1f --- /dev/null +++ b/rules-tests/Php84/Rector/FuncCall/RoundingModeEnumRector/RoundingModeEnumRectorTest.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/Php84/Rector/FuncCall/RoundingModeEnumRector/config/configured_rule.php b/rules-tests/Php84/Rector/FuncCall/RoundingModeEnumRector/config/configured_rule.php new file mode 100644 index 00000000000..68ca3b74849 --- /dev/null +++ b/rules-tests/Php84/Rector/FuncCall/RoundingModeEnumRector/config/configured_rule.php @@ -0,0 +1,13 @@ +rule(RoundingModeEnumRector::class); + + $rectorConfig->phpVersion(PhpVersion::PHP_84); +}; diff --git a/rules/Php84/Rector/FuncCall/RoundingModeEnumRector.php b/rules/Php84/Rector/FuncCall/RoundingModeEnumRector.php new file mode 100644 index 00000000000..1ed3e08e5ea --- /dev/null +++ b/rules/Php84/Rector/FuncCall/RoundingModeEnumRector.php @@ -0,0 +1,93 @@ +isName($node, 'round')) { + return null; + } + + if ($node->isFirstClassCallable()) { + return null; + } + + $args = $node->getArgs(); + + if (count($args) !== 3) { + return null; + } + + $modeArg = $args[2]->value; + + if ($modeArg instanceof ConstFetch) { + if (isset($modeArg->name->getParts()[0])) { + $enumCase = match ($modeArg->name->getParts()[0]) { + 'PHP_ROUND_HALF_UP' => 'HalfAwayFromZero', + 'PHP_ROUND_HALF_DOWN' => 'HalfTowardsZero', + 'PHP_ROUND_HALF_EVEN' => 'HalfEven', + 'PHP_ROUND_HALF_ODD' => 'HalfOdd', + default => null, + }; + + if ($enumCase === null) { + return null; + } + + $args[2]->value = new Node\Expr\ClassConstFetch(new FullyQualified('RoundingMode'), $enumCase); + } + } + + return $node; + } + + public function provideMinPhpVersion(): int + { + return PhpVersionFeature::ROUNDING_MODES; + } +} diff --git a/src/ValueObject/PhpVersionFeature.php b/src/ValueObject/PhpVersionFeature.php index 86de5ba52f7..5ee796b58ae 100644 --- a/src/ValueObject/PhpVersionFeature.php +++ b/src/ValueObject/PhpVersionFeature.php @@ -678,6 +678,12 @@ final class PhpVersionFeature */ public const DEPRECATE_IMPLICIT_NULLABLE_PARAM_TYPE = PhpVersion::PHP_84; + /** + * @see https://wiki.php.net/rfc/correctly_name_the_rounding_mode_and_make_it_an_enum + * @var int + */ + public const ROUNDING_MODES = PhpVersion::PHP_84; + /** * @see https://www.php.net/manual/en/migration83.deprecated.php#migration83.deprecated.ldap * @var int