Skip to content

Commit

Permalink
Fixed bug #19675 : Improper indent of nested anonymous function bodie…
Browse files Browse the repository at this point in the history
…s in a call
  • Loading branch information
gsherwood committed Oct 29, 2012
1 parent a6a5ae8 commit 920b8a6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -342,21 +342,36 @@ protected function calculateExpectedIndent(array $tokens, $stackPtr)

$tokenConditions = $tokens[$stackPtr]['conditions'];
foreach ($tokenConditions as $id => $condition) {
// If it's an indenting scope ie. it's not in our array of
// scopes that don't indent, increase indent.
if (in_array($condition, $this->nonIndentingScopes) === false) {
if ($condition === T_CLOSURE && $inParenthesis === true) {
// Closures cause problems with indents when they are
// used as function arguments because the code inside them
// is not technically inside the function yet, so the indent
// is always off by one. So instead, use the
// indent of the closure as the base value.
$indent = ($tokens[$id]['column'] - 1);
// If it's not an indenting scope i.e., it's in our array of
// scopes that don't indent, skip it.
if (in_array($condition, $this->nonIndentingScopes) === true) {
continue;
}

if ($condition === T_CLOSURE && $inParenthesis === true) {
// Closures cause problems with indents when they are
// used as function arguments because the code inside them
// is not technically inside the function yet, so the indent
// is always off by one. So instead, use the
// indent of the closure as the base value.
$lastContent = $id;
for ($i = ($id - 1); $i > 0; $i--) {
if ($tokens[$i]['line'] !== $tokens[$id]['line']) {
// Changed lines, so the last content we saw is what
// we want.
break;
}

if (in_array($tokens[$i]['code'], PHP_CodeSniffer_Tokens::$emptyTokens) === false) {
$lastContent = $i;
}
}

$indent += $this->indent;
$indent = ($tokens[$lastContent]['column'] - 1);
}
}

$indent += $this->indent;
}//end foreach

// Increase by 1 to indiciate that the code should start at a specific column.
// E.g., code indented 4 spaces should start at column 5.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,4 +410,14 @@ and the ending STRING on the next line is reported as having to be indented.
STRING
);
}

$var = call_user_func(
$new_var = function () use (&$a) {
if ($a > 0) {
return $a++;
} else {
return $a--;
}
}
);
?>
1 change: 1 addition & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
- Fixed bug #19629 : PSR2 error for inline comments on multi-line argument lists
- Fixed bug #19644 : Alternative syntax, e.g. if/endif triggers Inline Control Structure error
- Fixed bug #19655 : Closures reporting as multi-line when they are not
- Fixed bug #19675 : Improper indent of nested anonymous function bodies in a call
</notes>
<contents>
<dir name="/">
Expand Down

0 comments on commit 920b8a6

Please sign in to comment.