Skip to content

Commit d497751

Browse files
committed
[DowngradePhp84] Downgrade RoundingMode enum
1 parent 7b990bd commit d497751

File tree

5 files changed

+169
-0
lines changed

5 files changed

+169
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\DowngradePhp84\Rector\FuncCall\DowngradeRoundingModeEnumRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class DowngradeRoundingModeEnumRectorTest extends AbstractRectorTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php84\Rector\FuncCall\RoundingModeEnumRector;
4+
5+
round(1.5, 0);
6+
round(1.5);
7+
round(1.5, 0, \RoundingMode::HalfAwayFromZero);
8+
round(1.5, 0, \RoundingMode::HalfTowardsZero);
9+
round(1.5, 0, \RoundingMode::HalfEven);
10+
round(1.5, 0, \RoundingMode::HalfOdd);
11+
round(1.5, 0, \RoundingMode::TowardsZero);
12+
round(1.5, 0, \RoundingMode::AwayFromZero);
13+
round(1.5, 0, \RoundingMode::NegativeInfinity);
14+
round(1.5, 0, \RoundingMode::PositiveInfinity);
15+
round(1.5, 0, 'invalid');
16+
round(1.5, 0, PHP_INT_MAX);
17+
18+
?>
19+
-----
20+
<?php
21+
22+
namespace Rector\Tests\Php84\Rector\FuncCall\RoundingModeEnumRector;
23+
24+
round(1.5, 0);
25+
round(1.5);
26+
round(1.5, 0, PHP_ROUND_HALF_UP);
27+
round(1.5, 0, PHP_ROUND_HALF_DOWN);
28+
round(1.5, 0, PHP_ROUND_HALF_EVEN);
29+
round(1.5, 0, PHP_ROUND_HALF_ODD);
30+
round(1.5, 0, \RoundingMode::TowardsZero);
31+
round(1.5, 0, \RoundingMode::AwayFromZero);
32+
round(1.5, 0, \RoundingMode::NegativeInfinity);
33+
round(1.5, 0, \RoundingMode::PositiveInfinity);
34+
round(1.5, 0, 'invalid');
35+
round(1.5, 0, PHP_INT_MAX);
36+
37+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php84\Rector\FuncCall\RoundingModeEnumRector;
4+
5+
6+
$mode = \RoundingMode::HalfTowardsZero;
7+
8+
round(1.5, 0, $mode);
9+
10+
?>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\DowngradePhp84\Rector\FuncCall\DowngradeRoundingModeEnumRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(DowngradeRoundingModeEnumRector::class);
10+
};
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\DowngradePhp84\Rector\FuncCall;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Expr\ClassConstFetch;
9+
use PhpParser\Node\Expr\MethodCall;
10+
use Rector\Rector\AbstractRector;
11+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
12+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
13+
14+
/**
15+
* @changelog https://wiki.php.net/rfc/new_without_parentheses
16+
*
17+
* @see \Rector\Tests\DowngradePhp84\Rector\MethodCall\DowngradeNewMethodCallWithoutParenthesesRector\DowngradeNewMethodCallWithoutParenthesesRectorTest
18+
*/
19+
final class DowngradeRoundingModeEnumRector extends AbstractRector
20+
{
21+
public function getNodeTypes(): array
22+
{
23+
return [Node\Expr\FuncCall::class];
24+
}
25+
26+
public function getRuleDefinition(): RuleDefinition
27+
{
28+
return new RuleDefinition(
29+
'Replace RoundingMode enum to rounding mode constant in round()',
30+
[
31+
new CodeSample(
32+
<<<'CODE_SAMPLE'
33+
round(1.5, 0, RoundingMode::HalfAwayFromZero);
34+
CODE_SAMPLE
35+
,
36+
<<<'CODE_SAMPLE'
37+
round(1.5, 0, PHP_ROUND_HALF_UP);
38+
CODE_SAMPLE
39+
),
40+
]
41+
);
42+
}
43+
44+
/**
45+
* @param MethodCall $node
46+
*/
47+
public function refactor(Node $node): ?Node
48+
{
49+
if (!$this->isName($node, 'round')) {
50+
return null;
51+
}
52+
53+
if ($node->isFirstClassCallable()) {
54+
return null;
55+
}
56+
57+
$args = $node->getArgs();
58+
59+
if (count($args) !== 3) {
60+
return null;
61+
}
62+
63+
$modeArg = $args[2]->value;
64+
if ($modeArg instanceof ClassConstFetch) {
65+
if ($modeArg->class->name === 'RoundingMode') {
66+
$constantName = match ($modeArg->name->name) {
67+
'HalfAwayFromZero' => 'PHP_ROUND_HALF_UP',
68+
'HalfTowardsZero' => 'PHP_ROUND_HALF_DOWN',
69+
'HalfEven' => 'PHP_ROUND_HALF_EVEN',
70+
'HalfOdd' => 'PHP_ROUND_HALF_ODD',
71+
default => null,
72+
};
73+
74+
if ($constantName === null) {
75+
return null;
76+
}
77+
78+
$args[2]->value = new Node\Expr\ConstFetch(new Node\Name($constantName));
79+
}
80+
}
81+
82+
return $node;
83+
}
84+
}

0 commit comments

Comments
 (0)