Skip to content

Commit 5f66230

Browse files
committed
Fixed bug #2882 : Generic.Arrays.ArrayIndent can request close brace indent to be less than the statement indent level
1 parent 2672ddb commit 5f66230

File tree

4 files changed

+53
-5
lines changed

4 files changed

+53
-5
lines changed

src/Standards/Generic/Sniffs/Arrays/ArrayIndentSniff.php

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,41 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
5959
{
6060
$tokens = $phpcsFile->getTokens();
6161

62-
$first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $stackPtr, true);
63-
$expectedIndent = ($tokens[$first]['column'] - 1 + $this->indent);
62+
// Determine how far indented the entire array declaration should be.
63+
$ignore = Tokens::$emptyTokens;
64+
$ignore[] = T_DOUBLE_ARROW;
65+
$prev = $phpcsFile->findPrevious($ignore, ($stackPtr - 1), null, true);
66+
$start = $phpcsFile->findStartOfStatement($prev);
67+
$first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $start, true);
68+
$baseIndent = ($tokens[$first]['column'] - 1);
69+
70+
$first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $stackPtr, true);
71+
$startIndent = ($tokens[$first]['column'] - 1);
72+
73+
// If the open brace is not indented to at least to the level of the start
74+
// of the statement, the sniff will conflict with other sniffs trying to
75+
// check indent levels because it's not valid. But we don't enforce exactly
76+
// how far indented it should be.
77+
if ($startIndent < $baseIndent) {
78+
$error = 'Array open brace not indented correctly; expected at least %s spaces but found %s';
79+
$data = [
80+
$baseIndent,
81+
$startIndent,
82+
];
83+
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'OpenBraceIncorrect', $data);
84+
if ($fix === true) {
85+
$padding = str_repeat(' ', $baseIndent);
86+
if ($startIndent === 0) {
87+
$phpcsFile->fixer->addContentBefore($first, $padding);
88+
} else {
89+
$phpcsFile->fixer->replaceToken(($first - 1), $padding);
90+
}
91+
}
92+
93+
return;
94+
}//end if
95+
96+
$expectedIndent = ($startIndent + $this->indent);
6497

6598
foreach ($indices as $index) {
6699
if (isset($index['index_start']) === true) {

src/Standards/Generic/Tests/Arrays/ArrayIndentUnitTest.inc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ $array = [
6363
$c ? $d : $e,
6464
];
6565

66+
$foo =
67+
[
68+
'bar' =>
69+
[
70+
],
71+
];
72+
6673
// phpcs:set Generic.Arrays.ArrayIndent indent 2
6774

6875
$var = [

src/Standards/Generic/Tests/Arrays/ArrayIndentUnitTest.inc.fixed

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ $array = [
6464
$c ? $d : $e,
6565
];
6666

67+
$foo =
68+
[
69+
'bar' =>
70+
[
71+
],
72+
];
73+
6774
// phpcs:set Generic.Arrays.ArrayIndent indent 2
6875

6976
$var = [

src/Standards/Generic/Tests/Arrays/ArrayIndentUnitTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ public function getErrorList()
3636
62 => 1,
3737
63 => 1,
3838
69 => 1,
39-
70 => 1,
40-
71 => 1,
41-
72 => 1,
39+
76 => 1,
40+
77 => 1,
41+
78 => 1,
42+
79 => 1,
4243
];
4344

4445
}//end getErrorList()

0 commit comments

Comments
 (0)