-
-
Notifications
You must be signed in to change notification settings - Fork 335
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Php74] Add MoneyFormatToNumberFormatRector (#2727)
Co-authored-by: GitHub Action <action@github.com>
- Loading branch information
1 parent
d894ccc
commit fadb6a4
Showing
6 changed files
with
210 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
rules-tests/Php74/Rector/FuncCall/MoneyFormatToNumberFormatRector/Fixture/fixture.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php74\Rector\FuncCall\MoneyFormatToNumberFormatRector\Fixture; | ||
|
||
class Fixture | ||
{ | ||
public function run($value) | ||
{ | ||
$value = money_format('%i', $value); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\Php74\Rector\FuncCall\MoneyFormatToNumberFormatRector\Fixture; | ||
|
||
class Fixture | ||
{ | ||
public function run($value) | ||
{ | ||
$roundedValue = round($value, 2, PHP_ROUND_HALF_ODD); | ||
$value = number_format($roundedValue, 2, '.', ''); | ||
} | ||
} | ||
|
||
?> |
33 changes: 33 additions & 0 deletions
33
...4/Rector/FuncCall/MoneyFormatToNumberFormatRector/MoneyFormatToNumberFormatRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\Php74\Rector\FuncCall\MoneyFormatToNumberFormatRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
final class MoneyFormatToNumberFormatRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function test(SmartFileInfo $fileInfo): void | ||
{ | ||
$this->doTestFileInfo($fileInfo); | ||
} | ||
|
||
/** | ||
* @return Iterator<SmartFileInfo> | ||
*/ | ||
public function provideData(): Iterator | ||
{ | ||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
rules-tests/Php74/Rector/FuncCall/MoneyFormatToNumberFormatRector/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\Php74\Rector\FuncCall\MoneyFormatToNumberFormatRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(MoneyFormatToNumberFormatRector::class); | ||
}; |
131 changes: 131 additions & 0 deletions
131
rules/Php74/Rector/FuncCall/MoneyFormatToNumberFormatRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
<?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']; | ||
|
||
public function __construct( | ||
private readonly ArgsAnalyzer $argsAnalyzer, | ||
private readonly NodesToAddCollector $nodesToAddCollector, | ||
private readonly 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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters