Просмотр исходного кода

bug #5596 NullableTypeTransformer - fix for attributes (kubawerlos, jrmajor)

This PR was merged into the 2.18 branch.

Discussion
----------

NullableTypeTransformer - fix for attributes

Fixes https://github.com/FriendsOfPHP/PHP-CS-Fixer/issues/5533

Commits
-------

86f5b391e Update tests/Fixer/Operator/TernaryToElvisOperatorFixerTest.php
e3f0d3725 add test reproducing #5533
62cceb9f0 Fix
58e6a4dd6 Add failing test case
Dariusz Ruminski 3 лет назад
Родитель
Сommit
f01521b6f4

+ 1 - 0
src/Tokenizer/Transformer/NullableTypeTransformer.php

@@ -62,6 +62,7 @@ final class NullableTypeTransformer extends AbstractTransformer
             [CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PUBLIC],
             [CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PROTECTED],
             [CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PRIVATE],
+            [CT::T_ATTRIBUTE_CLOSE],
             [T_PRIVATE],
             [T_PROTECTED],
             [T_PUBLIC],

+ 25 - 0
tests/Fixer/Operator/TernaryToElvisOperatorFixerTest.php

@@ -498,4 +498,29 @@ EOT
             ],
         ];
     }
+
+    /**
+     * @param string $input
+     *
+     * @dataProvider provideDoNotFix80Cases
+     * @requires PHP 8.0
+     */
+    public function test80DoNotFix($input)
+    {
+        $this->doTest($input);
+    }
+
+    public function provideDoNotFix80Cases()
+    {
+        return [
+            ['<?php
+
+function test(#[TestAttribute] ?User $user) {}
+'],
+            ['<?php
+
+function test(#[TestAttribute] ?User $user = null) {}
+'],
+        ];
+    }
 }

+ 8 - 0
tests/Tokenizer/Transformer/NullableTypeTransformerTest.php

@@ -173,6 +173,14 @@ final class NullableTypeTransformerTest extends AbstractTransformerTestCase
                     41 => CT::T_NULLABLE_TYPE,
                 ],
             ],
+            [
+                '<?php
+                    function test(#[TestAttribute] ?User $user) {}
+                ',
+                [
+                    10 => CT::T_NULLABLE_TYPE,
+                ],
+            ],
         ];
     }
 }