Skip to content

Commit cf75c4b

Browse files
[depre] Deprecate AddLiteralSeparatorToNumberRector as cannot be automated and depends on context (#7569)
* [depre] Deprecate AddLiteralSeparatorToNumberRector as cannot be automated and depends on context * [ci-review] Rector Rectify * [ci-review] Rector Rectify --------- Co-authored-by: GitHub Action <actions@github.com>
1 parent be2708c commit cf75c4b

9 files changed

Lines changed: 8 additions & 242 deletions

File tree

rules-tests/Php74/Rector/LNumber/AddLiteralSeparatorToNumberRector/AddLiteralSeparatorToNumberRectorTest.php

Lines changed: 0 additions & 28 deletions
This file was deleted.

rules-tests/Php74/Rector/LNumber/AddLiteralSeparatorToNumberRector/Fixture/big_floats.php.inc

Lines changed: 0 additions & 31 deletions
This file was deleted.

rules-tests/Php74/Rector/LNumber/AddLiteralSeparatorToNumberRector/Fixture/big_integers.php.inc

Lines changed: 0 additions & 27 deletions
This file was deleted.

rules-tests/Php74/Rector/LNumber/AddLiteralSeparatorToNumberRector/Fixture/skip_already_formatted.php.inc

Lines changed: 0 additions & 11 deletions
This file was deleted.

rules-tests/Php74/Rector/LNumber/AddLiteralSeparatorToNumberRector/Fixture/skip_hexadecimal.php.inc

Lines changed: 0 additions & 11 deletions
This file was deleted.

rules-tests/Php74/Rector/LNumber/AddLiteralSeparatorToNumberRector/Fixture/skip_low_valuefixture.php.inc

Lines changed: 0 additions & 11 deletions
This file was deleted.

rules-tests/Php74/Rector/LNumber/AddLiteralSeparatorToNumberRector/Fixture/skip_non_dec_simple_float_numbers.php.inc

Lines changed: 0 additions & 13 deletions
This file was deleted.

rules-tests/Php74/Rector/LNumber/AddLiteralSeparatorToNumberRector/config/configured_rule.php

Lines changed: 0 additions & 13 deletions
This file was deleted.

rules/Php74/Rector/LNumber/AddLiteralSeparatorToNumberRector.php

Lines changed: 8 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -7,51 +7,34 @@
77
use PhpParser\Node;
88
use PhpParser\Node\Scalar\Float_;
99
use PhpParser\Node\Scalar\Int_;
10+
use Rector\Configuration\Deprecation\Contract\DeprecatedInterface;
1011
use Rector\Contract\Rector\ConfigurableRectorInterface;
11-
use Rector\NodeTypeResolver\Node\AttributeKey;
12+
use Rector\Exception\ShouldNotHappenException;
1213
use Rector\Rector\AbstractRector;
13-
use Rector\Util\StringUtils;
1414
use Rector\ValueObject\PhpVersionFeature;
1515
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
1616
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
1717
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
18-
use Webmozart\Assert\Assert;
1918

2019
/**
2120
* Taking the most generic use case to the account: https://wiki.php.net/rfc/numeric_literal_separator#should_it_be_the_role_of_an_ide_to_group_digits
2221
* The final check should be done manually
2322
*
24-
* @see \Rector\Tests\Php74\Rector\LNumber\AddLiteralSeparatorToNumberRector\AddLiteralSeparatorToNumberRectorTest
23+
* @deprecated as opinionated and group size depends on context. Cannot be automated. Use manually where neeed isntead.
2524
*/
26-
final class AddLiteralSeparatorToNumberRector extends AbstractRector implements MinPhpVersionInterface, ConfigurableRectorInterface
25+
final class AddLiteralSeparatorToNumberRector extends AbstractRector implements MinPhpVersionInterface, ConfigurableRectorInterface, DeprecatedInterface
2726
{
2827
/**
2928
* @api
3029
* @var string
3130
*/
3231
public const LIMIT_VALUE = 'limit_value';
3332

34-
/**
35-
* @var int
36-
*/
37-
private const GROUP_SIZE = 3;
38-
39-
/**
40-
* @var int
41-
*/
42-
private const DEFAULT_LIMIT_VALUE = 1_000_000;
43-
44-
private int $limitValue = self::DEFAULT_LIMIT_VALUE;
45-
4633
/**
4734
* @param array<string, mixed> $configuration
4835
*/
4936
public function configure(array $configuration): void
5037
{
51-
$limitValue = $configuration[self::LIMIT_VALUE] ?? self::DEFAULT_LIMIT_VALUE;
52-
Assert::integer($limitValue);
53-
54-
$this->limitValue = $limitValue;
5538
}
5639

5740
public function getRuleDefinition(): RuleDefinition
@@ -103,86 +86,14 @@ public function getNodeTypes(): array
10386
*/
10487
public function refactor(Node $node): ?Node
10588
{
106-
$rawValue = $node->getAttribute(AttributeKey::RAW_VALUE);
107-
108-
if ($this->shouldSkip($node, $rawValue)) {
109-
return null;
110-
}
111-
112-
if (\str_contains((string) $rawValue, '.')) {
113-
[$mainPart, $decimalPart] = explode('.', (string) $rawValue);
114-
115-
$chunks = $this->strSplitNegative($mainPart, self::GROUP_SIZE);
116-
$literalSeparatedNumber = implode('_', $chunks) . '.' . $decimalPart;
117-
} else {
118-
$chunks = $this->strSplitNegative($rawValue, self::GROUP_SIZE);
119-
$literalSeparatedNumber = implode('_', $chunks);
120-
121-
// PHP converts: (string) 1000.0 -> "1000"!
122-
if (is_float($node->value)) {
123-
$literalSeparatedNumber .= '.0';
124-
}
125-
}
126-
127-
// this cannot be integer directly to $node->value, as PHPStan sees it as error type
128-
// @see https://github.com/rectorphp/rector/issues/7454
129-
$node->setAttribute(AttributeKey::RAW_VALUE, $literalSeparatedNumber);
130-
$node->setAttribute(AttributeKey::REPRINT_RAW_VALUE, true);
131-
$node->setAttribute(AttributeKey::ORIGINAL_NODE, null);
132-
133-
return $node;
89+
throw new ShouldNotHappenException(sprintf(
90+
'%s is deprecated as opinonated and group size depends on context. Cannot be automated. Use manually where needed instead',
91+
self::class
92+
));
13493
}
13594

13695
public function provideMinPhpVersion(): int
13796
{
13897
return PhpVersionFeature::LITERAL_SEPARATOR;
13998
}
140-
141-
private function shouldSkip(Int_ | Float_ $node, mixed $rawValue): bool
142-
{
143-
if (! is_string($rawValue)) {
144-
return true;
145-
}
146-
147-
// already contains separator
148-
if (str_contains($rawValue, '_')) {
149-
return true;
150-
}
151-
152-
if ($node->value < $this->limitValue) {
153-
return true;
154-
}
155-
156-
$kind = $node->getAttribute(AttributeKey::KIND);
157-
if (in_array($kind, [Int_::KIND_BIN, Int_::KIND_OCT, Int_::KIND_HEX], true)) {
158-
return true;
159-
}
160-
161-
// e+/e-
162-
if (StringUtils::isMatch($rawValue, '#e#i')) {
163-
return true;
164-
}
165-
166-
// too short
167-
return strlen($rawValue) <= self::GROUP_SIZE;
168-
}
169-
170-
/**
171-
* @param int<1, max> $length
172-
* @return string[]
173-
*/
174-
private function strSplitNegative(string $string, int $length): array
175-
{
176-
$inversed = strrev($string);
177-
178-
/** @var string[] $chunks */
179-
$chunks = str_split($inversed, $length);
180-
181-
$chunks = array_reverse($chunks);
182-
foreach ($chunks as $key => $chunk) {
183-
$chunks[$key] = strrev($chunk);
184-
}
185-
186-
return $chunks;
187-
}
18899
}

0 commit comments

Comments
 (0)