Skip to content

Commit

Permalink
Split the CommaAfterLast errors, adding *CloserSameLine ones
Browse files Browse the repository at this point in the history
Some standards may want to have different rules when
the array closer is in the same line than the last element.

When that happens, the new *CloserSameLine errors are
emitted, allowing to disable them via ruleset. Only for
MultiLine cases, they don't make sense in SingleLine ones.
  • Loading branch information
stronk7 committed Nov 13, 2023
1 parent 5bda2df commit 1694346
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
7 changes: 7 additions & 0 deletions NormalizedArrays/Sniffs/Arrays/CommaAfterLastSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,13 @@ public function process(File $phpcsFile, $stackPtr)
return;
}

// If the closer is on the same line as the last element, change the error code for multi-line arrays.
if ($errorCode === 'MultiLine'
&& $tokens[$lastNonEmpty]['line'] === $tokens[$closer]['line']
) {
$errorCode .= 'CloserSameLine';
}

$isComma = ($tokens[$lastNonEmpty]['code'] === \T_COMMA);

$phpcsFile->recordMetric(
Expand Down
42 changes: 42 additions & 0 deletions NormalizedArrays/Tests/Arrays/CommaAfterLastUnitTest.1.inc
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,48 @@ EOD
, /*comment*/
) );

/**
* Tests enforcing a comma after the last array item when the closer is in the same line.
*/
// phpcs:set NormalizedArrays.Arrays.CommaAfterLast multiLine enforce

$missing = array(
1, 2,
3, 4);

$missing = [
'1', '2',
'3', '4'];

$good = array(
1, 2,
3, 4,);

$good = [
'1', '2',
'3', '4',];

/**
* Tests forbidding a comma after the last array item when the closer is in the same line.
*/
// phpcs:set NormalizedArrays.Arrays.CommaAfterLast multiLine forbid

$found = array(
1, 2,
3, 4,);

$found = [
'1', '2',
'3', '4',];

$good = array(
1, 2,
3, 4);

$good = [
'1', '2',
'3', '4'];

// Reset the properties to the defaults.
// phpcs:set NormalizedArrays.Arrays.CommaAfterLast singleLine forbid
// phpcs:set NormalizedArrays.Arrays.CommaAfterLast multiLine enforce
Expand Down
42 changes: 42 additions & 0 deletions NormalizedArrays/Tests/Arrays/CommaAfterLastUnitTest.1.inc.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,48 @@ EOD
/*comment*/
) );

/**
* Tests enforcing a comma after the last array item when the closer is in the same line.
*/
// phpcs:set NormalizedArrays.Arrays.CommaAfterLast multiLine enforce

$missing = array(
1, 2,
3, 4,);

$missing = [
'1', '2',
'3', '4',];

$good = array(
1, 2,
3, 4,);

$good = [
'1', '2',
'3', '4',];

/**
* Tests forbidding a comma after the last array item when the closer is in the same line.
*/
// phpcs:set NormalizedArrays.Arrays.CommaAfterLast multiLine forbid

$found = array(
1, 2,
3, 4);

$found = [
'1', '2',
'3', '4'];

$good = array(
1, 2,
3, 4);

$good = [
'1', '2',
'3', '4'];

// Reset the properties to the defaults.
// phpcs:set NormalizedArrays.Arrays.CommaAfterLast singleLine forbid
// phpcs:set NormalizedArrays.Arrays.CommaAfterLast multiLine enforce
Expand Down
4 changes: 4 additions & 0 deletions NormalizedArrays/Tests/Arrays/CommaAfterLastUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ public function getErrorList($testFile = '')
152 => 1,
159 => 1,
166 => 1,
176 => 1,
180 => 1,
197 => 1,
201 => 1,
];

case 'CommaAfterLastUnitTest.2.inc':
Expand Down

0 comments on commit 1694346

Please sign in to comment.