Skip to content

Commit

Permalink
fix: numeric_literal_separator - Handle zero-leading floats properly (
Browse files Browse the repository at this point in the history
  • Loading branch information
muuvmuuv authored Jan 15, 2024
1 parent 27933b0 commit 761a4d6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/Fixer/Basic/NumericLiteralSeparatorFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ private function formatValue(string $value): string
// Octal
return $this->insertEveryRight($value, 3, 2);
}
if (str_starts_with($lowerValue, '0')) {
// Octal prior PHP 8.1
if (str_starts_with($lowerValue, '0') && !str_contains($lowerValue, '.')) {
// Octal notation prior PHP 8.1 but still valid
return $this->insertEveryRight($value, 3, 1);
}

Expand Down
25 changes: 20 additions & 5 deletions tests/Fixer/Basic/NumericLiteralSeparatorFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ public static function provideFixCases(): iterable
'0b110001000' => '0b1_10001000',
],
'float' => [
'.001' => null,
'.1001' => '.100_1',
'0.0001' => '0.000_1',
'0.001' => null,
'1234.5' => '1_234.5',
'1.2345' => '1.234_5',
'1234e5' => '1_234e5',
Expand Down Expand Up @@ -168,14 +171,26 @@ private static function yieldCases(array $cases): iterable
{
foreach ($cases as $pairsType => $pairs) {
foreach ($pairs as $withoutSeparator => $withSeparator) {
yield "add separator to {$pairsType} {$withoutSeparator}" => [
sprintf('<?php echo %s;', $withSeparator),
sprintf('<?php echo %s;', $withoutSeparator),
['strategy' => NumericLiteralSeparatorFixer::STRATEGY_USE_SEPARATOR],
];
if (null === $withSeparator) {
yield "do not modify valid {$pairsType} {$withoutSeparator}" => [
sprintf('<?php echo %s;', $withoutSeparator),
null,
['strategy' => NumericLiteralSeparatorFixer::STRATEGY_USE_SEPARATOR],
];
} else {
yield "add separator to {$pairsType} {$withoutSeparator}" => [
sprintf('<?php echo %s;', $withSeparator),
sprintf('<?php echo %s;', $withoutSeparator),
['strategy' => NumericLiteralSeparatorFixer::STRATEGY_USE_SEPARATOR],
];
}
}

foreach ($pairs as $withoutSeparator => $withSeparator) {
if (null === $withSeparator) {
continue;
}

yield "remove separator from {$pairsType} {$withoutSeparator}" => [
sprintf('<?php echo %s;', $withoutSeparator),
sprintf('<?php echo %s;', $withSeparator),
Expand Down

0 comments on commit 761a4d6

Please sign in to comment.