Browse Source

chore: NoMultipleStatementsPerLineFixer - be aware of PHP 8.4 property hooks (#8344)

Dariusz Rumiński 2 months ago
parent
commit
d517fbb390

+ 1 - 1
dev-tools/phpstan/baseline.php

@@ -1562,7 +1562,7 @@ $ignoreErrors[] = [
 	'path' => __DIR__ . '/../../src/Fixer/Operator/TernaryToElvisOperatorFixer.php',
 ];
 $ignoreErrors[] = [
-	'message' => '#^Offset 1\\|2\\|3\\|4\\|5\\|6\\|7\\|8\\|9\\|10\\|11\\|12\\|13\\|14 might not exist on array\\<1\\|2\\|3\\|4\\|5\\|6\\|7\\|8\\|9\\|10\\|11\\|12\\|13\\|14, array\\{start\\: array\\{int, string\\}\\|string, end\\: array\\{int, string\\}\\|string\\}\\>\\.$#',
+	'message' => '#^Offset 1\\|2\\|3\\|4\\|5\\|6\\|7\\|8\\|9\\|10\\|11\\|12\\|13\\|14\\|15 might not exist on array\\<1\\|2\\|3\\|4\\|5\\|6\\|7\\|8\\|9\\|10\\|11\\|12\\|13\\|14\\|15, array\\{start\\: array\\{int, string\\}\\|string, end\\: array\\{int, string\\}\\|string\\}\\>\\.$#',
 	'identifier' => 'offsetAccess.notFound',
 	'count' => 1,
 	'path' => __DIR__ . '/../../src/Fixer/Operator/TernaryToElvisOperatorFixer.php',

+ 10 - 0
src/Fixer/Basic/NoMultipleStatementsPerLineFixer.php

@@ -21,6 +21,7 @@ use PhpCsFixer\FixerDefinition\CodeSample;
 use PhpCsFixer\FixerDefinition\FixerDefinition;
 use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
 use PhpCsFixer\Preg;
+use PhpCsFixer\Tokenizer\CT;
 use PhpCsFixer\Tokenizer\Tokens;
 
 /**
@@ -66,6 +67,15 @@ final class NoMultipleStatementsPerLineFixer extends AbstractFixer implements Wh
                 continue;
             }
 
+            if ($tokens[$index]->isGivenKind(CT::T_PROPERTY_HOOK_BRACE_OPEN)) {
+                $index = $tokens->findBlockEnd(
+                    Tokens::BLOCK_TYPE_PROPERTY_HOOK,
+                    $index
+                );
+
+                continue;
+            }
+
             if (!$tokens[$index]->equals(';')) {
                 continue;
             }

+ 5 - 0
src/Tokenizer/Tokens.php

@@ -51,6 +51,7 @@ class Tokens extends \SplFixedArray
     public const BLOCK_TYPE_DISJUNCTIVE_NORMAL_FORM_TYPE_PARENTHESIS = 12;
     public const BLOCK_TYPE_DYNAMIC_CLASS_CONSTANT_FETCH_CURLY_BRACE = 13;
     public const BLOCK_TYPE_COMPLEX_STRING_VARIABLE = 14;
+    public const BLOCK_TYPE_PROPERTY_HOOK = 15;
 
     /**
      * Static class cache.
@@ -278,6 +279,10 @@ class Tokens extends \SplFixedArray
                     'start' => [T_DOLLAR_OPEN_CURLY_BRACES, '${'],
                     'end' => [CT::T_DOLLAR_CLOSE_CURLY_BRACES, '}'],
                 ],
+                self::BLOCK_TYPE_PROPERTY_HOOK => [
+                    'start' => [CT::T_PROPERTY_HOOK_BRACE_OPEN, '{'],
+                    'end' => [CT::T_PROPERTY_HOOK_BRACE_CLOSE, '}'],
+                ],
             ];
 
             // @TODO: drop condition when PHP 8.0+ is required

+ 24 - 0
tests/Fixer/Basic/NoMultipleStatementsPerLineFixerTest.php

@@ -87,4 +87,28 @@ final class NoMultipleStatementsPerLineFixerTest extends AbstractFixerTestCase
             '<?php switch ($foo): case true: foo(); endswitch;',
         ];
     }
+
+    /**
+     * @dataProvider provideFix84Cases
+     *
+     * @requires PHP 8.4
+     */
+    public function testFix84(string $expected, ?string $input = null): void
+    {
+        $this->doTest($expected, $input);
+    }
+
+    /**
+     * @return iterable<string, array{string, 1?: string}>
+     */
+    public static function provideFix84Cases(): iterable
+    {
+        yield "don't touch property hooks" => [
+            '<?php interface I {
+    public string $readable { get; }
+    public string $writeable { set; }
+    public string $both { get; set; }
+}',
+        ];
+    }
 }