mirror of
https://github.com/rectorphp/rector.git
synced 2024-05-28 15:00:50 +00:00
Updated Rector to commit 1e0a1ceb897de085e3b8a68fbf88b4028d2064b0
1e0a1ceb89
Return nodes directly in MoneyFormatToNumberFormatRector (#3986)
This commit is contained in:
parent
466c54de06
commit
c613309b06
|
@ -15,12 +15,12 @@ use PhpParser\Node\Scalar\LNumber;
|
|||
use PhpParser\Node\Scalar\String_;
|
||||
use PhpParser\Node\Stmt;
|
||||
use PhpParser\Node\Stmt\Expression;
|
||||
use PhpParser\Node\Stmt\Return_;
|
||||
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;
|
||||
|
@ -31,29 +31,19 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|||
*/
|
||||
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)
|
||||
public function __construct(ArgsAnalyzer $argsAnalyzer, VariableNaming $variableNaming)
|
||||
{
|
||||
$this->argsAnalyzer = $argsAnalyzer;
|
||||
$this->nodesToAddCollector = $nodesToAddCollector;
|
||||
$this->variableNaming = $variableNaming;
|
||||
}
|
||||
public function provideMinPhpVersion() : int
|
||||
|
@ -76,42 +66,59 @@ CODE_SAMPLE
|
|||
*/
|
||||
public function getNodeTypes() : array
|
||||
{
|
||||
return [FuncCall::class];
|
||||
return [Expression::class, Return_::class];
|
||||
}
|
||||
/**
|
||||
* @param FuncCall $node
|
||||
* @param Expression|Return_ $node
|
||||
* @return Stmt[]|null
|
||||
*/
|
||||
public function refactorWithScope(Node $node, Scope $scope) : ?Node
|
||||
public function refactorWithScope(Node $node, Scope $scope) : ?array
|
||||
{
|
||||
if (!$this->isName($node, 'money_format')) {
|
||||
$funcCall = $this->matchFunCall($node);
|
||||
if (!$funcCall instanceof FuncCall) {
|
||||
return null;
|
||||
}
|
||||
$args = $node->getArgs();
|
||||
if (!$this->isName($funcCall, 'money_format')) {
|
||||
return null;
|
||||
}
|
||||
$args = $funcCall->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);
|
||||
if (!$this->valueResolver->isValue($formatValue, '%i')) {
|
||||
return null;
|
||||
}
|
||||
return $this->resolveNumberFormat($funcCall, $args[1]->value, $scope, $node);
|
||||
}
|
||||
/**
|
||||
* @return Stmt[]
|
||||
* @param \PhpParser\Node\Stmt\Expression|\PhpParser\Node\Stmt\Return_ $stmt
|
||||
*/
|
||||
private function resolveNumberFormat(FuncCall $funcCall, Expr $expr, Scope $scope, $stmt) : array
|
||||
{
|
||||
$newValue = $this->nodeFactory->createFuncCall('round', [$expr, new LNumber(2), new ConstFetch(new Name('PHP_ROUND_HALF_ODD'))]);
|
||||
$countedVariableName = $this->variableNaming->createCountedValueName('roundedValue', $scope);
|
||||
$variable = new Variable($countedVariableName);
|
||||
$funcCall->name = new Name('number_format');
|
||||
$funcCall->args = [new Arg($variable), new Arg(new LNumber(2)), new Arg(new String_('.')), new Arg(new String_(''))];
|
||||
return [new Expression(new Assign($variable, $newValue)), $stmt];
|
||||
}
|
||||
/**
|
||||
* @param \PhpParser\Node\Stmt\Expression|\PhpParser\Node\Stmt\Return_ $node
|
||||
*/
|
||||
private function matchFunCall($node) : ?\PhpParser\Node\Expr\FuncCall
|
||||
{
|
||||
if ($node->expr instanceof Assign) {
|
||||
$assign = $node->expr;
|
||||
if ($assign->expr instanceof FuncCall) {
|
||||
return $assign->expr;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
if ($node->expr instanceof FuncCall) {
|
||||
return $node->expr;
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,12 +19,12 @@ final class VersionResolver
|
|||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = '582af523662c9aac2603a1535d58fc932ab1c330';
|
||||
public const PACKAGE_VERSION = '1e0a1ceb897de085e3b8a68fbf88b4028d2064b0';
|
||||
/**
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2023-05-27 10:49:03';
|
||||
public const RELEASE_DATE = '2023-05-27 10:13:58';
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
|
|
|
@ -15,7 +15,7 @@ final class RectorKernel
|
|||
/**
|
||||
* @var string
|
||||
*/
|
||||
private const CACHE_KEY = 'v36';
|
||||
private const CACHE_KEY = 'v37';
|
||||
/**
|
||||
* @var \Symfony\Component\DependencyInjection\ContainerInterface|null
|
||||
*/
|
||||
|
|
2
vendor/autoload.php
vendored
2
vendor/autoload.php
vendored
|
@ -22,4 +22,4 @@ if (PHP_VERSION_ID < 50600) {
|
|||
|
||||
require_once __DIR__ . '/composer/autoload_real.php';
|
||||
|
||||
return ComposerAutoloaderInite2205975d34f6a61a31c784eba830e2f::getLoader();
|
||||
return ComposerAutoloaderInitd06e72155775bec6b1dd572afe5a16ce::getLoader();
|
||||
|
|
10
vendor/composer/autoload_real.php
vendored
10
vendor/composer/autoload_real.php
vendored
|
@ -2,7 +2,7 @@
|
|||
|
||||
// autoload_real.php @generated by Composer
|
||||
|
||||
class ComposerAutoloaderInite2205975d34f6a61a31c784eba830e2f
|
||||
class ComposerAutoloaderInitd06e72155775bec6b1dd572afe5a16ce
|
||||
{
|
||||
private static $loader;
|
||||
|
||||
|
@ -22,17 +22,17 @@ class ComposerAutoloaderInite2205975d34f6a61a31c784eba830e2f
|
|||
return self::$loader;
|
||||
}
|
||||
|
||||
spl_autoload_register(array('ComposerAutoloaderInite2205975d34f6a61a31c784eba830e2f', 'loadClassLoader'), true, true);
|
||||
spl_autoload_register(array('ComposerAutoloaderInitd06e72155775bec6b1dd572afe5a16ce', 'loadClassLoader'), true, true);
|
||||
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInite2205975d34f6a61a31c784eba830e2f', 'loadClassLoader'));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInitd06e72155775bec6b1dd572afe5a16ce', 'loadClassLoader'));
|
||||
|
||||
require __DIR__ . '/autoload_static.php';
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInite2205975d34f6a61a31c784eba830e2f::getInitializer($loader));
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInitd06e72155775bec6b1dd572afe5a16ce::getInitializer($loader));
|
||||
|
||||
$loader->setClassMapAuthoritative(true);
|
||||
$loader->register(true);
|
||||
|
||||
$filesToLoad = \Composer\Autoload\ComposerStaticInite2205975d34f6a61a31c784eba830e2f::$files;
|
||||
$filesToLoad = \Composer\Autoload\ComposerStaticInitd06e72155775bec6b1dd572afe5a16ce::$files;
|
||||
$requireFile = \Closure::bind(static function ($fileIdentifier, $file) {
|
||||
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
|
||||
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
|
||||
|
|
8
vendor/composer/autoload_static.php
vendored
8
vendor/composer/autoload_static.php
vendored
|
@ -4,7 +4,7 @@
|
|||
|
||||
namespace Composer\Autoload;
|
||||
|
||||
class ComposerStaticInite2205975d34f6a61a31c784eba830e2f
|
||||
class ComposerStaticInitd06e72155775bec6b1dd572afe5a16ce
|
||||
{
|
||||
public static $files = array (
|
||||
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
|
||||
|
@ -3046,9 +3046,9 @@ class ComposerStaticInite2205975d34f6a61a31c784eba830e2f
|
|||
public static function getInitializer(ClassLoader $loader)
|
||||
{
|
||||
return \Closure::bind(function () use ($loader) {
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInite2205975d34f6a61a31c784eba830e2f::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInite2205975d34f6a61a31c784eba830e2f::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInite2205975d34f6a61a31c784eba830e2f::$classMap;
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInitd06e72155775bec6b1dd572afe5a16ce::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInitd06e72155775bec6b1dd572afe5a16ce::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInitd06e72155775bec6b1dd572afe5a16ce::$classMap;
|
||||
|
||||
}, null, ClassLoader::class);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user