-
-
Notifications
You must be signed in to change notification settings - Fork 336
/
DowngradeNumericLiteralSeparatorRector.php
119 lines (103 loc) · 3.05 KB
/
DowngradeNumericLiteralSeparatorRector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
declare(strict_types=1);
namespace Rector\DowngradePhp74\Rector\LNumber;
use PhpParser\Node;
use PhpParser\Node\Scalar\DNumber;
use PhpParser\Node\Scalar\LNumber;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @changelog https://wiki.php.net/rfc/numeric_literal_separator
*
* @see \Rector\Tests\DowngradePhp74\Rector\LNumber\DowngradeNumericLiteralSeparatorRector\DowngradeNumericLiteralSeparatorRectorTest
*/
final class DowngradeNumericLiteralSeparatorRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Remove "_" as thousands separator in numbers',
[
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
$int = 1_000;
$float = 1_000_500.001;
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
$int = 1000;
$float = 1000500.001;
}
}
CODE_SAMPLE
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [LNumber::class, DNumber::class];
}
/**
* @param LNumber|DNumber $node
*/
public function refactor(Node $node): ?Node
{
if ($this->shouldSkip($node)) {
return null;
}
$numberValueAsString = (string) $node->value;
if (\str_contains($numberValueAsString, '+')) {
return null;
}
// trigger reprint
$node->setAttribute(AttributeKey::ORIGINAL_NODE, null);
if ($node instanceof LNumber) {
return $node;
}
/**
* This code follows a guess, to avoid modifying floats needlessly.
* If the node is a float, but it doesn't contain ".",
* then it's likely that the number was forced to be a float
* by adding ".0" at the end (eg: 0.0).
* Then, add it again.
*/
if (! \str_contains($numberValueAsString, '.')) {
$numberValueAsString .= '.0';
}
$node->value = (float) $numberValueAsString;
return $node;
}
private function shouldSkip(LNumber | DNumber $node): bool
{
// "_" notation can be applied to decimal numbers only
if ($node instanceof LNumber) {
$numberKind = $node->getAttribute(AttributeKey::KIND);
if ($numberKind !== LNumber::KIND_DEC) {
return true;
}
}
// we have to hack around tokens to get original value, see https://github.com/nikic/PHP-Parser/pull/832
$oldTokens = $this->file->getOldTokens();
$tokenValue = $oldTokens[$node->getStartTokenPos()][1] ?? null;
if ($tokenValue === null) {
return true;
}
return ! str_contains((string) $tokenValue, '_');
}
}