Browse Source

feat: Add literal separator support for `integer_literal_case` (#7081)

Co-authored-by: Greg Korba <greg@codito.dev>
Michael Voříšek 1 year ago
parent
commit
e3eef8ff8c

+ 4 - 4
src/Fixer/Basic/OctalNotationFixer.php

@@ -52,13 +52,13 @@ final class OctalNotationFixer extends AbstractFixer
 
             $content = $token->getContent();
 
-            if (!Preg::match('#^0[\d_]+$#', $content)) {
+            $newContent = Preg::replace('#^0_*+([0-7_]+)$#', '0o$1', $content);
+
+            if ($content === $newContent) {
                 continue;
             }
 
-            $tokens[$index] = Preg::match('#^0+$#', $content)
-                ? new Token([T_LNUMBER, '0'])
-                : new Token([T_LNUMBER, '0o'.('_' === $content[1] ? '0' : '').substr($content, 1)]);
+            $tokens[$index] = new Token([T_LNUMBER, $newContent]);
         }
     }
 }

+ 1 - 5
src/Fixer/Casing/IntegerLiteralCaseFixer.php

@@ -50,11 +50,7 @@ final class IntegerLiteralCaseFixer extends AbstractFixer
 
             $content = $token->getContent();
 
-            if (!Preg::match('#^0[bxoBXO][0-9a-fA-F]+$#', $content)) {
-                continue;
-            }
-
-            $newContent = '0'.strtolower($content[1]).strtoupper(substr($content, 2));
+            $newContent = Preg::replaceCallback('#^0([boxBOX])([0-9a-fA-F_]+)$#', static fn ($matches) => '0'.strtolower($matches[1]).strtoupper($matches[2]), $content);
 
             if ($content === $newContent) {
                 continue;

+ 8 - 3
tests/Fixer/Basic/OctalNotationFixerTest.php

@@ -55,7 +55,7 @@ final class OctalNotationFixerTest extends AbstractFixerTestCase
         ];
 
         yield [
-            '<?php $foo = 0;',
+            '<?php $foo = 0o0000;',
             '<?php $foo = 00000;',
         ];
 
@@ -76,8 +76,13 @@ final class OctalNotationFixerTest extends AbstractFixerTestCase
         ];
 
         yield [
-            '<?php $foo = 0o0_123_456;',
-            '<?php $foo = 0_123_456;',
+            '<?php $foo = +0o1_234;',
+            '<?php $foo = +01_234;',
+        ];
+
+        yield [
+            '<?php $foo = -0o123_456;',
+            '<?php $foo = -0_123_456;',
         ];
     }
 }

+ 35 - 0
tests/Fixer/Casing/IntegerLiteralCaseFixerTest.php

@@ -43,6 +43,11 @@ final class IntegerLiteralCaseFixerTest extends AbstractFixerTestCase
             '<?php $foo = 0xa1fb20;',
         ];
 
+        yield [
+            '<?php $foo = -0xA1FB20;',
+            '<?php $foo = -0xa1fb20;',
+        ];
+
         yield [
             '<?php $foo = 0b1101;',
             '<?php $foo = 0B1101;',
@@ -51,6 +56,31 @@ final class IntegerLiteralCaseFixerTest extends AbstractFixerTestCase
         yield [
             '<?php $A = 1_234_567;',
         ];
+
+        yield [
+            '<?php $A = +0xAA_FF_00;',
+            '<?php $A = +0Xaa_ff_00;',
+        ];
+
+        yield [
+            '<?php $A = -0x00_AA_FF_00;',
+            '<?php $A = -0X00_aa_ff_00;',
+        ];
+
+        yield 'bin_PHP_INT_MAX' => [
+            '<?php $foo = 0b111111111111111111111111111111111111111111111111111111111111111;',
+            '<?php $foo = 0B111111111111111111111111111111111111111111111111111111111111111;',
+        ];
+
+        yield 'hex_plus_PHP_INT_MAX' => [
+            '<?php $foo = +0x7FFFFFFFFFFFFFFF;',
+            '<?php $foo = +0X7fffffffffffffff;',
+        ];
+
+        yield 'hex_minus_PHP_INT_MAX' => [
+            '<?php $foo = -0x7FFFFFFFFFFFFFFF;',
+            '<?php $foo = -0X7fffffffffffffff;',
+        ];
     }
 
     /**
@@ -69,5 +99,10 @@ final class IntegerLiteralCaseFixerTest extends AbstractFixerTestCase
             '<?php $foo = 0o123;',
             '<?php $foo = 0O123;',
         ];
+
+        yield [
+            '<?php $foo = -0o123;',
+            '<?php $foo = -0O123;',
+        ];
     }
 }