Skip to content

Commit

Permalink
Merge pull request #487 from rodrigoprimo/test-coverage-array-indent
Browse files Browse the repository at this point in the history
Generic/ArrayIndent: improve code coverage and implement a few small improvements to the sniff code
  • Loading branch information
jrfnl committed May 14, 2024
2 parents 05f8b21 + 3a2230c commit a2c048b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/Standards/Generic/Sniffs/Arrays/ArrayIndentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
// Determine how far indented the entire array declaration should be.
$ignore = Tokens::$emptyTokens;
$ignore[] = T_DOUBLE_ARROW;
$ignore[] = T_COMMA;
$prev = $phpcsFile->findPrevious($ignore, ($stackPtr - 1), null, true);
$start = $phpcsFile->findStartOfStatement($prev);
$first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $start, true);
Expand Down Expand Up @@ -152,28 +151,27 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
$error = 'Closing brace of array declaration must be on a new line';
$fix = $phpcsFile->addFixableError($error, $arrayEnd, 'CloseBraceNotNewLine');
if ($fix === true) {
$padding = $phpcsFile->eolChar.str_repeat(' ', $expectedIndent);
$padding = $phpcsFile->eolChar.str_repeat(' ', $startIndent);
$phpcsFile->fixer->addContentBefore($arrayEnd, $padding);
}

return;
}

// The close brace must be indented one stop less.
$expectedIndent -= $this->indent;
$foundIndent = ($tokens[$arrayEnd]['column'] - 1);
if ($foundIndent === $expectedIndent) {
$foundIndent = ($tokens[$arrayEnd]['column'] - 1);
if ($foundIndent === $startIndent) {
return;
}

$pluralizeSpace = 's';
if ($expectedIndent === 1) {
if ($startIndent === 1) {
$pluralizeSpace = '';
}

$error = 'Array close brace not indented correctly; expected %s space%s but found %s';
$data = [
$expectedIndent,
$startIndent,
$pluralizeSpace,
$foundIndent,
];
Expand All @@ -182,7 +180,7 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
return;
}

$padding = str_repeat(' ', $expectedIndent);
$padding = str_repeat(' ', $startIndent);
if ($foundIndent === 0) {
$phpcsFile->fixer->addContentBefore($arrayEnd, $padding);
} else {
Expand Down
3 changes: 3 additions & 0 deletions src/Standards/Generic/Tests/Arrays/ArrayIndentUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,6 @@ $var = [
];

// phpcs:set Generic.Arrays.ArrayIndent indent 4

$array = [1,
];
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,6 @@ $var = [
];

// phpcs:set Generic.Arrays.ArrayIndent indent 4

$array = [1,
];

0 comments on commit a2c048b

Please sign in to comment.