Skip to content

Commit

Permalink
Support shorthand list assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
sirbrillig committed Sep 11, 2018
1 parent 7bcce48 commit f44a68c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
19 changes: 19 additions & 0 deletions VariableAnalysis/Lib/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,25 @@
use PHP_CodeSniffer\Files\File;

class Helpers {
public static function findContainingOpeningSquareBracket(File $phpcsFile, $stackPtr) {
$tokens = $phpcsFile->getTokens();
$previousStatementPtr = self::getPreviousStatementPtr($phpcsFile, $stackPtr);
return $phpcsFile->findPrevious(T_OPEN_SHORT_ARRAY, $stackPtr - 1, $previousStatementPtr);
}

public static function findContainingClosingSquareBracket(File $phpcsFile, $stackPtr) {
$tokens = $phpcsFile->getTokens();
$endOfStatementPtr = $phpcsFile->findNext([T_SEMICOLON], $stackPtr + 1);
if (! $endOfStatementPtr) {
return false;
}
return $phpcsFile->findNext(T_CLOSE_SHORT_ARRAY, $stackPtr + 1, $endOfStatementPtr);
}

public static function getPreviousStatementPtr(File $phpcsFile, $stackPtr) {
return $phpcsFile->findPrevious([T_SEMICOLON, T_CLOSE_CURLY_BRACKET], $stackPtr - 1) ?: 1;
}

public static function findContainingOpeningBracket(File $phpcsFile, $stackPtr) {
$tokens = $phpcsFile->getTokens();
if (isset($tokens[$stackPtr]['nested_parenthesis'])) {
Expand Down
28 changes: 28 additions & 0 deletions VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,29 @@ protected function checkForAssignment(File $phpcsFile, $stackPtr, $varName, $cur
return true;
}

protected function checkForListShorthandAssignment(File $phpcsFile, $stackPtr, $varName, $currScope) {
$tokens = $phpcsFile->getTokens();
$token = $tokens[$stackPtr];

// OK, are we within a [ ... ] construct?
$openPtr = Helpers::findContainingOpeningSquareBracket($phpcsFile, $stackPtr);
if ($openPtr === false) {
return false;
}

// OK, we're a [ ... ] construct... are we being assigned to?
$closePtr = Helpers::findContainingClosingSquareBracket($phpcsFile, $stackPtr);
$assignPtr = Helpers::isNextThingAnAssign($phpcsFile, $closePtr);
if ($assignPtr === false) {
return false;
}

// Yes, we're being assigned.
$writtenPtr = Helpers::findWhereAssignExecuted($phpcsFile, $assignPtr);
$this->markVariableAssignment($varName, $writtenPtr, $currScope);
return true;
}

protected function checkForListAssignment(File $phpcsFile, $stackPtr, $varName, $currScope) {
$tokens = $phpcsFile->getTokens();
$token = $tokens[$stackPtr];
Expand Down Expand Up @@ -737,6 +760,11 @@ protected function processVariable(File $phpcsFile, $stackPtr) {
return;
}

// OK, are we within a [...] = construct?
if ($this->checkForListShorthandAssignment($phpcsFile, $stackPtr, $varName, $currScope)) {
return;
}

// Are we a global declaration?
if ($this->checkForGlobalDeclaration($phpcsFile, $stackPtr, $varName, $currScope)) {
return;
Expand Down

0 comments on commit f44a68c

Please sign in to comment.