Browse Source

Ensure compatibility with PHP 7.4 null coalescing assignment operator

Julien Falque 5 years ago
parent
commit
4088ff5c5e

+ 2 - 1
.composer-require-checker.json

@@ -15,7 +15,8 @@
 
         "null", "true", "false",
         "static", "self", "parent",
-        "array", "string", "int", "float", "bool", "iterable", "callable", "void"
+        "array", "string", "int", "float", "bool", "iterable", "callable", "void",
+        "T_COALESCE_EQUAL"
     ],
     "php-core-extensions" : [
         "dom", "mbstring", "Phar",

+ 5 - 0
src/Fixer/Operator/BinaryOperatorSpacesFixer.php

@@ -136,6 +136,7 @@ final class BinaryOperatorSpacesFixer extends AbstractFixer implements Configura
         '**=',
         '<=>',
         '??',
+        '??=',
     ];
 
     /**
@@ -463,6 +464,10 @@ $foo = \json_encode($bar, JSON_PRESERVE_ZERO_FRACTION | JSON_PRETTY_PRINT);
             unset($operators['??']);
         }
 
+        if (!\defined('T_COALESCE_EQUAL')) {
+            unset($operators['??=']);
+        }
+
         return $operators;
     }
 

+ 4 - 0
src/Tokenizer/TokensAnalyzer.php

@@ -553,6 +553,10 @@ final class TokensAnalyzer
             if (\defined('T_COALESCE')) {
                 $arrayOperators[T_COALESCE] = true;  // ??
             }
+
+            if (\defined('T_COALESCE_EQUAL')) {
+                $arrayOperators[T_COALESCE_EQUAL] = true;  // ??=
+            }
         }
 
         $tokens = $this->tokens;

+ 27 - 0
tests/Fixer/Operator/BinaryOperatorSpacesFixerTest.php

@@ -2128,4 +2128,31 @@ $a = $ae?? $b;
             ],
         ];
     }
+
+    /**
+     * @param string      $expected
+     * @param null|string $input
+     *
+     * @dataProvider provideFixPhp74Cases
+     * @requires PHP 7.4
+     */
+    public function testFixPhp74($expected, $input = null, array $configration = null)
+    {
+        if (null !== $configration) {
+            $this->fixer->configure($configration);
+        }
+
+        $this->doTest($expected, $input);
+    }
+
+    public function provideFixPhp74Cases()
+    {
+        return [
+            [
+                '<?php $a ??= 1;',
+                '<?php $a??=1;',
+                ['operators' => ['??=' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE]],
+            ],
+        ];
+    }
 }

+ 29 - 0
tests/Tokenizer/TokensAnalyzerTest.php

@@ -1235,6 +1235,35 @@ $b;',
         ];
     }
 
+    /**
+     * @param string $source
+     *
+     * @dataProvider provideIsBinaryOperator74Cases
+     * @requires PHP 7.4
+     */
+    public function testIsBinaryOperator74($source, array $expected)
+    {
+        $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
+
+        foreach ($expected as $index => $isBinary) {
+            static::assertSame($isBinary, $tokensAnalyzer->isBinaryOperator($index));
+            if ($isBinary) {
+                static::assertFalse($tokensAnalyzer->isUnarySuccessorOperator($index));
+                static::assertFalse($tokensAnalyzer->isUnaryPredecessorOperator($index));
+            }
+        }
+    }
+
+    public function provideIsBinaryOperator74Cases()
+    {
+        return [
+            [
+                '<?php $a ??= $b;',
+                [3 => true],
+            ],
+        ];
+    }
+
     /**
      * @param string $source
      * @param int    $tokenIndex