Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed bug #19687 : Anonymous functions inside arrays marked as indent…

…ed incorrectly in PSR2
  • Loading branch information...
commit 20ea734e1a1689a8ea58106812af6e1719db8308 1 parent 2b1adc7
Greg Sherwood authored November 01, 2012
25  CodeSniffer/Standards/Generic/Sniffs/WhiteSpace/ScopeIndentSniff.php
@@ -127,13 +127,24 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
127 127
         if ($tokens[$firstToken]['code'] !== T_CLOSURE
128 128
             && $tokens[$firstToken]['column'] !== $expectedIndent
129 129
         ) {
130  
-            $error = 'Line indented incorrectly; expected %s spaces, found %s';
131  
-            $data  = array(
132  
-                      ($expectedIndent - 1),
133  
-                      ($tokens[$firstToken]['column'] - 1),
134  
-                     );
135  
-            $phpcsFile->addError($error, $stackPtr, 'Incorrect', $data);
136  
-        }
  130
+            // If the scope opener is a closure but it is not the first token on the
  131
+            // line, then the first token may be a variable or array index as so
  132
+            // should not require exact identation unless the exact member var
  133
+            // is set to TRUE.
  134
+            $exact = true;
  135
+            if ($tokens[$stackPtr]['code'] === T_CLOSURE) {
  136
+                $exact = $this->exact;
  137
+            }
  138
+
  139
+            if ($exact === true || $tokens[$firstToken]['column'] < $expectedIndent) {
  140
+                $error = 'Line indented incorrectly; expected %s spaces, found %s';
  141
+                $data  = array(
  142
+                          ($expectedIndent - 1),
  143
+                          ($tokens[$firstToken]['column'] - 1),
  144
+                         );
  145
+                $phpcsFile->addError($error, $stackPtr, 'Incorrect', $data);
  146
+            }
  147
+        }//end if
137 148
 
138 149
         $scopeOpener = $tokens[$stackPtr]['scope_opener'];
139 150
         $scopeCloser = $tokens[$stackPtr]['scope_closer'];
16  CodeSniffer/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.inc
@@ -420,4 +420,20 @@ $var = call_user_func(
420 420
         }
421 421
     }
422 422
 );
  423
+
  424
+class AnonymousFn
  425
+{
  426
+    public function getAnonFn()
  427
+    {
  428
+        return array(
  429
+            'functions' => Array(
  430
+                'function1' => function ($a, $b, $c) {
  431
+                    $a = $b + $c;
  432
+                    $b = $c / 2;
  433
+                    return Array($a, $b, $c);
  434
+                },
  435
+            ),
  436
+        );
  437
+    }
  438
+}
423 439
 ?>
1  package.xml
@@ -66,6 +66,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
66 66
   - Fixed bug #19655 : Closures reporting as multi-line when they are not
67 67
   - Fixed bug #19675 : Improper indent of nested anonymous function bodies in a call
68 68
   - Fixed bug #19685 : PSR2 catch-22 with empty third statement in for loop
  69
+  - Fixed bug #19687 : Anonymous functions inside arrays marked as indented incorrectly in PSR2
69 70
  </notes>
70 71
  <contents>
71 72
   <dir name="/">

2 notes on commit 20ea734

Stephen Rees-Carter

Awesome, thanks for fixing this :)

Tom Bevers

Thx for fixing this issue!

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