Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 );
}
}
}