Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed bug #19675 : Improper indent of nested anonymous function bodie…

…s in a call
  • Loading branch information...
commit 920b8a6a3b1845a8e8d60325f553d7d70a69672e 1 parent a6a5ae8
Greg Sherwood gsherwood authored
39 CodeSniffer/Standards/Generic/Sniffs/WhiteSpace/ScopeIndentSniff.php
View
@@ -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.
10 CodeSniffer/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.inc
View
@@ -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  package.xml
View
@@ -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="/">
Please sign in to comment.
Something went wrong with that request. Please try again.