Browse Source

feat: no_superfluous_phpdoc_tags - also cover ?type (#8125)

Dariusz Rumiński 7 months ago
parent
commit
b09e8aa52b

+ 9 - 0
src/Fixer/Phpdoc/NoSuperfluousPhpdocTagsFixer.php

@@ -640,6 +640,15 @@ class Foo {
      */
     private function toComparableNames(array $types, ?string $namespace, ?string $currentSymbol, array $symbolShortNames): array
     {
+        \assert(0 !== \count($types));
+
+        if ('?' === $types[0][0]) {
+            $types = [
+                substr($types[0], 1),
+                'null',
+            ];
+        }
+
         $normalized = array_map(
             function (string $type) use ($namespace, $currentSymbol, $symbolShortNames): string {
                 if (str_contains($type, '&')) {

+ 95 - 0
tests/Fixer/Phpdoc/NoSuperfluousPhpdocTagsFixerTest.php

@@ -1519,6 +1519,25 @@ class Foo {
 }',
         ];
 
+        yield 'same ?type declaration' => [
+            '<?php
+class Foo {
+    /**
+     *
+     */
+    public function doFoo(?Bar $bar): ?Baz {}
+}',
+            '<?php
+class Foo {
+    /**
+     * @param ?Bar $bar
+     *
+     * @return ?Baz
+     */
+    public function doFoo(?Bar $bar): ?Baz {}
+}',
+        ];
+
         yield 'same nullable type declaration with description' => [
             '<?php
 class Foo {
@@ -2863,6 +2882,82 @@ new #[Bar] class() extends Foo {
     public function bar(self $other, int $superfluous): self {}
 };',
         ];
+
+        yield 'same nullable type declaration v2' => [
+            '<?php
+class Foo {
+    /**
+     *
+     */
+    public function doFoo(null|Bar $bar): ?Baz {}
+}',
+            '<?php
+class Foo {
+    /**
+     * @param Bar|null $bar
+     *
+     * @return Baz|null
+     */
+    public function doFoo(null|Bar $bar): ?Baz {}
+}',
+        ];
+
+        yield 'same nullable type declaration v3' => [
+            '<?php
+class Foo {
+    /**
+     *
+     */
+    public function doFoo(Bar|null $bar): ?Baz {}
+}',
+            '<?php
+class Foo {
+    /**
+     * @param Bar|null $bar
+     *
+     * @return Baz|null
+     */
+    public function doFoo(Bar|null $bar): ?Baz {}
+}',
+        ];
+
+        yield 'same ?type declaration v2' => [
+            '<?php
+class Foo {
+    /**
+     *
+     */
+    public function doFoo(null|Bar $bar): ?Baz {}
+}',
+            '<?php
+class Foo {
+    /**
+     * @param ?Bar $bar
+     *
+     * @return ?Baz
+     */
+    public function doFoo(null|Bar $bar): ?Baz {}
+}',
+        ];
+
+        yield 'same ?type declaration v3' => [
+            '<?php
+class Foo {
+    /**
+     *
+     */
+    public function doFoo(Bar|null $bar): ?Baz {}
+}',
+            '<?php
+class Foo {
+    /**
+     * @param ?Bar $bar
+     *
+     * @return ?Baz
+     */
+    public function doFoo(Bar|null $bar): ?Baz {}
+}',
+        ];
     }
 
     /**

+ 0 - 2
tests/Tokenizer/TokenTest.php

@@ -208,8 +208,6 @@ final class TokenTest extends TestCase
     }
 
     /**
-     * @param ?int $tokenId
-     *
      * @dataProvider provideIsMagicConstantCases
      */
     public function testIsMagicConstant(?int $tokenId, string $content, bool $isConstant = true): void

+ 0 - 1
tests/Tokenizer/TokensTest.php

@@ -647,7 +647,6 @@ final class TokensTest extends TestCase
     }
 
     /**
-     * @param ?int                          $expectedIndex
      * @param -1|1                          $direction
      * @param list<array{int}|string|Token> $findTokens
      *