Skip to content

Split the errors, adding the *CloserSameLine ones #284

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions NormalizedArrays/Sniffs/Arrays/CommaAfterLastSniff.php
Original file line number Diff line number Diff line change
@@ -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(
50 changes: 50 additions & 0 deletions NormalizedArrays/Tests/Arrays/CommaAfterLastUnitTest.1.inc
Original file line number Diff line number Diff line change
@@ -166,6 +166,56 @@ 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'];

$missing_but_good = [
'1', '2',
'3', '4']; // phpcs:ignore NormalizedArrays.Arrays.CommaAfterLast.MissingMultiLineCloserSameLine

$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',];

$found_but_good = [
'1', '2',
'3', '4',]; // phpcs:ignore NormalizedArrays.Arrays.CommaAfterLast.FoundMultiLineCloserSameLine

$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
50 changes: 50 additions & 0 deletions NormalizedArrays/Tests/Arrays/CommaAfterLastUnitTest.1.inc.fixed
Original file line number Diff line number Diff line change
@@ -166,6 +166,56 @@ 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',];

$missing_but_good = [
'1', '2',
'3', '4']; // phpcs:ignore NormalizedArrays.Arrays.CommaAfterLast.MissingMultiLineCloserSameLine

$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'];

$found_but_good = [
'1', '2',
'3', '4',]; // phpcs:ignore NormalizedArrays.Arrays.CommaAfterLast.FoundMultiLineCloserSameLine

$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
4 changes: 4 additions & 0 deletions NormalizedArrays/Tests/Arrays/CommaAfterLastUnitTest.php
Original file line number Diff line number Diff line change
@@ -52,6 +52,10 @@ public function getErrorList($testFile = '')
152 => 1,
159 => 1,
166 => 1,
176 => 1,
180 => 1,
201 => 1,
205 => 1,
];

case 'CommaAfterLastUnitTest.2.inc':