Skip to content

Commit

Permalink
Merge pull request #118 from PHPCSStandards/normalizedarrays/arraybra…
Browse files Browse the repository at this point in the history
…cespacing-allow-comment-after-opener

NormalizedArrays/ArrayBraceSpacing: allow for trailing comments after array opener
  • Loading branch information
jrfnl committed Jul 1, 2022
2 parents 38e83a7 + 740a7db commit 96bac0f
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 13 deletions.
18 changes: 16 additions & 2 deletions NormalizedArrays/Sniffs/Arrays/ArrayBraceSpacingSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;
use PHPCSUtils\Fixers\SpacesFixer;
use PHPCSUtils\Utils\Arrays;

Expand Down Expand Up @@ -156,7 +157,7 @@ public function process(File $phpcsFile, $stackPtr)

$openClose = Arrays::getOpenClose($phpcsFile, $stackPtr);
if ($openClose === false) {
// Short list or real square brackets.
// Live coding, short list or real square brackets.
return;
}

Expand Down Expand Up @@ -264,10 +265,23 @@ public function process(File $phpcsFile, $stackPtr)
$error = 'Expected %s after the array opener in a multi line array. Found: %s';
$code = 'SpaceAfterArrayOpenerMultiLine';

$nextNonWhitespace = $phpcsFile->findNext(\T_WHITESPACE, ($opener + 1), null, true);
if ($this->spacesMultiLine === 'newline') {
// Check for a trailing comment after the array opener and allow for it.
if (($tokens[$nextNonWhitespace]['code'] === \T_COMMENT
|| isset(Tokens::$phpcsCommentTokens[$tokens[$nextNonWhitespace]['code']]) === true)
&& $tokens[$nextNonWhitespace]['line'] === $tokens[$opener]['line']
) {
// We found a trailing comment after array opener. Treat that as the opener instead.
$opener = $nextNonWhitespace;
$nextNonWhitespace = $phpcsFile->findNext(\T_WHITESPACE, ($opener + 1), null, true);
}
}

SpacesFixer::checkAndFix(
$phpcsFile,
$opener,
$phpcsFile->findNext(\T_WHITESPACE, ($opener + 1), null, true),
$nextNonWhitespace,
$this->spacesMultiLine,
$error,
$code,
Expand Down
96 changes: 95 additions & 1 deletion NormalizedArrays/Tests/Arrays/ArrayBraceSpacingUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,58 @@ $array = [


$a,
$b]; // Error x 2.
$b]; // Error x 1.

$array = array($a,
$b
); // Error x 1.

$array = array(
// Comment at the first line is always fine.
$a,
$b
);

$array = array( // Allow trailing comment after opener.
$a,
$b
);

$array = array( /* Allow trailing comment after opener. */
$a,
$b
);

$array = array( // phpcs:ignore Stdn.Cat.Sniff -- Allow trailing annotation after opener.
$a,
$b
);

$array = array( /* Allow trailing comment after opener. */ /* But don't allow a second one */
$a,
$b
); // Error x 1.

$array = array( /* This is still an error */ $a,
$b
); // Error x 1.

$array = [ // Allow trailing comment after opener.



$a,
$b
];

$array = [ /* Allow trailing comment after opener */



$a,
$b
];

// phpcs:set NormalizedArrays.Arrays.ArrayBraceSpacing spacesMultiLine 0

