Skip to content

Commit

Permalink
[DowngradePhp84] Downgrade RoundingMode enum
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgsowa committed Dec 19, 2024
1 parent 7b990bd commit d497751
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\DowngradePhp84\Rector\FuncCall\DowngradeRoundingModeEnumRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class DowngradeRoundingModeEnumRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Rector\Tests\Php84\Rector\FuncCall\RoundingModeEnumRector;

round(1.5, 0);
round(1.5);
round(1.5, 0, \RoundingMode::HalfAwayFromZero);
round(1.5, 0, \RoundingMode::HalfTowardsZero);
round(1.5, 0, \RoundingMode::HalfEven);
round(1.5, 0, \RoundingMode::HalfOdd);
round(1.5, 0, \RoundingMode::TowardsZero);
round(1.5, 0, \RoundingMode::AwayFromZero);
round(1.5, 0, \RoundingMode::NegativeInfinity);
round(1.5, 0, \RoundingMode::PositiveInfinity);
round(1.5, 0, 'invalid');
round(1.5, 0, PHP_INT_MAX);

?>
-----
<?php

namespace Rector\Tests\Php84\Rector\FuncCall\RoundingModeEnumRector;

round(1.5, 0);
round(1.5);
round(1.5, 0, PHP_ROUND_HALF_UP);
round(1.5, 0, PHP_ROUND_HALF_DOWN);
round(1.5, 0, PHP_ROUND_HALF_EVEN);
round(1.5, 0, PHP_ROUND_HALF_ODD);
round(1.5, 0, \RoundingMode::TowardsZero);
round(1.5, 0, \RoundingMode::AwayFromZero);
round(1.5, 0, \RoundingMode::NegativeInfinity);
round(1.5, 0, \RoundingMode::PositiveInfinity);
round(1.5, 0, 'invalid');
round(1.5, 0, PHP_INT_MAX);

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Rector\Tests\Php84\Rector\FuncCall\RoundingModeEnumRector;


$mode = \RoundingMode::HalfTowardsZero;

round(1.5, 0, $mode);

?>
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\DowngradePhp84\Rector\FuncCall\DowngradeRoundingModeEnumRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(DowngradeRoundingModeEnumRector::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

declare(strict_types=1);

namespace Rector\DowngradePhp84\Rector\FuncCall;

use PhpParser\Node;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\MethodCall;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @changelog https://wiki.php.net/rfc/new_without_parentheses
*
* @see \Rector\Tests\DowngradePhp84\Rector\MethodCall\DowngradeNewMethodCallWithoutParenthesesRector\DowngradeNewMethodCallWithoutParenthesesRectorTest
*/
final class DowngradeRoundingModeEnumRector extends AbstractRector
{
public function getNodeTypes(): array
{
return [Node\Expr\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;
}

$modeArg = $args[2]->value;
if ($modeArg instanceof ClassConstFetch) {
if ($modeArg->class->name === 'RoundingMode') {

Check failure on line 65 in rules/DowngradePhp84/Rector/FuncCall/DowngradeRoundingModeEnumRector.php

View workflow job for this annotation

GitHub Actions / code_analysis_reusable / PHPStan

Access to an undefined property PhpParser\Node\Expr|PhpParser\Node\Name::$name.
$constantName = match ($modeArg->name->name) {

Check failure on line 66 in rules/DowngradePhp84/Rector/FuncCall/DowngradeRoundingModeEnumRector.php

View workflow job for this annotation

GitHub Actions / code_analysis_reusable / PHPStan

Access to an undefined property PhpParser\Node\Expr|PhpParser\Node\Identifier::$name.
'HalfAwayFromZero' => 'PHP_ROUND_HALF_UP',
'HalfTowardsZero' => 'PHP_ROUND_HALF_DOWN',
'HalfEven' => 'PHP_ROUND_HALF_EVEN',
'HalfOdd' => 'PHP_ROUND_HALF_ODD',
default => null,
};

if ($constantName === null) {
return null;
}

$args[2]->value = new Node\Expr\ConstFetch(new Node\Name($constantName));
}
}

return $node;
}
}

0 comments on commit d497751

Please sign in to comment.