Skip to content

Commit 92822a0

Browse files
committed
Squiz.Arrays.ArrayDeclaration no longer tries to change multi-line arrays to single line when they contain comments (ref #2091)
1 parent 239b570 commit 92822a0

File tree

7 files changed

+42
-2
lines changed

7 files changed

+42
-2
lines changed

package.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
4747
- Squiz.Arrays.ArrayDeclaration no longer removes the array opening brace while fixing
4848
-- This could occur when the opening brace was on a new line and the first array key directly followed
4949
-- This change also stops the KeyNotAligned error message being incorrectly reported in these cases
50+
- Squiz.Arrays.ArrayDeclaration no longer tries to change multi-line arrays to single line when they contain comments
51+
-- Fixes a conflict between this sniff and some indentation sniffs
5052
- Squiz.Classes.ClassDeclaration no longer enforces spacing rules when a class is followed by a function
5153
-- Fixes a conflict between this sniff and the Squiz.WhiteSpace.FunctionSpacing sniff
5254
- The Squiz.Classes.ValidClassName.NotCamelCaps message now references PascalCase instead of CamelCase

src/Standards/Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -524,8 +524,18 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
524524
if ($singleValue === true) {
525525
// Array cannot be empty, so this is a multi-line array with
526526
// a single value. It should be defined on single line.
527-
$error = 'Multi-line array contains a single value; use single-line array instead';
528-
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'MultiLineNotAllowed');
527+
$error = 'Multi-line array contains a single value; use single-line array instead';
528+
$errorCode = 'MultiLineNotAllowed';
529+
530+
$find = Tokens::$phpcsCommentTokens;
531+
$find[] = T_COMMENT;
532+
$comment = $phpcsFile->findNext($find, ($arrayStart + 1), $arrayEnd);
533+
if ($comment === false) {
534+
$fix = $phpcsFile->addFixableError($error, $stackPtr, $errorCode);
535+
} else {
536+
$fix = false;
537+
$phpcsFile->addError($error, $stackPtr, $errorCode);
538+
}
529539

530540
if ($fix === true) {
531541
$phpcsFile->fixer->beginChangeset();

src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.1.inc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,3 +378,9 @@ $foo = array(
378378
: self::STATUS_VERIFIED,
379379
'3' => strtotime($row['date']),
380380
);
381+
382+
$foo = foo(
383+
array(
384+
// comment
385+
)
386+
);

src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.1.inc.fixed

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,3 +406,9 @@ $foo = array(
406406
: self::STATUS_VERIFIED,
407407
'3' => strtotime($row['date']),
408408
);
409+
410+
$foo = foo(
411+
array(
412+
// comment
413+
)
414+
);

src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,3 +366,10 @@ $foo = [
366366
: self::STATUS_VERIFIED,
367367
'3' => strtotime($row['date']),
368368
];
369+
370+
371+
$foo = foo(
372+
[
373+
// comment
374+
]
375+
);

src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc.fixed

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,3 +392,10 @@ $foo = [
392392
: self::STATUS_VERIFIED,
393393
'3' => strtotime($row['date']),
394394
];
395+
396+
397+
$foo = foo(
398+
[
399+
// comment
400+
]
401+
);

src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ public function getErrorList($testFile='')
111111
368 => 2,
112112
369 => 1,
113113
370 => 1,
114+
383 => 1,
114115
];
115116
case 'ArrayDeclarationUnitTest.2.inc':
116117
return [
@@ -184,6 +185,7 @@ public function getErrorList($testFile='')
184185
356 => 2,
185186
357 => 1,
186187
358 => 1,
188+
372 => 1,
187189
];
188190
default:
189191
return [];

0 commit comments

Comments
 (0)