Browse Source

PHP8.1 - type "never" support

SpacePossum 3 years ago
parent
commit
b6fa37f82e

+ 1 - 0
src/DocBlock/TypeExpression.php

@@ -244,6 +244,7 @@ final class TypeExpression
             'int',
             'iterable',
             'mixed',
+            'never',
             'null',
             'object',
             'resource',

+ 5 - 0
src/Fixer/Casing/NativeFunctionTypeDeclarationCasingFixer.php

@@ -42,6 +42,7 @@ final class NativeFunctionTypeDeclarationCasingFixer extends AbstractFixer
      * object   PHP 7.2
      * static   PHP 8.0 (return type only)
      * mixed    PHP 8.0
+     * never    PHP 8.1
      *
      * @var array<string, true>
      */
@@ -89,6 +90,10 @@ final class NativeFunctionTypeDeclarationCasingFixer extends AbstractFixer
             $this->hints = array_merge($this->hints, ['mixed' => true]);
         }
 
+        if (\PHP_VERSION_ID >= 80100) {
+            $this->hints = array_merge($this->hints, ['never' => true]);
+        }
+
         $this->functionsAnalyzer = new FunctionsAnalyzer();
     }
 

+ 1 - 1
src/Fixer/FunctionNotation/PhpdocToPropertyTypeFixer.php

@@ -145,7 +145,7 @@ class Foo {
 
             [$propertyType, $isNullable] = $typeInfo;
 
-            if (\in_array($propertyType, ['void', 'callable'], true)) {
+            if (\in_array($propertyType, ['callable', 'never', 'void'], true)) {
                 continue;
             }
 

+ 1 - 0
src/Tokenizer/Analyzer/Analysis/TypeAnalysis.php

@@ -38,6 +38,7 @@ final class TypeAnalysis implements StartEndTokenAwareAnalysis
         'int',
         'iterable',
         'mixed',
+        'never',
         'numeric',
         'object',
         'resource',

+ 1 - 1
src/Tokenizer/Analyzer/ClassyAnalyzer.php

@@ -30,7 +30,7 @@ final class ClassyAnalyzer
             throw new \LogicException(sprintf('No T_STRING at given index %d, got "%s".', $index, $tokens[$index]->getName()));
         }
 
-        if (\in_array(strtolower($token->getContent()), ['bool', 'float', 'int', 'iterable', 'object', 'parent', 'self', 'string', 'void', 'null', 'false'], true)) {
+        if (\in_array(strtolower($token->getContent()), ['bool', 'float', 'int', 'iterable', 'object', 'parent', 'self', 'string', 'void', 'null', 'false', 'never'], true)) {
             return false;
         }
 

+ 1 - 0
tests/DocBlock/TypeExpressionTest.php

@@ -144,6 +144,7 @@ final class TypeExpressionTest extends TestCase
         yield ['int|null', 'int'];
         yield ['null|int', 'int'];
         yield ['void', 'void'];
+        yield ['never', 'never'];
         yield ['array|Traversable', 'iterable', null, [$useTraversable]];
         yield ['array|Traversable', 'iterable', $globalNamespace, [$useTraversable]];
         yield ['array|Traversable', 'iterable', $appNamespace, [$useTraversable]];

+ 17 - 0
tests/Fixer/Casing/NativeFunctionTypeDeclarationCasingFixerTest.php

@@ -204,4 +204,21 @@ function Foo(INTEGER $a) {}
             '<?php function foo(): INT|BOOL {}',
         ];
     }
+
+    /**
+     * @dataProvider provideFix81Cases
+     * @requires PHP 8.1
+     */
+    public function testFix81(string $expected, string $input): void
+    {
+        $this->doTest($expected, $input);
+    }
+
+    public function provideFix81Cases(): \Generator
+    {
+        yield [
+            '<?php class T { public function Foo(object $A): never {die;}}',
+            '<?php class T { public function Foo(object $A): NEVER {die;}}',
+        ];
+    }
 }

+ 3 - 0
tests/Fixer/FunctionNotation/PhpdocToPropertyTypeFixerTest.php

@@ -79,6 +79,9 @@ final class PhpdocToPropertyTypeFixerTest extends AbstractFixerTestCase
             'void' => [
                 '<?php class Foo { /** @var void */ private $foo; }',
             ],
+            'never' => [
+                '<?php class Foo { /** @var never */ private $foo; }',
+            ],
             'iterable' => [
                 '<?php class Foo { /** @var iterable */ private iterable $foo; }',
                 '<?php class Foo { /** @var iterable */ private $foo; }',

+ 25 - 0
tests/Fixtures/Integration/misc/PHP8_1.test

@@ -0,0 +1,25 @@
+--TEST--
+PHP 8.1 test.
+--RULESET--
+{
+    "@PhpCsFixer": true
+}
+--REQUIREMENTS--
+{"php": 80100}
+--EXPECT--
+<?php
+
+// https://wiki.php.net/rfc/noreturn_type
+function endProgram(): never
+{
+    exit();
+}
+
+--INPUT--
+<?php
+
+// https://wiki.php.net/rfc/noreturn_type
+function endProgram(): NEVER
+{
+    die();
+}

+ 1 - 0
tests/Tokenizer/Analyzer/Analysis/TypeAnalysisTest.php

@@ -75,6 +75,7 @@ final class TypeAnalysisTest extends TestCase
             ['int', true],
             ['iterable', true],
             ['mixed', true],
+            ['never', true],
             ['numeric', true],
             ['object', true],
             ['other', false],

Some files were not shown because too many files changed in this diff