Skip to content

Commit 20ea734

Browse files
committed
Fixed bug #19687 : Anonymous functions inside arrays marked as indented incorrectly in PSR2
1 parent 2b1adc7 commit 20ea734

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

CodeSniffer/Standards/Generic/Sniffs/WhiteSpace/ScopeIndentSniff.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,24 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
127127
if ($tokens[$firstToken]['code'] !== T_CLOSURE
128128
&& $tokens[$firstToken]['column'] !== $expectedIndent
129129
) {
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
137148

138149
$scopeOpener = $tokens[$stackPtr]['scope_opener'];
139150
$scopeCloser = $tokens[$stackPtr]['scope_closer'];

CodeSniffer/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.inc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,4 +420,20 @@ $var = call_user_func(
420420
}
421421
}
422422
);
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+
}
423439
?>

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
6666
- Fixed bug #19655 : Closures reporting as multi-line when they are not
6767
- Fixed bug #19675 : Improper indent of nested anonymous function bodies in a call
6868
- 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
6970
</notes>
7071
<contents>
7172
<dir name="/">

0 commit comments

Comments
 (0)