From e19550b47d5b57a15cd9b133ee6109997313a5ed Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Mon, 13 Nov 2023 09:29:30 +0100 Subject: [PATCH] Split the CommaAfterLast errors, adding *CloserSameLine ones 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. Fixes #283 --- .../Sniffs/Arrays/CommaAfterLastSniff.php | 7 ++++ .../Tests/Arrays/CommaAfterLastUnitTest.1.inc | 42 +++++++++++++++++++ .../Arrays/CommaAfterLastUnitTest.1.inc.fixed | 42 +++++++++++++++++++ .../Tests/Arrays/CommaAfterLastUnitTest.php | 4 ++ 4 files changed, 95 insertions(+) diff --git a/NormalizedArrays/Sniffs/Arrays/CommaAfterLastSniff.php b/NormalizedArrays/Sniffs/Arrays/CommaAfterLastSniff.php index d03d1ffc..7384421d 100644 --- a/NormalizedArrays/Sniffs/Arrays/CommaAfterLastSniff.php +++ b/NormalizedArrays/Sniffs/Arrays/CommaAfterLastSniff.php @@ -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( diff --git a/NormalizedArrays/Tests/Arrays/CommaAfterLastUnitTest.1.inc b/NormalizedArrays/Tests/Arrays/CommaAfterLastUnitTest.1.inc index 8615ee9e..11f06816 100644 --- a/NormalizedArrays/Tests/Arrays/CommaAfterLastUnitTest.1.inc +++ b/NormalizedArrays/Tests/Arrays/CommaAfterLastUnitTest.1.inc @@ -166,6 +166,48 @@ EOD , /*comment*/ ) ); +/** + * Tests enforcing a comma after the last array item when the closer is in the same line. See #283. + */ +// 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. See #283. + */ +// 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 diff --git a/NormalizedArrays/Tests/Arrays/CommaAfterLastUnitTest.1.inc.fixed b/NormalizedArrays/Tests/Arrays/CommaAfterLastUnitTest.1.inc.fixed index d06573c1..6b1c4130 100644 --- a/NormalizedArrays/Tests/Arrays/CommaAfterLastUnitTest.1.inc.fixed +++ b/NormalizedArrays/Tests/Arrays/CommaAfterLastUnitTest.1.inc.fixed @@ -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 diff --git a/NormalizedArrays/Tests/Arrays/CommaAfterLastUnitTest.php b/NormalizedArrays/Tests/Arrays/CommaAfterLastUnitTest.php index fbffdc12..f84ae845 100644 --- a/NormalizedArrays/Tests/Arrays/CommaAfterLastUnitTest.php +++ b/NormalizedArrays/Tests/Arrays/CommaAfterLastUnitTest.php @@ -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':