Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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

…ed incorrectly in PSR2
  • Loading branch information...
commit 20ea734e1a1689a8ea58106812af6e1719db8308 1 parent 2b1adc7
@gsherwood gsherwood authored
View
25 CodeSniffer/Standards/Generic/Sniffs/WhiteSpace/ScopeIndentSniff.php
@@ -127,13 +127,24 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
if ($tokens[$firstToken]['code'] !== T_CLOSURE
&& $tokens[$firstToken]['column'] !== $expectedIndent
) {
- $error = 'Line indented incorrectly; expected %s spaces, found %s';
- $data = array(
- ($expectedIndent - 1),
- ($tokens[$firstToken]['column'] - 1),
- );
- $phpcsFile->addError($error, $stackPtr, 'Incorrect', $data);
- }
+ // If the scope opener is a closure but it is not the first token on the
+ // line, then the first token may be a variable or array index as so
+ // should not require exact identation unless the exact member var
+ // is set to TRUE.
+ $exact = true;
+ if ($tokens[$stackPtr]['code'] === T_CLOSURE) {
+ $exact = $this->exact;
+ }
+
+ if ($exact === true || $tokens[$firstToken]['column'] < $expectedIndent) {
+ $error = 'Line indented incorrectly; expected %s spaces, found %s';
+ $data = array(
+ ($expectedIndent - 1),
+ ($tokens[$firstToken]['column'] - 1),
+ );
+ $phpcsFile->addError($error, $stackPtr, 'Incorrect', $data);
+ }
+ }//end if
$scopeOpener = $tokens[$stackPtr]['scope_opener'];
$scopeCloser = $tokens[$stackPtr]['scope_closer'];
View
16 CodeSniffer/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.inc
@@ -420,4 +420,20 @@ $var = call_user_func(
}
}
);
+
+class AnonymousFn
+{
+ public function getAnonFn()
+ {
+ return array(
+ 'functions' => Array(
+ 'function1' => function ($a, $b, $c) {
+ $a = $b + $c;
+ $b = $c / 2;
+ return Array($a, $b, $c);
+ },
+ ),
+ );
+ }
+}
?>
View
1  package.xml
@@ -66,6 +66,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
- 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
- Fixed bug #19685 : PSR2 catch-22 with empty third statement in for loop
+ - Fixed bug #19687 : Anonymous functions inside arrays marked as indented incorrectly in PSR2
</notes>
<contents>
<dir name="/">

2 comments on commit 20ea734

@valorin

Awesome, thanks for fixing this :)

@tombevers

Thx for fixing this issue!

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