Browse Source

VisibilityRequiredFixer - support type alternation for properties

Dariusz Ruminski 4 years ago
parent
commit
6081f933ff

+ 1 - 1
src/Fixer/ClassNotation/VisibilityRequiredFixer.php

@@ -97,7 +97,7 @@ class Sample
     protected function applyFix(\SplFileInfo $file, Tokens $tokens)
     {
         $tokensAnalyzer = new TokensAnalyzer($tokens);
-        $propertyTypeDeclarationKinds = [T_STRING, T_NS_SEPARATOR, CT::T_NULLABLE_TYPE, CT::T_ARRAY_TYPEHINT];
+        $propertyTypeDeclarationKinds = [T_STRING, T_NS_SEPARATOR, CT::T_NULLABLE_TYPE, CT::T_ARRAY_TYPEHINT, CT::T_TYPE_ALTERNATION];
 
         foreach (array_reverse($tokensAnalyzer->getClassyElements(), true) as $index => $element) {
             if (!\in_array($element['type'], $this->configuration['elements'], true)) {

+ 23 - 0
tests/Fixer/ClassNotation/VisibilityRequiredFixerTest.php

@@ -827,4 +827,27 @@ AB# <- this is the name
             '<?php class Foo { static public ?array $foo; }',
         ];
     }
+
+    /**
+     * @param string      $expected
+     * @param null|string $input
+     *
+     * @requires PHP 8.0
+     * @dataProvider provideFix80Cases
+     */
+    public function testFix80($expected, $input = null)
+    {
+        $this->doTest($expected, $input);
+    }
+
+    public function provideFix80Cases()
+    {
+        yield [
+            '<?php class Foo { private int|float|null $foo; }',
+        ];
+
+        yield [
+            '<?php class Foo { private int | /* or empty */ null $foo; }',
+        ];
+    }
 }