Updated Rector to commit fadb6a4c4978009293099f56f4ed3ae3c0775321

fadb6a4c49 [Php74] Add MoneyFormatToNumberFormatRector (#2727)
This commit is contained in:
Tomas Votruba 2022-08-24 20:17:59 +00:00
parent 9badddc299
commit f5e65b1dfa
8 changed files with 139 additions and 14 deletions

View File

@ -12,6 +12,7 @@ use Rector\Php74\Rector\FuncCall\ArrayKeyExistsOnPropertyRector;
use Rector\Php74\Rector\FuncCall\ArraySpreadInsteadOfArrayMergeRector;
use Rector\Php74\Rector\FuncCall\FilterVarToAddSlashesRector;
use Rector\Php74\Rector\FuncCall\MbStrrposEncodingArgumentPositionRector;
use Rector\Php74\Rector\FuncCall\MoneyFormatToNumberFormatRector;
use Rector\Php74\Rector\LNumber\AddLiteralSeparatorToNumberRector;
use Rector\Php74\Rector\MethodCall\ChangeReflectionTypeToStringToGetNameRector;
use Rector\Php74\Rector\Property\RestoreDefaultNullToNullableTypePropertyRector;
@ -40,4 +41,5 @@ return static function (RectorConfig $rectorConfig) : void {
$rectorConfig->rule(ChangeReflectionTypeToStringToGetNameRector::class);
$rectorConfig->rule(RestoreDefaultNullToNullableTypePropertyRector::class);
$rectorConfig->rule(CurlyToSquareBracketArrayStringRector::class);
$rectorConfig->rule(MoneyFormatToNumberFormatRector::class);
};

View File

@ -0,0 +1,117 @@
<?php
declare (strict_types=1);
namespace Rector\Php74\Rector\FuncCall;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Expression;
use PHPStan\Analyser\Scope;
use Rector\Core\NodeAnalyzer\ArgsAnalyzer;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\Naming\Naming\VariableNaming;
use Rector\PostRector\Collector\NodesToAddCollector;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @changelog https://www.php.net/manual/en/function.money-format.php#warning
*
* @see \Rector\Tests\Php74\Rector\FuncCall\MoneyFormatToNumberFormatRector\MoneyFormatToNumberFormatRectorTest
*/
final class MoneyFormatToNumberFormatRector extends AbstractScopeAwareRector implements MinPhpVersionInterface
{
/**
* @var string[]
*/
private const FORMATS = ['%i'];
/**
* @readonly
* @var \Rector\Core\NodeAnalyzer\ArgsAnalyzer
*/
private $argsAnalyzer;
/**
* @readonly
* @var \Rector\PostRector\Collector\NodesToAddCollector
*/
private $nodesToAddCollector;
/**
* @readonly
* @var \Rector\Naming\Naming\VariableNaming
*/
private $variableNaming;
public function __construct(ArgsAnalyzer $argsAnalyzer, NodesToAddCollector $nodesToAddCollector, VariableNaming $variableNaming)
{
$this->argsAnalyzer = $argsAnalyzer;
$this->nodesToAddCollector = $nodesToAddCollector;
$this->variableNaming = $variableNaming;
}
public function provideMinPhpVersion() : int
{
return PhpVersionFeature::DEPRECATE_MONEY_FORMAT;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Change money_format() to equivalent number_format()', [new CodeSample(<<<'CODE_SAMPLE'
$value = money_format('%i', $value);
CODE_SAMPLE
, <<<'CODE_SAMPLE'
$roundedValue = round($value, 2, PHP_ROUND_HALF_ODD);
$value = number_format($roundedValue, 2, '.', '');
CODE_SAMPLE
)]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [FuncCall::class];
}
/**
* @param FuncCall $node
*/
public function refactorWithScope(Node $node, Scope $scope) : ?Node
{
if (!$this->isName($node, 'money_format')) {
return null;
}
$args = $node->getArgs();
if ($this->argsAnalyzer->hasNamedArg($args)) {
return null;
}
$formatValue = $args[0]->value;
foreach (self::FORMATS as $format) {
if ($this->valueResolver->isValue($formatValue, $format)) {
return $this->resolveNumberFormat($node, $args[1]->value, $scope);
}
}
return null;
}
private function resolveNumberFormat(FuncCall $funcCall, Expr $expr, Scope $scope) : ?FuncCall
{
$currentStmt = $this->betterNodeFinder->resolveCurrentStatement($funcCall);
if (!$currentStmt instanceof Stmt) {
return null;
}
$newValue = $this->nodeFactory->createFuncCall('round', [$expr, new LNumber(2), new ConstFetch(new Name('PHP_ROUND_HALF_ODD'))]);
$variable = new Variable($this->variableNaming->createCountedValueName('roundedValue', $scope));
$this->nodesToAddCollector->addNodeBeforeNode(new Expression(new Assign($variable, $newValue)), $currentStmt);
$funcCall->name = new Name('number_format');
$funcCall->args[0] = new Arg($variable);
$funcCall->args[1] = new Arg(new LNumber(2));
$funcCall->args[2] = new Arg(new String_('.'));
$funcCall->args[3] = new Arg(new String_(''));
return $funcCall;
}
}

