-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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. 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
Showing
6 changed files
with
504 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
201 changes: 201 additions & 0 deletions
201
NormalizedArrays/Tests/Arrays/CommaAfterLastUnitTest.3.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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( |
Oops, something went wrong.