Browse Source

bug: PhpdocAddMissingParamAnnotationFixer - fix for promoted properties (#7090)

Kuba Werłos 1 year ago
parent
commit
84c7f80000

+ 11 - 2
src/Fixer/Phpdoc/PhpdocAddMissingParamAnnotationFixer.php

@@ -27,6 +27,7 @@ use PhpCsFixer\FixerDefinition\FixerDefinition;
 use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
 use PhpCsFixer\Preg;
 use PhpCsFixer\Tokenizer\Analyzer\ArgumentsAnalyzer;
+use PhpCsFixer\Tokenizer\CT;
 use PhpCsFixer\Tokenizer\Token;
 use PhpCsFixer\Tokenizer\Tokens;
 
@@ -128,7 +129,6 @@ function f9(string $foo, $bar, $baz) {}
                 T_PROTECTED,
                 T_PUBLIC,
                 T_STATIC,
-                T_VAR,
             ])) {
                 $index = $tokens->getNextMeaningfulToken($index);
             }
@@ -232,7 +232,16 @@ function f9(string $foo, $bar, $baz) {}
         for ($index = $start; $index <= $end; ++$index) {
             $token = $tokens[$index];
 
-            if ($token->isComment() || $token->isWhitespace()) {
+            if (
+                $token->isComment()
+                || $token->isWhitespace()
+                || $token->isGivenKind([
+                    CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PRIVATE,
+                    CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PROTECTED,
+                    CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PUBLIC,
+                ])
+                || (\defined('T_READONLY') && $token->isGivenKind(T_READONLY))
+            ) {
                 continue;
             }
 

+ 73 - 0
tests/Fixer/Phpdoc/PhpdocAddMissingParamAnnotationFixerTest.php

@@ -493,4 +493,77 @@ final class PhpdocAddMissingParamAnnotationFixerTest extends AbstractFixerTestCa
             ],
         ];
     }
+
+    /**
+     * @dataProvider provideFix80Cases
+     *
+     * @requires PHP 8.0
+     */
+    public function testFix80(string $expected, ?string $input = null): void
+    {
+        $this->fixer->configure(['only_untyped' => false]);
+        $this->doTest($expected, $input);
+    }
+
+    public static function provideFix80Cases(): iterable
+    {
+        yield [
+            '<?php class Foo {
+                /**
+                 * @param Bar $x
+                 * @param ?Bar $y
+                 * @param null|Bar $z
+                 */
+                public function __construct(
+                    public Bar $x,
+                    protected ?Bar $y,
+                    private null|Bar $z,
+                ) {}
+            }',
+            '<?php class Foo {
+                /**
+                 */
+                public function __construct(
+                    public Bar $x,
+                    protected ?Bar $y,
+                    private null|Bar $z,
+                ) {}
+            }',
+        ];
+    }
+
+    /**
+     * @dataProvider provideFix81Cases
+     *
+     * @requires PHP 8.1
+     */
+    public function testFix81(string $expected, ?string $input = null): void
+    {
+        $this->fixer->configure(['only_untyped' => false]);
+        $this->doTest($expected, $input);
+    }
+
+    public static function provideFix81Cases(): iterable
+    {
+        yield [
+            '<?php class Foo {
+                /**
+                 * @param Bar $bar
+                 * @param Baz $baz
+                 */
+                public function __construct(
+                    public readonly Bar $bar,
+                    readonly public Baz $baz,
+                ) {}
+            }',
+            '<?php class Foo {
+                /**
+                 */
+                public function __construct(
+                    public readonly Bar $bar,
+                    readonly public Baz $baz,
+                ) {}
+            }',
+        ];
+    }
 }