Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

…s in a call
  • Loading branch information...
commit 920b8a6a3b1845a8e8d60325f553d7d70a69672e 1 parent a6a5ae8
Greg Sherwood authored October 29, 2012
39  CodeSniffer/Standards/Generic/Sniffs/WhiteSpace/ScopeIndentSniff.php
@@ -342,21 +342,36 @@ protected function calculateExpectedIndent(array $tokens, $stackPtr)
342 342
 
343 343
         $tokenConditions = $tokens[$stackPtr]['conditions'];
344 344
         foreach ($tokenConditions as $id => $condition) {
345  
-            // If it's an indenting scope ie. it's not in our array of
346  
-            // scopes that don't indent, increase indent.
347  
-            if (in_array($condition, $this->nonIndentingScopes) === false) {
348  
-                if ($condition === T_CLOSURE && $inParenthesis === true) {
349  
-                    // Closures cause problems with indents when they are
350  
-                    // used as function arguments because the code inside them
351  
-                    // is not technically inside the function yet, so the indent
352  
-                    // is always off by one. So instead, use the
353  
-                    // indent of the closure as the base value.
354  
-                    $indent = ($tokens[$id]['column'] - 1);
  345
+            // If it's not an indenting scope i.e., it's in our array of
  346
+            // scopes that don't indent, skip it.
  347
+            if (in_array($condition, $this->nonIndentingScopes) === true) {
  348
+                continue;
  349
+            }
  350
+
  351
+            if ($condition === T_CLOSURE && $inParenthesis === true) {
  352
+                // Closures cause problems with indents when they are
  353
+                // used as function arguments because the code inside them
  354
+                // is not technically inside the function yet, so the indent
  355
+                // is always off by one. So instead, use the
  356
+                // indent of the closure as the base value.
  357
+                $lastContent = $id;
  358
+                for ($i = ($id - 1); $i > 0; $i--) {
  359
+                    if ($tokens[$i]['line'] !== $tokens[$id]['line']) {
  360
+                        // Changed lines, so the last content we saw is what
  361
+                        // we want.
  362
+                        break;
  363
+                    }
  364
+
  365
+                    if (in_array($tokens[$i]['code'], PHP_CodeSniffer_Tokens::$emptyTokens) === false) {
  366
+                        $lastContent = $i;
  367
+                    }
355 368
                 }
356 369
 
357  
-                $indent += $this->indent;
  370
+                $indent = ($tokens[$lastContent]['column'] - 1);
358 371
             }
359  
-        }
  372
+
  373
+            $indent += $this->indent;
  374
+        }//end foreach
360 375
 
361 376
         // Increase by 1 to indiciate that the code should start at a specific column.
362 377
         // E.g., code indented 4 spaces should start at column 5.
10  CodeSniffer/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.inc
@@ -410,4 +410,14 @@ and the ending STRING on the next line is reported as having to be indented.
410 410
 STRING
411 411
     );
412 412
 }
  413
+
  414
+$var = call_user_func(
  415
+    $new_var = function () use (&$a) {
  416
+        if ($a > 0) {
  417
+            return $a++;
  418
+        } else {
  419
+            return $a--;
  420
+        }
  421
+    }
  422
+);
413 423
 ?>
1  package.xml
@@ -62,6 +62,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
62 62
   - Fixed bug #19629 : PSR2 error for inline comments on multi-line argument lists
63 63
   - Fixed bug #19644 : Alternative syntax, e.g. if/endif triggers Inline Control Structure error
64 64
   - Fixed bug #19655 : Closures reporting as multi-line when they are not
  65
+  - Fixed bug #19675 : Improper indent of nested anonymous function bodies in a call
65 66
  </notes>
66 67
  <contents>
67 68
   <dir name="/">

0 notes on commit 920b8a6

Please sign in to comment.
Something went wrong with that request. Please try again.