Unverified Commit e58c490b authored by Mitch Portier's avatar Mitch Portier Committed by GitHub
Browse files

fix(VariableAnalysis): Add support for short-hand list unpacking (#2876245 by Arkener)

parent 70b03b11
Loading
Loading
Loading
Loading
+24 −14
Original line number Diff line number Diff line
@@ -1677,10 +1677,7 @@ class VariableAnalysisSniff implements Sniff
        $tokens = $phpcsFile->getTokens();

        // OK, are we within a list (...) construct?
        if (($openPtr = $this->findContainingBrackets($phpcsFile, $stackPtr)) === false) {
            return false;
        }

        if (($openPtr = $this->findContainingBrackets($phpcsFile, $stackPtr)) !== false) {
            $prevPtr = $phpcsFile->findPrevious(T_WHITESPACE, ($openPtr - 1), null, true, null, true);
            if (($prevPtr === false) || ($tokens[$prevPtr]['code'] !== T_LIST)) {
                return false;
@@ -1688,14 +1685,27 @@ class VariableAnalysisSniff implements Sniff

            // OK, we're a list (...) construct... are we being assigned to?
            $closePtr = $tokens[$openPtr]['parenthesis_closer'];
        if (($assignPtr = $this->isNextThingAnAssign($phpcsFile, $closePtr)) === false) {
            return false;
            if (($assignPtr = $this->isNextThingAnAssign($phpcsFile, $closePtr)) !== false) {
                // Yes, we're being assigned.
                $writtenPtr = $this->findWhereAssignExecuted($phpcsFile, $assignPtr);
                $this->markVariableAssignment($varName, $writtenPtr, $currScope);
                return true;
            }
        }

        // Are we within a short list [...] construct?
        $closePtr = $phpcsFile->findNext([T_WHITESPACE, T_VARIABLE, T_COMMA], ($stackPtr + 1), null, true);
        if ($tokens[$closePtr]['code'] === T_CLOSE_SHORT_ARRAY) {
            // OK, we're a short list [...] construct... are we being assigned to?
            if (($assignPtr = $this->isNextThingAnAssign($phpcsFile, $closePtr)) !== false) {
                // Yes, we're being assigned.
                $writtenPtr = $this->findWhereAssignExecuted($phpcsFile, $assignPtr);
                $this->markVariableAssignment($varName, $writtenPtr, $currScope);
                return true;
            }
        }

        return false;

    }//end checkForListAssignment()

+10 −0
Original line number Diff line number Diff line
@@ -31,3 +31,13 @@ function test4() {
    return $metatags;
  });
}

function test5() {
  $foo = [1, 2];
  [$a, $b] = $foo;

  $bar = [3, 4];
  list($x, $y) = $bar;

  return [$a, $b, $x, $y];
}