Unverified Commit 2f63f218 authored by Klaus Purer's avatar Klaus Purer Committed by GitHub
Browse files

fix(VariableAnalysis): Fix false positive for unused variables in closures with foreach loops

parent 7cd84911
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -1785,7 +1785,9 @@ class VariableAnalysisSniff implements Sniff
        }

        // Is there an 'as' token between us and the opening bracket?
        if ($phpcsFile->findPrevious(T_AS, ($stackPtr - 1), $openPtr) === false) {
        if (($asPtr = $phpcsFile->findPrevious(T_AS, ($stackPtr - 1), $openPtr)) === false
            || $this->findContainingBrackets($phpcsFile, $asPtr) !== $openPtr
        ) {
            return false;
        }

+10 −0
Original line number Diff line number Diff line
@@ -21,3 +21,13 @@ function test3() {
  }
  return $x;
}

function test4() {
  return new Foo(function () {
    $metatags = [];
    foreach ([1, 2] as $u) {
      $metatags[] = $u;
    }
    return $metatags;
  });
}