From 761a4d62d689dd755c62a34ea8908b8668feeb96 Mon Sep 17 00:00:00 2001 From: Marvin Heilemann <11534760+muuvmuuv@users.noreply.github.com> Date: Mon, 15 Jan 2024 15:44:08 +0100 Subject: [PATCH] fix: `numeric_literal_separator` - Handle zero-leading floats properly (#7737) --- .../Basic/NumericLiteralSeparatorFixer.php | 4 +-- .../NumericLiteralSeparatorFixerTest.php | 25 +++++++++++++++---- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/Fixer/Basic/NumericLiteralSeparatorFixer.php b/src/Fixer/Basic/NumericLiteralSeparatorFixer.php index eae9e80ae42..3e091f7672a 100644 --- a/src/Fixer/Basic/NumericLiteralSeparatorFixer.php +++ b/src/Fixer/Basic/NumericLiteralSeparatorFixer.php @@ -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); } diff --git a/tests/Fixer/Basic/NumericLiteralSeparatorFixerTest.php b/tests/Fixer/Basic/NumericLiteralSeparatorFixerTest.php index f7d2759ca10..1987c9c338d 100644 --- a/tests/Fixer/Basic/NumericLiteralSeparatorFixerTest.php +++ b/tests/Fixer/Basic/NumericLiteralSeparatorFixerTest.php @@ -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', @@ -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(' NumericLiteralSeparatorFixer::STRATEGY_USE_SEPARATOR], - ]; + if (null === $withSeparator) { + yield "do not modify valid {$pairsType} {$withoutSeparator}" => [ + sprintf(' NumericLiteralSeparatorFixer::STRATEGY_USE_SEPARATOR], + ]; + } else { + yield "add separator to {$pairsType} {$withoutSeparator}" => [ + sprintf(' NumericLiteralSeparatorFixer::STRATEGY_USE_SEPARATOR], + ]; + } } foreach ($pairs as $withoutSeparator => $withSeparator) { + if (null === $withSeparator) { + continue; + } + yield "remove separator from {$pairsType} {$withoutSeparator}" => [ sprintf('