$array = [$a,
Expand All @@ -163,6 +209,30 @@ $array = array($a,
$b
); // Error x 1.

$array = array( // Comment. Error x 1.
$a,
$b);

$array = array( /* Comment. Error x 1. */
$a,
$b);

$array = array( // phpcs:ignore Stdn.Cat.Sniff -- Error x 1.
$a,
$b);

$array = array(// Comment. No space, this is fine.
$a,
$b);

$array = array(/* Comment. No space, this is fine. */
$a,
$b);

$array = array(// phpcs:ignore Stdn.Cat.Sniff -- No space, this is fine.
$a,
$b);

// phpcs:set NormalizedArrays.Arrays.ArrayBraceSpacing spacesMultiLine 1

$array = [ $a,
Expand All @@ -186,6 +256,30 @@ $array = array($a,
$b
); // Error x 2.

$array = array( // Comment. One space, this is fine.
$a,
$b );

$array = array( /* Comment. One space, this is fine. */
$a,
$b );

$array = array( // phpcs:ignore Stdn.Cat.Sniff -- One space, this is fine.
$a,
$b );

$array = array(// Comment. No space, error x 1.
$a,
$b );

$array = array(/* Comment. No space, error x 1. */
$a,
$b );

$array = array(// phpcs:ignore Stdn.Cat.Sniff -- No space, error x 1.
$a,
$b );

// phpcs:set NormalizedArrays.Arrays.ArrayBraceSpacing spacesMultiLine false

$array = array(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,61 @@ $array = [

$a,
$b
]; // Error x 2.
]; // Error x 1.

$array = array(
$a,
$b
); // Error x 1.

$array = array(
// Comment at the first line is always fine.
$a,
$b
);

$array = array( // Allow trailing comment after opener.
$a,
$b
);

$array = array( /* Allow trailing comment after opener. */
$a,
$b
);

$array = array( // phpcs:ignore Stdn.Cat.Sniff -- Allow trailing annotation after opener.
$a,
$b
);

$array = array( /* Allow trailing comment after opener. */
/* But don't allow a second one */
$a,
$b
); // Error x 1.

$array = array( /* This is still an error */
$a,
$b
); // Error x 1.

$array = [ // Allow trailing comment after opener.



$a,
$b
];

$array = [ /* Allow trailing comment after opener */



$a,
$b
];

// phpcs:set NormalizedArrays.Arrays.ArrayBraceSpacing spacesMultiLine 0

$array = [$a,
Expand All @@ -158,6 +206,30 @@ $array = [$a,
$array = array($a,
$b); // Error x 1.

$array = array(// Comment. Error x 1.
$a,
$b);

$array = array(/* Comment. Error x 1. */
$a,
$b);

$array = array(// phpcs:ignore Stdn.Cat.Sniff -- Error x 1.
$a,
$b);

$array = array(// Comment. No space, this is fine.
$a,
$b);

$array = array(/* Comment. No space, this is fine. */
$a,
$b);

$array = array(// phpcs:ignore Stdn.Cat.Sniff -- No space, this is fine.
$a,
$b);

// phpcs:set NormalizedArrays.Arrays.ArrayBraceSpacing spacesMultiLine 1

$array = [ $a,
Expand All @@ -174,6 +246,30 @@ $array = [ $a,
$array = array( $a,
$b ); // Error x 2.

$array = array( // Comment. One space, this is fine.
$a,
$b );

$array = array( /* Comment. One space, this is fine. */
$a,
$b );

$array = array( // phpcs:ignore Stdn.Cat.Sniff -- One space, this is fine.
$a,
$b );

$array = array( // Comment. No space, error x 1.
$a,
$b );

$array = array( /* Comment. No space, error x 1. */
$a,
$b );

$array = array( // phpcs:ignore Stdn.Cat.Sniff -- No space, error x 1.
$a,
$b );

// phpcs:set NormalizedArrays.Arrays.ArrayBraceSpacing spacesMultiLine false

$array = array(
Expand Down
26 changes: 17 additions & 9 deletions NormalizedArrays/Tests/Arrays/ArrayBraceSpacingUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,24 @@ public function getErrorList()
130 => 1,
137 => 1,
139 => 1,
150 => 1,
153 => 1,
155 => 1,
164 => 1,
173 => 1,
176 => 1,
178 => 1,
183 => 1,
185 => 1,
187 => 1,
169 => 1,
196 => 1,
199 => 1,
201 => 1,
210 => 1,
212 => 1,
216 => 1,
220 => 1,
243 => 1,
246 => 1,
248 => 1,
253 => 1,
255 => 1,
257 => 1,
271 => 1,
275 => 1,
279 => 1,
];
}

Expand Down

0 comments on commit 96bac0f

Please sign in to comment.