Skip to content

Commit

Permalink
Merge pull request #16 from sirbrillig/fix/argument-assign-scope
Browse files Browse the repository at this point in the history
Fix inline assign and access
  • Loading branch information
sirbrillig authored Dec 13, 2017
2 parents e666bc0 + 10c728b commit 43713f0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
20 changes: 8 additions & 12 deletions VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -566,26 +566,22 @@ protected function findWhereAssignExecuted(
// the closing bracket, if that's first.
// eg: echo (($var = 12) && ($var == 12));
$semicolonPtr = $phpcsFile->findNext(T_SEMICOLON, $stackPtr + 1, null, false, null, true);
$commaPtr = $phpcsFile->findNext(T_COMMA, $stackPtr + 1, null, false, null, true);
$closePtr = false;
if (($openPtr = $this->findContainingBrackets($phpcsFile, $stackPtr)) !== false) {
if (isset($tokens[$openPtr]['parenthesis_closer'])) {
$closePtr = $tokens[$openPtr]['parenthesis_closer'];
}
}

if ($semicolonPtr === false) {
if ($closePtr === false) {
// TODO: panic
return $stackPtr;
}
return $closePtr;
}

if ($closePtr < $semicolonPtr) {
return $closePtr;
// Return the first thing: comma, semicolon, close-bracket, or stackPtr if nothing else
$assignEndTokens = [$commaPtr, $semicolonPtr, $closePtr];
$assignEndTokens = array_filter($assignEndTokens); // remove false values
sort($assignEndTokens);
if (empty($assignEndTokens)) {
return $stackPtr;
}

return $semicolonPtr;
return $assignEndTokens[0];
}

protected function findContainingBrackets(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,10 @@ function function_with_inline_assigns() {
}
echo $var2;
}

function function_with_assigns_and_usage() {
doSomething(
$foo = 'bar',
$foo
);
}

0 comments on commit 43713f0

Please sign in to comment.