Browse Source

Short array destructing support.

SpacePossum 8 years ago
parent
commit
075f0d3575

+ 11 - 1
src/Fixer/Phpdoc/PhpdocToCommentFixer.php

@@ -15,6 +15,7 @@ namespace PhpCsFixer\Fixer\Phpdoc;
 use PhpCsFixer\AbstractFixer;
 use PhpCsFixer\FixerDefinition\CodeSample;
 use PhpCsFixer\FixerDefinition\FixerDefinition;
+use PhpCsFixer\Tokenizer\CT;
 use PhpCsFixer\Tokenizer\Token;
 use PhpCsFixer\Tokenizer\Tokens;
 
@@ -78,10 +79,12 @@ foreach($connections as $key => $sqlite) {
             T_WHILE,
             T_FOR,
         );
+
         static $languageStructures = array(
             T_LIST,
             T_PRINT,
             T_ECHO,
+            CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
         );
 
         foreach ($tokens as $index => $token) {
@@ -94,6 +97,7 @@ foreach($connections as $key => $sqlite) {
 
             if (null === $nextToken || $nextToken->equals('}')) {
                 $tokens->overrideAt($index, array(T_COMMENT, '/*'.ltrim($token->getContent(), '/*')));
+
                 continue;
             }
 
@@ -194,7 +198,13 @@ foreach($connections as $key => $sqlite) {
      */
     private function isValidLanguageConstruct(Tokens $tokens, Token $docsToken, $languageConstructIndex)
     {
-        $endIndex = $tokens->getNextTokenOfKind($languageConstructIndex, array(')'));
+        $endKind = $tokens[$languageConstructIndex]->isGivenKind(CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN)
+            ? array(CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE)
+            : ')'
+        ;
+
+        $endIndex = $tokens->getNextTokenOfKind($languageConstructIndex, array($endKind));
+
         $docsContent = $docsToken->getContent();
 
         for ($index = $languageConstructIndex + 1; $index < $endIndex; ++$index) {

+ 48 - 0
tests/Fixer/Phpdoc/PhpdocToCommentFixerTest.php

@@ -552,4 +552,52 @@ trait DocBlocks
             ),
         );
     }
+
+    /**
+     * @param string      $expected
+     * @param null|string $input
+     *
+     * @dataProvider provideCases71
+     * @requires PHP 7.1
+     */
+    public function testFix71($expected, $input = null)
+    {
+        $this->doTest($expected, $input);
+    }
+
+    public function provideCases71()
+    {
+        return array(
+            array(
+                '<?php
+$first = true;// needed because by default first docblock is never fixed.
+
+/** @var int $a */
+[$a] = $b;
+
+/* @var int $c */
+[$a] = $c;
+                ',
+                '<?php
+$first = true;// needed because by default first docblock is never fixed.
+
+/** @var int $a */
+[$a] = $b;
+
+/** @var int $c */
+[$a] = $c;
+                ',
+            ),
+            array(
+                '<?php
+$first = true;// needed because by default first docblock is never fixed.
+
+/**
+ * @var int $a
+ */
+[$a] = $b;
+                ',
+            ),
+        );
+    }
 }