Skip to content

Commit

Permalink
Return nodes directly in MoneyFormatToNumberFormatRector (#3986)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed May 27, 2023
1 parent 582af52 commit 1e0a1ce
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Rector\Tests\Php74\Rector\FuncCall\MoneyFormatToNumberFormatRector\Fixture;

final class ReturnStmt
{
public function run($value)
{
return money_format('%i', $value);
}
}

?>
-----
<?php

namespace Rector\Tests\Php74\Rector\FuncCall\MoneyFormatToNumberFormatRector\Fixture;

final class ReturnStmt
{
public function run($value)
{
$roundedValue = round($value, 2, PHP_ROUND_HALF_ODD);
return number_format($roundedValue, 2, '.', '');
}
}

?>
80 changes: 46 additions & 34 deletions rules/Php74/Rector/FuncCall/MoneyFormatToNumberFormatRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
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;
Expand All @@ -33,14 +33,8 @@
*/
final class MoneyFormatToNumberFormatRector extends AbstractScopeAwareRector implements MinPhpVersionInterface
{
/**
* @var string[]
*/
private const FORMATS = ['%i'];

public function __construct(
private readonly ArgsAnalyzer $argsAnalyzer,
private readonly NodesToAddCollector $nodesToAddCollector,
private readonly VariableNaming $variableNaming
) {
}
Expand Down Expand Up @@ -74,58 +68,76 @@ public function getRuleDefinition(): RuleDefinition
*/
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 null;
return $this->resolveNumberFormat($funcCall, $args[1]->value, $scope, $node);
}

private function resolveNumberFormat(FuncCall $funcCall, Expr $expr, Scope $scope): ?FuncCall
/**
* @return Stmt[]
*/
private function resolveNumberFormat(FuncCall $funcCall, Expr $expr, Scope $scope, Expression|Return_ $stmt): array
{
$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
);
$countedVariableName = $this->variableNaming->createCountedValueName('roundedValue', $scope);
$variable = new Variable($countedVariableName);

$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_(''));
$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];
}

private function matchFunCall(Expression|Return_ $node): null|FuncCall
{
if ($node->expr instanceof Assign) {
$assign = $node->expr;
if ($assign->expr instanceof FuncCall) {
return $assign->expr;
}

return $funcCall;
return null;
}

if ($node->expr instanceof FuncCall) {
return $node->expr;
}

return null;
}
}
2 changes: 1 addition & 1 deletion src/Kernel/RectorKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class RectorKernel
/**
* @var string
*/
private const CACHE_KEY = 'v36';
private const CACHE_KEY = 'v37';

private ContainerInterface|null $container = null;

Expand Down

0 comments on commit 1e0a1ce

Please sign in to comment.