forked from PHP-CS-Fixer/PHP-CS-Fixer
-
Notifications
You must be signed in to change notification settings - Fork 2
/
NumericLiteralSeparatorFixer.php
232 lines (198 loc) · 7.62 KB
/
NumericLiteralSeparatorFixer.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Fixer\Basic;
use PhpCsFixer\AbstractFixer;
use PhpCsFixer\Fixer\ConfigurableFixerInterface;
use PhpCsFixer\Fixer\ConfigurableFixerTrait;
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface;
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Preg;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
/**
* Let's you add underscores to numeric literals.
*
* Inspired by:
* - {@link https://github.com/kubawerlos/php-cs-fixer-custom-fixers/blob/main/src/Fixer/NumericLiteralSeparatorFixer.php}
* - {@link https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/rules/numeric-separators-style.js}
*
* @author Marvin Heilemann <marvin.heilemann+github@googlemail.com>
* @author Greg Korba <greg@codito.dev>
*
* @implements ConfigurableFixerInterface<_AutogeneratedInputConfiguration, _AutogeneratedComputedConfiguration>
*
* @phpstan-type _AutogeneratedInputConfiguration array{
* override_existing?: bool,
* strategy?: 'no_separator'|'use_separator'
* }
* @phpstan-type _AutogeneratedComputedConfiguration array{
* override_existing: bool,
* strategy: 'no_separator'|'use_separator'
* }
*/
final class NumericLiteralSeparatorFixer extends AbstractFixer implements ConfigurableFixerInterface
{
/** @use ConfigurableFixerTrait<_AutogeneratedInputConfiguration, _AutogeneratedComputedConfiguration> */
use ConfigurableFixerTrait;
public const STRATEGY_USE_SEPARATOR = 'use_separator';
public const STRATEGY_NO_SEPARATOR = 'no_separator';
public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition(
'Adds separators to numeric literals of any kind.',
[
new CodeSample(
<<<'PHP'
<?php
$integer = 1234567890;
PHP
,
),
new CodeSample(
<<<'PHP'
<?php
$integer = 1234_5678;
$octal = 01_234_56;
$binary = 0b00_10_01_00;
$hexadecimal = 0x3D45_8F4F;
PHP
,
['strategy' => self::STRATEGY_NO_SEPARATOR],
),
new CodeSample(
<<<'PHP'
<?php
$integer = 12345678;
$octal = 0123456;
$binary = 0b0010010011011010;
$hexadecimal = 0x3D458F4F;
PHP
,
['strategy' => self::STRATEGY_USE_SEPARATOR],
),
new CodeSample(
"<?php \$var = 24_40_21;\n",
['override_existing' => true]
),
]
);
}
public function isCandidate(Tokens $tokens): bool
{
return $tokens->isAnyTokenKindsFound([T_DNUMBER, T_LNUMBER]);
}
protected function createConfigurationDefinition(): FixerConfigurationResolverInterface
{
return new FixerConfigurationResolver([
(new FixerOptionBuilder(
'override_existing',
'Whether literals already containing underscores should be reformatted.'
))
->setAllowedTypes(['bool'])
->setDefault(false)
->getOption(),
(new FixerOptionBuilder(
'strategy',
'Whether numeric literal should be separated by underscores or not.'
))
->setAllowedValues([self::STRATEGY_USE_SEPARATOR, self::STRATEGY_NO_SEPARATOR])
->setDefault(self::STRATEGY_USE_SEPARATOR)
->getOption(),
]);
}
protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
{
foreach ($tokens as $index => $token) {
if (!$token->isGivenKind([T_DNUMBER, T_LNUMBER])) {
continue;
}
$content = $token->getContent();
$newContent = $this->formatValue($content);
if ($content === $newContent) {
// Skip Token override if its the same content, like when it
// already got a valid literal separator structure.
continue;
}
$tokens[$index] = new Token([$token->getId(), $newContent]);
}
}
private function formatValue(string $value): string
{
if (self::STRATEGY_NO_SEPARATOR === $this->configuration['strategy']) {
return str_contains($value, '_') ? str_replace('_', '', $value) : $value;
}
if (true === $this->configuration['override_existing']) {
$value = str_replace('_', '', $value);
} elseif (str_contains($value, '_')) {
// Keep already underscored literals untouched.
return $value;
}
$lowerValue = strtolower($value);
if (str_starts_with($lowerValue, '0b')) {
// Binary
return $this->insertEveryRight($value, 8, 2);
}
if (str_starts_with($lowerValue, '0x')) {
// Hexadecimal
return $this->insertEveryRight($value, 2, 2);
}
if (str_starts_with($lowerValue, '0o')) {
// Octal
return $this->insertEveryRight($value, 3, 2);
}
if (str_starts_with($lowerValue, '0') && !str_contains($lowerValue, '.')) {
// Octal notation prior PHP 8.1 but still valid
return $this->insertEveryRight($value, 3, 1);
}
// All other types
/** If its a negative value we need an offset */
$negativeOffset = static fn ($v) => str_contains($v, '-') ? 1 : 0;
Preg::matchAll('/([0-9-_]+)?((\.)([0-9_]*))?((e)([0-9-_]+))?/i', $value, $result);
$integer = $result[1][0];
$joinedValue = $this->insertEveryRight($integer, 3, $negativeOffset($integer));
$dot = $result[3][0];
if ('' !== $dot) {
$integer = $result[4][0];
$decimal = $this->insertEveryLeft($integer, 3, $negativeOffset($integer));
$joinedValue = $joinedValue.$dot.$decimal;
}
$tim = $result[6][0];
if ('' !== $tim) {
$integer = $result[7][0];
$times = $this->insertEveryRight($integer, 3, $negativeOffset($integer));
$joinedValue = $joinedValue.$tim.$times;
}
return $joinedValue;
}
private function insertEveryRight(string $value, int $length, int $offset = 0): string
{
$position = $length * -1;
while ($position > -(\strlen($value) - $offset)) {
$value = substr_replace($value, '_', $position, 0);
$position -= $length + 1;
}
return $value;
}
private function insertEveryLeft(string $value, int $length, int $offset = 0): string
{
$position = $length;
while ($position < \strlen($value)) {
$value = substr_replace($value, '_', $position, $offset);
$position += $length + 1;
}
return $value;
}
}