View File

@ -17,12 +17,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = 'd894cccb8535fea0ad8ab93ead4a4383c81d56b8';
public const PACKAGE_VERSION = 'fadb6a4c4978009293099f56f4ed3ae3c0775321';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2022-08-24 13:59:38';
public const RELEASE_DATE = '2022-08-24 22:11:48';
/**
* @var int
*/

View File

@ -292,6 +292,10 @@ final class PhpVersionFeature
* @var int
*/
public const DEPRECATE_REAL = \Rector\Core\ValueObject\PhpVersion::PHP_74;
/**
* @var int
*/
public const DEPRECATE_MONEY_FORMAT = \Rector\Core\ValueObject\PhpVersion::PHP_74;
/**
* @var int
*/

2
vendor/autoload.php vendored
View File

@ -9,4 +9,4 @@ if (PHP_VERSION_ID < 50600) {
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit96d9ee8278804cd2938c50141f02b5d8::getLoader();
return ComposerAutoloaderInitbbb1b97d03c416707c299382ac40acf9::getLoader();

View File

@ -2342,6 +2342,7 @@ return array(
'Rector\\Php74\\Rector\\FuncCall\\ArraySpreadInsteadOfArrayMergeRector' => $baseDir . '/rules/Php74/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector.php',
'Rector\\Php74\\Rector\\FuncCall\\FilterVarToAddSlashesRector' => $baseDir . '/rules/Php74/Rector/FuncCall/FilterVarToAddSlashesRector.php',
'Rector\\Php74\\Rector\\FuncCall\\MbStrrposEncodingArgumentPositionRector' => $baseDir . '/rules/Php74/Rector/FuncCall/MbStrrposEncodingArgumentPositionRector.php',
'Rector\\Php74\\Rector\\FuncCall\\MoneyFormatToNumberFormatRector' => $baseDir . '/rules/Php74/Rector/FuncCall/MoneyFormatToNumberFormatRector.php',
'Rector\\Php74\\Rector\\LNumber\\AddLiteralSeparatorToNumberRector' => $baseDir . '/rules/Php74/Rector/LNumber/AddLiteralSeparatorToNumberRector.php',
'Rector\\Php74\\Rector\\MethodCall\\ChangeReflectionTypeToStringToGetNameRector' => $baseDir . '/rules/Php74/Rector/MethodCall/ChangeReflectionTypeToStringToGetNameRector.php',
'Rector\\Php74\\Rector\\Property\\RestoreDefaultNullToNullableTypePropertyRector' => $baseDir . '/rules/Php74/Rector/Property/RestoreDefaultNullToNullableTypePropertyRector.php',

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit96d9ee8278804cd2938c50141f02b5d8
class ComposerAutoloaderInitbbb1b97d03c416707c299382ac40acf9
{
private static $loader;
@ -22,19 +22,19 @@ class ComposerAutoloaderInit96d9ee8278804cd2938c50141f02b5d8
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit96d9ee8278804cd2938c50141f02b5d8', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInitbbb1b97d03c416707c299382ac40acf9', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInit96d9ee8278804cd2938c50141f02b5d8', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInitbbb1b97d03c416707c299382ac40acf9', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit96d9ee8278804cd2938c50141f02b5d8::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInitbbb1b97d03c416707c299382ac40acf9::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$includeFiles = \Composer\Autoload\ComposerStaticInit96d9ee8278804cd2938c50141f02b5d8::$files;
$includeFiles = \Composer\Autoload\ComposerStaticInitbbb1b97d03c416707c299382ac40acf9::$files;
foreach ($includeFiles as $fileIdentifier => $file) {
composerRequire96d9ee8278804cd2938c50141f02b5d8($fileIdentifier, $file);
composerRequirebbb1b97d03c416707c299382ac40acf9($fileIdentifier, $file);
}
return $loader;
@ -46,7 +46,7 @@ class ComposerAutoloaderInit96d9ee8278804cd2938c50141f02b5d8
* @param string $file
* @return void
*/
function composerRequire96d9ee8278804cd2938c50141f02b5d8($fileIdentifier, $file)
function composerRequirebbb1b97d03c416707c299382ac40acf9($fileIdentifier, $file)
{
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;

View File

@ -4,7 +4,7 @@
namespace Composer\Autoload;
class ComposerStaticInit96d9ee8278804cd2938c50141f02b5d8
class ComposerStaticInitbbb1b97d03c416707c299382ac40acf9
{
public static $files = array (
'0e6d7bf4a5811bfa5cf40c5ccd6fae6a' => __DIR__ . '/..' . '/symfony/polyfill-mbstring/bootstrap.php',
@ -2639,6 +2639,7 @@ class ComposerStaticInit96d9ee8278804cd2938c50141f02b5d8
'Rector\\Php74\\Rector\\FuncCall\\ArraySpreadInsteadOfArrayMergeRector' => __DIR__ . '/../..' . '/rules/Php74/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector.php',
'Rector\\Php74\\Rector\\FuncCall\\FilterVarToAddSlashesRector' => __DIR__ . '/../..' . '/rules/Php74/Rector/FuncCall/FilterVarToAddSlashesRector.php',
'Rector\\Php74\\Rector\\FuncCall\\MbStrrposEncodingArgumentPositionRector' => __DIR__ . '/../..' . '/rules/Php74/Rector/FuncCall/MbStrrposEncodingArgumentPositionRector.php',
'Rector\\Php74\\Rector\\FuncCall\\MoneyFormatToNumberFormatRector' => __DIR__ . '/../..' . '/rules/Php74/Rector/FuncCall/MoneyFormatToNumberFormatRector.php',
'Rector\\Php74\\Rector\\LNumber\\AddLiteralSeparatorToNumberRector' => __DIR__ . '/../..' . '/rules/Php74/Rector/LNumber/AddLiteralSeparatorToNumberRector.php',
'Rector\\Php74\\Rector\\MethodCall\\ChangeReflectionTypeToStringToGetNameRector' => __DIR__ . '/../..' . '/rules/Php74/Rector/MethodCall/ChangeReflectionTypeToStringToGetNameRector.php',
'Rector\\Php74\\Rector\\Property\\RestoreDefaultNullToNullableTypePropertyRector' => __DIR__ . '/../..' . '/rules/Php74/Rector/Property/RestoreDefaultNullToNullableTypePropertyRector.php',
@ -3255,9 +3256,9 @@ class ComposerStaticInit96d9ee8278804cd2938c50141f02b5d8
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit96d9ee8278804cd2938c50141f02b5d8::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit96d9ee8278804cd2938c50141f02b5d8::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit96d9ee8278804cd2938c50141f02b5d8::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInitbbb1b97d03c416707c299382ac40acf9::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInitbbb1b97d03c416707c299382ac40acf9::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInitbbb1b97d03c416707c299382ac40acf9::$classMap;
}, null, ClassLoader::class);
}