mirror of
https://github.com/rectorphp/rector.git
synced 2025-04-13 03:52:15 +02:00
Updated Rector to commit a72a02188ab22b7beeb94dd1ffda79bdfffd64c4
a72a02188a
[Php84] Add rule for RoundingMode enum (#6369)
This commit is contained in:
parent
2601fd2c97
commit
79fb8fc282
@ -4,7 +4,8 @@ declare (strict_types=1);
|
||||
namespace RectorPrefix202412;
|
||||
|
||||
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]);
|
||||
};
|
||||
|
88
rules/Php84/Rector/FuncCall/RoundingModeEnumRector.php
Normal file
88
rules/Php84/Rector/FuncCall/RoundingModeEnumRector.php
Normal file
@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\Php84\Rector\FuncCall;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\ClassConstFetch;
|
||||
use PhpParser\Node\Expr\ConstFetch;
|
||||
use PhpParser\Node\Expr\FuncCall;
|
||||
use PhpParser\Node\Name\FullyQualified;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Rector\ValueObject\PhpVersionFeature;
|
||||
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
/**
|
||||
* @see \Rector\Tests\Php84\Rector\FuncCall\RoundingModeEnumRector\RoundingModeEnumRectorTest
|
||||
*/
|
||||
final class RoundingModeEnumRector extends AbstractRector implements MinPhpVersionInterface
|
||||
{
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Replace rounding mode constant to RoundMode enum in round()', [new CodeSample(<<<'CODE_SAMPLE'
|
||||
round(1.5, 0, PHP_ROUND_HALF_UP);
|
||||
CODE_SAMPLE
|
||||
, <<<'CODE_SAMPLE'
|
||||
round(1.5, 0, RoundingMode::HalfAwayFromZero);
|
||||
CODE_SAMPLE
|
||||
)]);
|
||||
}
|
||||
public function getNodeTypes() : array
|
||||
{
|
||||
return [FuncCall::class];
|
||||
}
|
||||
/**
|
||||
* @param FuncCall $node
|
||||
*/
|
||||
public function refactor(Node $node) : ?FuncCall
|
||||
{
|
||||
if (!$this->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 ConstFetch) {
|
||||
switch ($modeArg->name->toString()) {
|
||||
case 'PHP_ROUND_HALF_UP':
|
||||
$enumCase = 'HalfAwayFromZero';
|
||||
break;
|
||||
case 'PHP_ROUND_HALF_DOWN':
|
||||
$enumCase = 'HalfTowardsZero';
|
||||
break;
|
||||
case 'PHP_ROUND_HALF_EVEN':
|
||||
$enumCase = 'HalfEven';
|
||||
break;
|
||||
case 'PHP_ROUND_HALF_ODD':
|
||||
$enumCase = 'HalfOdd';
|
||||
break;
|
||||
default:
|
||||
$enumCase = null;
|
||||
break;
|
||||
}
|
||||
if ($enumCase === null) {
|
||||
return null;
|
||||
}
|
||||
$args[2]->value = new ClassConstFetch(new FullyQualified('RoundingMode'), $enumCase);
|
||||
$hasChanged = \true;
|
||||
}
|
||||
if ($hasChanged) {
|
||||
return $node;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public function provideMinPhpVersion() : int
|
||||
{
|
||||
return PhpVersionFeature::ROUNDING_MODES;
|
||||
}
|
||||
}
|
@ -19,12 +19,12 @@ final class VersionResolver
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = '6aca457745976acad6cec231ffc400f0d399a386';
|
||||
public const PACKAGE_VERSION = 'a72a02188ab22b7beeb94dd1ffda79bdfffd64c4';
|
||||
/**
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2024-12-19 15:58:19';
|
||||
public const RELEASE_DATE = '2024-12-20 07:14:41';
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
|
@ -555,6 +555,11 @@ final class PhpVersionFeature
|
||||
* @var int
|
||||
*/
|
||||
public const DEPRECATE_IMPLICIT_NULLABLE_PARAM_TYPE = \Rector\ValueObject\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 = \Rector\ValueObject\PhpVersion::PHP_84;
|
||||
/**
|
||||
* @see https://www.php.net/manual/en/migration83.deprecated.php#migration83.deprecated.ldap
|
||||
* @var int
|
||||
|
2
vendor/composer/autoload_classmap.php
vendored
2
vendor/composer/autoload_classmap.php
vendored
@ -1511,6 +1511,7 @@ return array(
|
||||
'Rector\\DowngradePhp82\\Rector\\Class_\\DowngradeReadonlyClassRector' => $vendorDir . '/rector/rector-downgrade-php/rules/DowngradePhp82/Rector/Class_/DowngradeReadonlyClassRector.php',
|
||||
'Rector\\DowngradePhp82\\Rector\\FunctionLike\\DowngradeStandaloneNullTrueFalseReturnTypeRector' => $vendorDir . '/rector/rector-downgrade-php/rules/DowngradePhp82/Rector/FunctionLike/DowngradeStandaloneNullTrueFalseReturnTypeRector.php',
|
||||
'Rector\\DowngradePhp83\\Rector\\ClassConst\\DowngradeTypedClassConstRector' => $vendorDir . '/rector/rector-downgrade-php/rules/DowngradePhp83/Rector/ClassConst/DowngradeTypedClassConstRector.php',
|
||||
'Rector\\DowngradePhp84\\Rector\\FuncCall\\DowngradeRoundingModeEnumRector' => $vendorDir . '/rector/rector-downgrade-php/rules/DowngradePhp84/Rector/FuncCall/DowngradeRoundingModeEnumRector.php',
|
||||
'Rector\\DowngradePhp84\\Rector\\MethodCall\\DowngradeNewMethodCallWithoutParenthesesRector' => $vendorDir . '/rector/rector-downgrade-php/rules/DowngradePhp84/Rector/MethodCall/DowngradeNewMethodCallWithoutParenthesesRector.php',
|
||||
'Rector\\EarlyReturn\\NodeTransformer\\ConditionInverter' => $baseDir . '/rules/EarlyReturn/NodeTransformer/ConditionInverter.php',
|
||||
'Rector\\EarlyReturn\\Rector\\Foreach_\\ChangeNestedForeachIfsToEarlyContinueRector' => $baseDir . '/rules/EarlyReturn/Rector/Foreach_/ChangeNestedForeachIfsToEarlyContinueRector.php',
|
||||
@ -2013,6 +2014,7 @@ return array(
|
||||
'Rector\\Php83\\Rector\\ClassMethod\\AddOverrideAttributeToOverriddenMethodsRector' => $baseDir . '/rules/Php83/Rector/ClassMethod/AddOverrideAttributeToOverriddenMethodsRector.php',
|
||||
'Rector\\Php83\\Rector\\FuncCall\\CombineHostPortLdapUriRector' => $baseDir . '/rules/Php83/Rector/FuncCall/CombineHostPortLdapUriRector.php',
|
||||
'Rector\\Php83\\Rector\\FuncCall\\RemoveGetClassGetParentClassNoArgsRector' => $baseDir . '/rules/Php83/Rector/FuncCall/RemoveGetClassGetParentClassNoArgsRector.php',
|
||||
'Rector\\Php84\\Rector\\FuncCall\\RoundingModeEnumRector' => $baseDir . '/rules/Php84/Rector/FuncCall/RoundingModeEnumRector.php',
|
||||
'Rector\\Php84\\Rector\\Param\\ExplicitNullableParamTypeRector' => $baseDir . '/rules/Php84/Rector/Param/ExplicitNullableParamTypeRector.php',
|
||||
'Rector\\PhpAttribute\\AnnotationToAttributeMapper' => $baseDir . '/src/PhpAttribute/AnnotationToAttributeMapper.php',
|
||||
'Rector\\PhpAttribute\\AnnotationToAttributeMapper\\ArrayAnnotationToAttributeMapper' => $baseDir . '/src/PhpAttribute/AnnotationToAttributeMapper/ArrayAnnotationToAttributeMapper.php',
|
||||
|
2
vendor/composer/autoload_static.php
vendored
2
vendor/composer/autoload_static.php
vendored
@ -1730,6 +1730,7 @@ class ComposerStaticInite1459f150f08d0eee2804dfc37f818db
|
||||
'Rector\\DowngradePhp82\\Rector\\Class_\\DowngradeReadonlyClassRector' => __DIR__ . '/..' . '/rector/rector-downgrade-php/rules/DowngradePhp82/Rector/Class_/DowngradeReadonlyClassRector.php',
|
||||
'Rector\\DowngradePhp82\\Rector\\FunctionLike\\DowngradeStandaloneNullTrueFalseReturnTypeRector' => __DIR__ . '/..' . '/rector/rector-downgrade-php/rules/DowngradePhp82/Rector/FunctionLike/DowngradeStandaloneNullTrueFalseReturnTypeRector.php',
|
||||
'Rector\\DowngradePhp83\\Rector\\ClassConst\\DowngradeTypedClassConstRector' => __DIR__ . '/..' . '/rector/rector-downgrade-php/rules/DowngradePhp83/Rector/ClassConst/DowngradeTypedClassConstRector.php',
|
||||
'Rector\\DowngradePhp84\\Rector\\FuncCall\\DowngradeRoundingModeEnumRector' => __DIR__ . '/..' . '/rector/rector-downgrade-php/rules/DowngradePhp84/Rector/FuncCall/DowngradeRoundingModeEnumRector.php',
|
||||
'Rector\\DowngradePhp84\\Rector\\MethodCall\\DowngradeNewMethodCallWithoutParenthesesRector' => __DIR__ . '/..' . '/rector/rector-downgrade-php/rules/DowngradePhp84/Rector/MethodCall/DowngradeNewMethodCallWithoutParenthesesRector.php',
|
||||
'Rector\\EarlyReturn\\NodeTransformer\\ConditionInverter' => __DIR__ . '/../..' . '/rules/EarlyReturn/NodeTransformer/ConditionInverter.php',
|
||||
'Rector\\EarlyReturn\\Rector\\Foreach_\\ChangeNestedForeachIfsToEarlyContinueRector' => __DIR__ . '/../..' . '/rules/EarlyReturn/Rector/Foreach_/ChangeNestedForeachIfsToEarlyContinueRector.php',
|
||||
@ -2232,6 +2233,7 @@ class ComposerStaticInite1459f150f08d0eee2804dfc37f818db
|
||||
'Rector\\Php83\\Rector\\ClassMethod\\AddOverrideAttributeToOverriddenMethodsRector' => __DIR__ . '/../..' . '/rules/Php83/Rector/ClassMethod/AddOverrideAttributeToOverriddenMethodsRector.php',
|
||||
'Rector\\Php83\\Rector\\FuncCall\\CombineHostPortLdapUriRector' => __DIR__ . '/../..' . '/rules/Php83/Rector/FuncCall/CombineHostPortLdapUriRector.php',
|
||||
'Rector\\Php83\\Rector\\FuncCall\\RemoveGetClassGetParentClassNoArgsRector' => __DIR__ . '/../..' . '/rules/Php83/Rector/FuncCall/RemoveGetClassGetParentClassNoArgsRector.php',
|
||||
'Rector\\Php84\\Rector\\FuncCall\\RoundingModeEnumRector' => __DIR__ . '/../..' . '/rules/Php84/Rector/FuncCall/RoundingModeEnumRector.php',
|
||||
'Rector\\Php84\\Rector\\Param\\ExplicitNullableParamTypeRector' => __DIR__ . '/../..' . '/rules/Php84/Rector/Param/ExplicitNullableParamTypeRector.php',
|
||||
'Rector\\PhpAttribute\\AnnotationToAttributeMapper' => __DIR__ . '/../..' . '/src/PhpAttribute/AnnotationToAttributeMapper.php',
|
||||
'Rector\\PhpAttribute\\AnnotationToAttributeMapper\\ArrayAnnotationToAttributeMapper' => __DIR__ . '/../..' . '/src/PhpAttribute/AnnotationToAttributeMapper/ArrayAnnotationToAttributeMapper.php',
|
||||
|
8
vendor/composer/installed.json
vendored
8
vendor/composer/installed.json
vendored
@ -1752,12 +1752,12 @@
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https:\/\/github.com\/rectorphp\/rector-downgrade-php.git",
|
||||
"reference": "7b990bd35788fc6b62645d3dd8335871e4ba4e77"
|
||||
"reference": "183795c0743e88eb8be6297738df75fb2bde56fc"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https:\/\/api.github.com\/repos\/rectorphp\/rector-downgrade-php\/zipball\/7b990bd35788fc6b62645d3dd8335871e4ba4e77",
|
||||
"reference": "7b990bd35788fc6b62645d3dd8335871e4ba4e77",
|
||||
"url": "https:\/\/api.github.com\/repos\/rectorphp\/rector-downgrade-php\/zipball\/183795c0743e88eb8be6297738df75fb2bde56fc",
|
||||
"reference": "183795c0743e88eb8be6297738df75fb2bde56fc",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
@ -1775,7 +1775,7 @@
|
||||
"tomasvotruba\/class-leak": "^1.0",
|
||||
"tracy\/tracy": "^2.10"
|
||||
},
|
||||
"time": "2024-12-16T14:58:05+00:00",
|
||||
"time": "2024-12-20T00:07:21+00:00",
|
||||
"default-branch": true,
|
||||
"type": "rector-extension",
|
||||
"extra": {
|
||||
|
2
vendor/composer/installed.php
vendored
2
vendor/composer/installed.php
vendored
File diff suppressed because one or more lines are too long
@ -9,7 +9,7 @@ namespace Rector\RectorInstaller;
|
||||
*/
|
||||
final class GeneratedConfig
|
||||
{
|
||||
public const EXTENSIONS = array('rector/rector-doctrine' => array('install_path' => '/home/runner/work/rector-src/rector-src/rector-build/vendor/rector/rector-doctrine', 'relative_install_path' => '../../rector-doctrine', 'extra' => NULL, 'version' => 'dev-main a0ab15c'), 'rector/rector-downgrade-php' => array('install_path' => '/home/runner/work/rector-src/rector-src/rector-build/vendor/rector/rector-downgrade-php', 'relative_install_path' => '../../rector-downgrade-php', 'extra' => NULL, 'version' => 'dev-main 7b990bd'), 'rector/rector-phpunit' => array('install_path' => '/home/runner/work/rector-src/rector-src/rector-build/vendor/rector/rector-phpunit', 'relative_install_path' => '../../rector-phpunit', 'extra' => NULL, 'version' => 'dev-main 4bd1f6e'), 'rector/rector-symfony' => array('install_path' => '/home/runner/work/rector-src/rector-src/rector-build/vendor/rector/rector-symfony', 'relative_install_path' => '../../rector-symfony', 'extra' => NULL, 'version' => 'dev-main 29a1abf'));
|
||||
public const EXTENSIONS = array('rector/rector-doctrine' => array('install_path' => '/home/runner/work/rector-src/rector-src/rector-build/vendor/rector/rector-doctrine', 'relative_install_path' => '../../rector-doctrine', 'extra' => NULL, 'version' => 'dev-main a0ab15c'), 'rector/rector-downgrade-php' => array('install_path' => '/home/runner/work/rector-src/rector-src/rector-build/vendor/rector/rector-downgrade-php', 'relative_install_path' => '../../rector-downgrade-php', 'extra' => NULL, 'version' => 'dev-main 183795c'), 'rector/rector-phpunit' => array('install_path' => '/home/runner/work/rector-src/rector-src/rector-build/vendor/rector/rector-phpunit', 'relative_install_path' => '../../rector-phpunit', 'extra' => NULL, 'version' => 'dev-main 4bd1f6e'), 'rector/rector-symfony' => array('install_path' => '/home/runner/work/rector-src/rector-src/rector-build/vendor/rector/rector-symfony', 'relative_install_path' => '../../rector-symfony', 'extra' => NULL, 'version' => 'dev-main 29a1abf'));
|
||||
private function __construct()
|
||||
{
|
||||
}
|
||||
|
@ -4,9 +4,10 @@ declare (strict_types=1);
|
||||
namespace RectorPrefix202412;
|
||||
|
||||
use Rector\Config\RectorConfig;
|
||||
use Rector\DowngradePhp84\Rector\FuncCall\DowngradeRoundingModeEnumRector;
|
||||
use Rector\DowngradePhp84\Rector\MethodCall\DowngradeNewMethodCallWithoutParenthesesRector;
|
||||
use Rector\ValueObject\PhpVersion;
|
||||
return static function (RectorConfig $rectorConfig) : void {
|
||||
$rectorConfig->phpVersion(PhpVersion::PHP_83);
|
||||
$rectorConfig->rules([DowngradeNewMethodCallWithoutParenthesesRector::class]);
|
||||
$rectorConfig->rules([DowngradeNewMethodCallWithoutParenthesesRector::class, DowngradeRoundingModeEnumRector::class]);
|
||||
};
|
||||
|
@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\DowngradePhp84\Rector\FuncCall;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\ClassConstFetch;
|
||||
use PhpParser\Node\Expr\ConstFetch;
|
||||
use PhpParser\Node\Expr\FuncCall;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Identifier;
|
||||
use PhpParser\Node\Name\FullyQualified;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
/**
|
||||
* @changelog wiki.php.net/rfc/correctly_name_the_rounding_mode_and_make_it_an_enum
|
||||
*
|
||||
* @see \Rector\Tests\DowngradePhp84\Rector\FuncCall\DowngradeRoundingModeEnumRector\DowngradeRoundingModeEnumRectorTest
|
||||
*/
|
||||
final class DowngradeRoundingModeEnumRector extends AbstractRector
|
||||
{
|
||||
public function getNodeTypes() : array
|
||||
{
|
||||
return [FuncCall::class];
|
||||
}
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Replace RoundingMode enum to rounding mode constant in round()', [new CodeSample(<<<'CODE_SAMPLE'
|
||||
round(1.5, 0, RoundingMode::HalfAwayFromZero);
|
||||
CODE_SAMPLE
|
||||
, <<<'CODE_SAMPLE'
|
||||
round(1.5, 0, PHP_ROUND_HALF_UP);
|
||||
CODE_SAMPLE
|
||||
)]);
|
||||
}
|
||||
/**
|
||||
* @param MethodCall $node
|
||||
*/
|
||||
public function refactor(Node $node) : ?Node
|
||||
{
|
||||
if (!$this->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;
|
||||
}
|
||||
switch ($modeArg->name->name) {
|
||||
case 'HalfAwayFromZero':
|
||||
$constantName = 'PHP_ROUND_HALF_UP';
|
||||
break;
|
||||
case 'HalfTowardsZero':
|
||||
$constantName = 'PHP_ROUND_HALF_DOWN';
|
||||
break;
|
||||
case 'HalfEven':
|
||||
$constantName = 'PHP_ROUND_HALF_EVEN';
|
||||
break;
|
||||
case 'HalfOdd':
|
||||
$constantName = 'PHP_ROUND_HALF_ODD';
|
||||
break;
|
||||
default:
|
||||
$constantName = null;
|
||||
break;
|
||||
}
|
||||
if ($constantName === null) {
|
||||
return null;
|
||||
}
|
||||
$args[2]->value = new ConstFetch(new FullyQualified($constantName));
|
||||
$hasChanged = \true;
|
||||
}
|
||||
if ($hasChanged) {
|
||||
return $node;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user