Browse Source

Ensure compatibility with PHP 7.4

Julien Falque 5 years ago
parent
commit
fd4130a291

+ 19 - 2
src/Fixer/FunctionNotation/NullableTypeDeclarationForDefaultNullValueFixer.php

@@ -57,7 +57,19 @@ final class NullableTypeDeclarationForDefaultNullValueFixer extends AbstractFixe
      */
     public function isCandidate(Tokens $tokens)
     {
-        return \PHP_VERSION_ID >= 70100 && $tokens->isAllTokenKindsFound([T_FUNCTION, T_VARIABLE]);
+        if (\PHP_VERSION_ID < 70100) {
+            return false;
+        }
+
+        if (!$tokens->isTokenKindFound(T_VARIABLE)) {
+            return false;
+        }
+
+        if (\PHP_VERSION_ID >= 70400 && $tokens->isTokenKindFound(T_FN)) {
+            return true;
+        }
+
+        return $tokens->isTokenKindFound(T_FUNCTION);
     }
 
     /**
@@ -89,10 +101,15 @@ final class NullableTypeDeclarationForDefaultNullValueFixer extends AbstractFixe
     {
         $functionsAnalyzer = new FunctionsAnalyzer();
 
+        $tokenKinds = [T_FUNCTION];
+        if (\PHP_VERSION_ID >= 70400) {
+            $tokenKinds[] = T_FN;
+        }
+
         for ($index = $tokens->count() - 1; $index >= 0; --$index) {
             $token = $tokens[$index];
 
-            if (!$token->isGivenKind(T_FUNCTION)) {
+            if (!$token->isGivenKind($tokenKinds)) {
                 continue;
             }
 

+ 4 - 0
src/Fixer/Phpdoc/PhpdocLineSpanFixer.php

@@ -20,6 +20,7 @@ use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
 use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
 use PhpCsFixer\FixerDefinition\CodeSample;
 use PhpCsFixer\FixerDefinition\FixerDefinition;
+use PhpCsFixer\Tokenizer\CT;
 use PhpCsFixer\Tokenizer\Token;
 use PhpCsFixer\Tokenizer\Tokens;
 use PhpCsFixer\Tokenizer\TokensAnalyzer;
@@ -130,6 +131,9 @@ final class PhpdocLineSpanFixer extends AbstractFixer implements WhitespacesAwar
             T_COMMENT,
             T_VAR,
             T_STATIC,
+            T_STRING,
+            T_NS_SEPARATOR,
+            CT::T_NULLABLE_TYPE,
         ]));
 
         return $index;

+ 58 - 0
tests/Fixer/FunctionNotation/NullableTypeDeclarationForDefaultNullValueFixerTest.php

@@ -299,4 +299,62 @@ final class NullableTypeDeclarationForDefaultNullValueFixerTest extends Abstract
             '<?php function foo(? /*comment*/ string $param = null) {}',
         ];
     }
+
+    /**
+     * @param string $input
+     * @param string $expected
+     *
+     * @dataProvider provideFixPhp74Cases
+     * @requires PHP 7.4
+     */
+    public function testFixPhp74($input, $expected)
+    {
+        $this->doTest($expected, $input);
+    }
+
+    /**
+     * @param string $input
+     * @param string $expected
+     *
+     * @dataProvider provideFixPhp74Cases
+     * @requires PHP 7.4
+     */
+    public function testFixInversePhp74($expected, $input)
+    {
+        $this->fixer->configure(['use_nullable_type_declaration' => false]);
+
+        $this->doTest($expected, $input);
+    }
+
+    public function provideFixPhp74Cases()
+    {
+        yield [
+            '<?php $foo = fn (string $param = null) => null;',
+            '<?php $foo = fn (?string $param = null) => null;',
+        ];
+        yield [
+            '<?php $foo = fn (string &$param = null) => null;',
+            '<?php $foo = fn (?string &$param = null) => null;',
+        ];
+        yield [
+            '<?php $foo = fn (Baz $param = null) => null;',
+            '<?php $foo = fn (?Baz $param = null) => null;',
+        ];
+        yield [
+            '<?php $foo = fn (Baz &$param = null) => null;',
+            '<?php $foo = fn (?Baz &$param = null) => null;',
+        ];
+        yield [
+            '<?php $foo = fn (Baz & $param = null) => null;',
+            '<?php $foo = fn (?Baz & $param = null) => null;',
+        ];
+        yield [
+            '<?php $foo = fn(array $a = null,
+                    array $b = null, array     $c = null, array
+                    $d = null) => null;',
+            '<?php $foo = fn(?array $a = null,
+                    ?array $b = null, ?array     $c = null, ?array
+                    $d = null) => null;',
+        ];
+    }
 }

+ 40 - 0
tests/Fixer/Phpdoc/PhpdocLineSpanFixerTest.php

@@ -429,6 +429,46 @@ class Foo
      *
      */
     private $foo;
+}',
+                [
+                    'property' => 'single',
+                ],
+            ],
+        ];
+    }
+
+    /**
+     * @requires PHP 7.4
+     * @dataProvider provideFixPhp74Cases
+     *
+     * @param string $expected
+     * @param string $input
+     */
+    public function testFixPhp74($expected, $input = null, array $config = [])
+    {
+        $this->fixer->configure($config);
+        $this->doTest($expected, $input);
+    }
+
+    public function provideFixPhp74Cases()
+    {
+        return [
+            'It can handle properties with type declaration' => [
+                '<?php
+
+class Foo
+{
+    /**  */
+    private ?string $foo;
+}',
+                '<?php
+
+class Foo
+{
+    /**
+     *
+     */
+    private ?string $foo;
 }',
                 [
                     'property' => 'single',