Skip to content

Commit

Permalink
Fix broken foreach variable identification (#36)
Browse files Browse the repository at this point in the history
* Tests: add foreach failure to fixture

* Tests: Add PHP 7.1 shorthand list syntax to foreach fixture

* Add Helpers::findParenthesisOwner

* Ignore list keyword in foreach condition

* Tests: temporarily disable failing undeclared test

We need to fix the major bug here and this is relatively minor.
  • Loading branch information
sirbrillig authored Feb 13, 2018
1 parent 793a6d4 commit a5a01ba
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
5 changes: 5 additions & 0 deletions VariableAnalysis/Lib/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ public static function findContainingOpeningBracket(File $phpcsFile, int $stackP
return false;
}

public static function findParenthesisOwner(File $phpcsFile, int $stackPtr) {
$tokens = $phpcsFile->getTokens();
return $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true);
}

public static function areAnyConditionsAClosure(File $phpcsFile, array $conditions) {
// self within a closure is invalid
$tokens = $phpcsFile->getTokens();
Expand Down
25 changes: 19 additions & 6 deletions VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -537,17 +537,30 @@ protected function checkForForeachLoopVar(File $phpcsFile, $stackPtr, $varName,
$token = $tokens[$stackPtr];

// Are we a foreach loopvar?
$lastStatementPtr = $phpcsFile->findPrevious(T_SEMICOLON, $stackPtr);
if ($lastStatementPtr === false) {
$lastStatementPtr = 0;
$openParenPtr = Helpers::findContainingOpeningBracket($phpcsFile, $stackPtr);
if ($openParenPtr === false) {
return false;
}
$openPtr = $phpcsFile->findPrevious(T_FOREACH, $stackPtr, $lastStatementPtr);
if ($openPtr === false) {
$foreachPtr = Helpers::findParenthesisOwner($phpcsFile, $openParenPtr);
if ($foreachPtr === false) {
return false;
}
if ($tokens[$foreachPtr]['code'] === T_LIST) {
$openParenPtr = Helpers::findContainingOpeningBracket($phpcsFile, $foreachPtr);
if ($openParenPtr === false) {
return false;
}
$foreachPtr = Helpers::findParenthesisOwner($phpcsFile, $openParenPtr);
if ($foreachPtr === false) {
return false;
}
}
if ($tokens[$foreachPtr]['code'] !== T_FOREACH) {
return false;
}

// Is there an 'as' token between us and the foreach?
if ($phpcsFile->findPrevious(T_AS, $stackPtr - 1, $openPtr) === false) {
if ($phpcsFile->findPrevious(T_AS, $stackPtr - 1, $openParenPtr) === false) {
return false;
}

Expand Down
4 changes: 4 additions & 0 deletions VariableAnalysis/Tests/CodeAnalysis/VariableAnalysisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ public function testFunctionWithForeachWarnings() {
50,
52,
54,
// FIXME: this is an unused variable that needs to be fixed but for now
// we will ignore it. See
// https://github.com/sirbrillig/phpcs-variable-analysis/pull/36
// 67,
];
$this->assertEquals($expectedWarnings, $lines);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,22 @@ function function_with_defined_foreach() {
foreach ($data as $val) {
echo json_encode($val);
}
foreach ($data as $val) {
foreach( $val as $el ) {
echo "hi";
}
}
foreach ($data as list($name, $label)) {
printf('<div id="%s">%s</div>', $name, $label);
}
foreach ($data as [$first, $second]) {
printf('<div id="%s">%s</div>', $first, $second);
}
function doSomethingLoopy($receipts) {
foreach ( $receipts as &$receipt ) {
$items = $receipt->items;
foreach ( $items AS $item ) {
$receipt->receipt_items[] = array_filter( $item );
}
}
}

0 comments on commit a5a01ba

Please sign in to comment.