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.

For testing, a couple of cases have been added to CommaAfterLastUnitTest.1.inc
and then, the fixtures have been 100% duplicated into CommaAfterLastUnitTest.3.inc
that runs with the *CloserSameLine errors disabled.

A simple diff shows the 8 cases in which the outcome is different, as
expected.

Fixes #283.
  • Loading branch information
stronk7 committed Nov 8, 2023
1 parent 746c319 commit a23d82b
Show file tree
Hide file tree
Showing 6 changed files with 504 additions and 18 deletions.
6 changes: 6 additions & 0 deletions NormalizedArrays/Sniffs/Arrays/CommaAfterLastSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ public function process(File $phpcsFile, $stackPtr)

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

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

$phpcsFile->recordMetric(
$stackPtr,
\sprintf(self::METRIC_NAME, \ucfirst($phrase)),
Expand Down
23 changes: 23 additions & 0 deletions NormalizedArrays/Tests/Arrays/CommaAfterLastUnitTest.1.inc
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<?php

// This is exactly the same as CommasInArrayUnitTest.3.inc, but with these lines
// and all the *MultiLineCloserSameLine errors enabled (default) for the whole file.

// nophpcs:disable NormalizedArrays.Arrays.CommaAfterLast.FoundMultiLineCloserSameLine
// nophpcs:disable NormalizedArrays.Arrays.CommaAfterLast.MissingMultiLineCloserSameLine


/*
* Verify that non-arrays are not touched by the sniff.
* - square brackets, not short array.
Expand Down Expand Up @@ -79,6 +86,14 @@ $missing = [
'c'/* Comment. */
];

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

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

$missingInNested = array(
1 => 'value' ,
2 => [
Expand Down Expand Up @@ -140,6 +155,14 @@ $found = [
'c',
];

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

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

$foundInNested = array(
1 => 'value' ,
2 => [
Expand Down
23 changes: 23 additions & 0 deletions NormalizedArrays/Tests/Arrays/CommaAfterLastUnitTest.1.inc.fixed
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<?php

// This is exactly the same as CommasInArrayUnitTest.3.inc, but with these lines
// and all the *MultiLineCloserSameLine errors enabled (default) for the whole file.

// nophpcs:disable NormalizedArrays.Arrays.CommaAfterLast.FoundMultiLineCloserSameLine
// nophpcs:disable NormalizedArrays.Arrays.CommaAfterLast.MissingMultiLineCloserSameLine


/*
* Verify that non-arrays are not touched by the sniff.
* - square brackets, not short array.
Expand Down Expand Up @@ -79,6 +86,14 @@ $missing = [
'c',/* Comment. */
];

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

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

$missingInNested = array(
1 => 'value' ,
2 => [
Expand Down Expand Up @@ -141,6 +156,14 @@ $found = [
'c'
];

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

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

$foundInNested = array(
1 => 'value' ,
2 => [
Expand Down
201 changes: 201 additions & 0 deletions NormalizedArrays/Tests/Arrays/CommaAfterLastUnitTest.3.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
<?php

// This is exactly the same as CommasInArrayUnitTest.1.inc, but with these lines
// and all the *MultiLineCloserSameLine errors disabled for the whole file.
//
// phpcs:disable NormalizedArrays.Arrays.CommaAfterLast.FoundMultiLineCloserSameLine
// phpcs:disable NormalizedArrays.Arrays.CommaAfterLast.MissingMultiLineCloserSameLine


/*
* Verify that non-arrays are not touched by the sniff.
* - square brackets, not short array.
* - short lists.
*/
$a['array_access'] = 123;

// Short list, not short array.
[,,] = $array;
[$var1, , [$var2, $var3,], $var4,] = $array;

/*
* Empty arrays should be ignored.
*/
$empty = array();
$empty = [ /* comment */ ];

/*
* Test skipping the checks when 'skip' has been passed.
*/
// phpcs:set NormalizedArrays.Arrays.CommaAfterLast singleLine skip
// phpcs:set NormalizedArrays.Arrays.CommaAfterLast multiLine skip

$ignore = array(1, 2, 3,);
$ignore = [
1,
3
];

/*
* Test skipping the checks when invalid property settings have been passed.
*/
// phpcs:set NormalizedArrays.Arrays.CommaAfterLast singleLine disallow
// phpcs:set NormalizedArrays.Arrays.CommaAfterLast multiLine demand

$ignore = array(1, 2, 3,);
$ignore = [
1,
3
];

/*
* Test enforcing a comma after the last array item.
*/
// phpcs:set NormalizedArrays.Arrays.CommaAfterLast singleLine enforce

$good = array( 1, 2, 3, );
$good = [ 'a', 'b', 'c', ];

$missing = array( 1, 2, 3 );
$missing = [ 'a', 'b', 'c' ];

// phpcs:set NormalizedArrays.Arrays.CommaAfterLast multiLine enforce

$good = array(
1,
3,
);
$good = [
'a',
'c',
];

$goodHeredoc = function_call()->method_name( array(
'phrase' => <<<EOD
Here comes some text.
EOD
,
) );

$missing = array(
1,
3
);
$missing = [
'a',
'c'/* Comment. */
];

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

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

$missingInNested = 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 */
);

$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(

0 comments on commit a23d82b

Please sign in to comment.