diff --git a/NormalizedArrays/Sniffs/Arrays/CommaAfterLastSniff.php b/NormalizedArrays/Sniffs/Arrays/CommaAfterLastSniff.php index d03d1ff..7720e7a 100644 --- a/NormalizedArrays/Sniffs/Arrays/CommaAfterLastSniff.php +++ b/NormalizedArrays/Sniffs/Arrays/CommaAfterLastSniff.php @@ -145,7 +145,8 @@ public function process(File $phpcsFile, $stackPtr) return; } - $isComma = ($tokens[$lastNonEmpty]['code'] === \T_COMMA); + $isComma = ($tokens[$lastNonEmpty]['code'] === \T_COMMA); + $sameLineCloser = ($tokens[$lastNonEmpty]['line'] === $tokens[$closer]['line']) ? 'CloserSameLine' : ''; $phpcsFile->recordMetric( $stackPtr, @@ -160,8 +161,9 @@ public function process(File $phpcsFile, $stackPtr) } $error = 'There should be a comma after the last array item in a %s array.'; - $errorCode = 'Missing' . $errorCode; + $errorCode = 'Missing' . $errorCode . $sameLineCloser; $data = [$phrase]; + error_log('enforce: ' . $errorCode . ', line: ' . $tokens[$lastNonEmpty]['line']); $fix = $phpcsFile->addFixableError($error, $lastNonEmpty, $errorCode, $data); if ($fix === true) { $extraContent = ','; @@ -186,8 +188,9 @@ public function process(File $phpcsFile, $stackPtr) } $error = 'A comma after the last array item in a %s array is not allowed.'; - $errorCode = 'Found' . $errorCode; + $errorCode = 'Found' . $errorCode . $sameLineCloser; $data = [$phrase]; + error_log('forbid: ' . $errorCode . ', line: ' . $tokens[$lastNonEmpty]['line']); $fix = $phpcsFile->addFixableError($error, $lastNonEmpty, $errorCode, $data); if ($fix === true) { $start = $lastNonEmpty; diff --git a/NormalizedArrays/Tests/Arrays/CommaAfterLastUnitTest.1.inc b/NormalizedArrays/Tests/Arrays/CommaAfterLastUnitTest.1.inc index 8615ee9..82f0ef9 100644 --- a/NormalizedArrays/Tests/Arrays/CommaAfterLastUnitTest.1.inc +++ b/NormalizedArrays/Tests/Arrays/CommaAfterLastUnitTest.1.inc @@ -1,5 +1,12 @@ 'value' , 2 => [ @@ -140,6 +155,14 @@ $found = [ 'c', ]; +$found = array( + 1, 2, + 3, 4,); + +$found = [ + '1', '2', + '3', '4',]; + $foundInNested = array( 1 => 'value' , 2 => [ diff --git a/NormalizedArrays/Tests/Arrays/CommaAfterLastUnitTest.1.inc.fixed b/NormalizedArrays/Tests/Arrays/CommaAfterLastUnitTest.1.inc.fixed index d06573c..840ca3b 100644 --- a/NormalizedArrays/Tests/Arrays/CommaAfterLastUnitTest.1.inc.fixed +++ b/NormalizedArrays/Tests/Arrays/CommaAfterLastUnitTest.1.inc.fixed @@ -1,5 +1,12 @@ 'value' , 2 => [ @@ -141,6 +156,14 @@ $found = [ 'c' ]; +$found = array( + 1, 2, + 3, 4); + +$found = [ + '1', '2', + '3', '4']; + $foundInNested = array( 1 => 'value' , 2 => [ diff --git a/NormalizedArrays/Tests/Arrays/CommaAfterLastUnitTest.3.inc b/NormalizedArrays/Tests/Arrays/CommaAfterLastUnitTest.3.inc new file mode 100644 index 0000000..8bbeea9 --- /dev/null +++ b/NormalizedArrays/Tests/Arrays/CommaAfterLastUnitTest.3.inc @@ -0,0 +1,201 @@ +method_name( array( + 'phrase' => << 'value' , + 2 => [ + 'a' => 'value' ,// phpcs:disable Standard.Category.Sniff - the extra spacing is fine, might be for alignment with other comments. + 'b' => array( + 1 + ), + 'c' => apply_filters( 'filter', $input, $var ) + ], + 3 => apply_filters( 'filter', $input, $var )/* phpcs:ignore Standard.Category.Sniff */ +); + +$missing = array( + 'first', + 'second' + //'third', + ); + +$missingNowdoc = function_call()->method_name( array( + 'phrase' => <<<'EOD' +Here comes some text. +EOD +) ); + +/* + * Test forbidding a comma after the last array item. + */ +// phpcs:set NormalizedArrays.Arrays.CommaAfterLast singleLine forbid + +$good = array( 1, 2, 3 ); +$good = [ 'a', 'b', 'c' ]; + +$found = array( 1, 2, 3, ); +$found = [ 'a', 'b', 'c', ]; + +// phpcs:set NormalizedArrays.Arrays.CommaAfterLast multiLine forbid + +$good = array( + 1, + 3 +); +$good = [ + 'a', + 'c' +]; + +$goodNowdoc = function_call()->method_name( array( + 'phrase' => <<<'EOD' +Here comes some text. +EOD +) ); + +$found = array( + 1, + 3,/* Comment. */ +); +$found = [ + 'a', + 'c', +]; + +$found = array( + 1, 2, + 3, 4,); + +$found = [ + '1', '2', + '3', '4',]; + +$foundInNested = array( + 1 => 'value' , + 2 => [ + 'a' => 'value' ,// phpcs:disable Standard.Category.Sniff - the extra spacing is fine, might be for alignment with other comments. + 'b' => array( + 1, + ), + 'c' => apply_filters( 'filter', $input, $var ), + ], + 3 => apply_filters( 'filter', $input, $var ), /* phpcs:ignore Standard.Category.Sniff */ +); + +$foundHeredoc = function_call()->method_name( array( + 'phrase' => <<<"EOD" +Here comes some text. +EOD +, +) ); + +$foundHeredoc = function_call()->method_name( array( + 'phrase' => <<<"EOD" +Here comes some text. +EOD +, /*comment*/ +) ); + +// Reset the properties to the defaults. +// phpcs:set NormalizedArrays.Arrays.CommaAfterLast singleLine forbid +// phpcs:set NormalizedArrays.Arrays.CommaAfterLast multiLine enforce +// phpcs:enable + +/* + * Test live coding. This should be the last test in the file. + */ +// Intentional parse error. +$ignore = array( diff --git a/NormalizedArrays/Tests/Arrays/CommaAfterLastUnitTest.3.inc.fixed b/NormalizedArrays/Tests/Arrays/CommaAfterLastUnitTest.3.inc.fixed new file mode 100644 index 0000000..80345e2 --- /dev/null +++ b/NormalizedArrays/Tests/Arrays/CommaAfterLastUnitTest.3.inc.fixed @@ -0,0 +1,201 @@ +method_name( array( + 'phrase' => << 'value' , + 2 => [ + 'a' => 'value' ,// phpcs:disable Standard.Category.Sniff - the extra spacing is fine, might be for alignment with other comments. + 'b' => array( + 1, + ), + 'c' => apply_filters( 'filter', $input, $var ), + ], + 3 => apply_filters( 'filter', $input, $var ),/* phpcs:ignore Standard.Category.Sniff */ +); + +$missing = array( + 'first', + 'second', + //'third', + ); + +$missingNowdoc = function_call()->method_name( array( + 'phrase' => <<<'EOD' +Here comes some text. +EOD +, +) ); + +/* + * Test forbidding a comma after the last array item. + */ +// phpcs:set NormalizedArrays.Arrays.CommaAfterLast singleLine forbid + +$good = array( 1, 2, 3 ); +$good = [ 'a', 'b', 'c' ]; + +$found = array( 1, 2, 3, ); +$found = [ 'a', 'b', 'c', ]; + +// phpcs:set NormalizedArrays.Arrays.CommaAfterLast multiLine forbid + +$good = array( + 1, + 3 +); +$good = [ + 'a', + 'c' +]; + +$goodNowdoc = function_call()->method_name( array( + 'phrase' => <<<'EOD' +Here comes some text. +EOD +) ); + +$found = array( + 1, + 3/* Comment. */ +); +$found = [ + 'a', + 'c' +]; + +$found = array( + 1, 2, + 3, 4,); + +$found = [ + '1', '2', + '3', '4',]; + +$foundInNested = array( + 1 => 'value' , + 2 => [ + 'a' => 'value' ,// phpcs:disable Standard.Category.Sniff - the extra spacing is fine, might be for alignment with other comments. + 'b' => array( + 1 + ), + 'c' => apply_filters( 'filter', $input, $var ) + ], + 3 => apply_filters( 'filter', $input, $var ) /* phpcs:ignore Standard.Category.Sniff */ +); + +$foundHeredoc = function_call()->method_name( array( + 'phrase' => <<<"EOD" +Here comes some text. +EOD +) ); + +$foundHeredoc = function_call()->method_name( array( + 'phrase' => <<<"EOD" +Here comes some text. +EOD + /*comment*/ +) ); + +// Reset the properties to the defaults. +// phpcs:set NormalizedArrays.Arrays.CommaAfterLast singleLine forbid +// phpcs:set NormalizedArrays.Arrays.CommaAfterLast multiLine enforce +// phpcs:enable + +/* + * Test live coding. This should be the last test in the file. + */ +// Intentional parse error. +$ignore = array( diff --git a/NormalizedArrays/Tests/Arrays/CommaAfterLastUnitTest.php b/NormalizedArrays/Tests/Arrays/CommaAfterLastUnitTest.php index fbffdc1..74578a5 100644 --- a/NormalizedArrays/Tests/Arrays/CommaAfterLastUnitTest.php +++ b/NormalizedArrays/Tests/Arrays/CommaAfterLastUnitTest.php @@ -34,24 +34,28 @@ public function getErrorList($testFile = '') switch ($testFile) { case 'CommaAfterLastUnitTest.1.inc': return [ - 52 => 1, - 53 => 1, - 75 => 1, - 79 => 1, - 87 => 1, - 89 => 1, - 91 => 1, - 96 => 1, - 103 => 1, - 114 => 1, - 115 => 1, - 136 => 1, - 140 => 1, - 148 => 1, - 150 => 1, - 152 => 1, - 159 => 1, - 166 => 1, + 59 => 1, // Enabled MissingSingleLineCloserSameLine + 60 => 1, // Enabled MissingSingleLineCloserSameLine + 82 => 1, + 86 => 1, + 91 => 1, // Enabled MissingMultiLineLineCloserSameLine + 95 => 1, // Enabled MissingMultiLineLineCloserSameLine + 102 => 1, + 104 => 1, + 106 => 1, + 111 => 1, + 118 => 1, + 129 => 1, // Enabled FoundSingleLineCloserSameLine + 130 => 1, // Enabled FoundSingleLineCloserSameLine + 151 => 1, + 155 => 1, + 160 => 1, // Enabled FoundMultiLineLineCloserSameLine + 164 => 1, // Enabled FoundMultiLineLineCloserSameLine + 171 => 1, + 173 => 1, + 175 => 1, + 182 => 1, + 189 => 1, ]; case 'CommaAfterLastUnitTest.2.inc': @@ -66,6 +70,34 @@ public function getErrorList($testFile = '') 43 => 1, ]; + case 'CommaAfterLastUnitTest.3.inc': + // This is exactly the same as CommasInArrayUnitTest.1.inc, but with all the + // *CloserSameLine errors disabled. + return [ + // 59 => 1, // Disabled MissingSingleLineCloserSameLine + // 60 => 1, // Disabled MissingSingleLineCloserSameLine + 82 => 1, + 86 => 1, + // 91 => 1, // Disabled MissingMultiLineLineCloserSameLine + // 95 => 1, // Disabled MissingMultiLineLineCloserSameLine + 102 => 1, + 104 => 1, + 106 => 1, + 111 => 1, + 118 => 1, + // 129 => 1, // Disabled FoundSingleLineCloserSameLine + // 130 => 1, // Disabled FoundSingleLineCloserSameLine + 151 => 1, + 155 => 1, + // 160 => 1, // Disabled FoundMultiLineLineCloserSameLine + // 164 => 1, // Disabled FoundMultiLineLineCloserSameLine + 171 => 1, + 173 => 1, + 175 => 1, + 182 => 1, + 189 => 1, + ]; + default: return []; }