Skip to content

Commit

Permalink
Squiz.Arrays.ArrayDeclaration no longer reports errors for a comma on…
Browse files Browse the repository at this point in the history
… a line new after a here/nowdoc (ref #2142)
  • Loading branch information
gsherwood committed Aug 28, 2018
1 parent a874a64 commit a06ff3b
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 16 deletions.
3 changes: 3 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ http://pear.php.net/dtd/package-2.0.xsd">
-- Previously, this standard enforced the use of underscores
- Generic.PHP.NoSilencedErrors error messages now contain a code snippet to show the context of the error
-- Thanks to Juliette Reinders Folmer for the patch
- Squiz.Arrays.ArrayDeclaration no longer reports errors for a comma on a line new after a here/nowdoc
-- Also stops a parse error being generated when auto-fixing
-- The SpaceBeforeComma error message has been changed to only have one data value instead of two
- Squiz.Commenting.FunctionComment no longer errors when trying to fix indents of multi-line param comments
- Squiz.Formatting.OperatorBracket now correctly fixes statements that contain strings
- Squiz.PHP.CommentedOutCode now ignores more @-style annotations and includes better comment block detection
Expand Down
35 changes: 19 additions & 16 deletions src/Standards/Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -431,24 +431,27 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array

if ($keyUsed === false) {
if ($tokens[($nextToken - 1)]['code'] === T_WHITESPACE) {
$content = $tokens[($nextToken - 2)]['content'];
if ($tokens[($nextToken - 1)]['content'] === $phpcsFile->eolChar) {
$spaceLength = 'newline';
} else {
$spaceLength = $tokens[($nextToken - 1)]['length'];
}
$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($nextToken - 1), null, true);
if (($tokens[$prev]['code'] !== T_END_HEREDOC
&& $tokens[$prev]['code'] !== T_END_NOWDOC)
|| $tokens[($nextToken - 1)]['line'] === $tokens[$nextToken]['line']
) {
$content = $tokens[($nextToken - 2)]['content'];
if ($tokens[($nextToken - 1)]['content'] === $phpcsFile->eolChar) {
$spaceLength = 'newline';
} else {
$spaceLength = $tokens[($nextToken - 1)]['length'];
}

$error = 'Expected 0 spaces between "%s" and comma; %s found';
$data = [
$content,
$spaceLength,
];
$error = 'Expected 0 spaces before comma; %s found';
$data = [$spaceLength];

$fix = $phpcsFile->addFixableError($error, $nextToken, 'SpaceBeforeComma', $data);
if ($fix === true) {
$phpcsFile->fixer->replaceToken(($nextToken - 1), '');
$fix = $phpcsFile->addFixableError($error, $nextToken, 'SpaceBeforeComma', $data);
if ($fix === true) {
$phpcsFile->fixer->replaceToken(($nextToken - 1), '');
}
}
}
}//end if

$valueContent = $phpcsFile->findNext(
Tokens::$emptyTokens,
Expand Down Expand Up @@ -837,7 +840,7 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array

// Check that there is no space before the comma.
if ($nextComma !== false && $tokens[($nextComma - 1)]['code'] === T_WHITESPACE) {
// Here/nowdoc closing tags must have the command on the next line.
// Here/nowdoc closing tags must have the comma on the next line.
$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($nextComma - 1), null, true);
if ($tokens[$prev]['code'] !== T_END_HEREDOC && $tokens[$prev]['code'] !== T_END_NOWDOC) {
$content = $tokens[($nextComma - 2)]['content'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,3 +384,12 @@ $foo = foo(
// comment
)
);

$foo = array(
<<<HERE
HERE
,
<<<HERE
HERE
,
);
Original file line number Diff line number Diff line change
Expand Up @@ -412,3 +412,12 @@ $foo = foo(
// comment
)
);

$foo = array(
<<<HERE
HERE
,
<<<HERE
HERE
,
);
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,12 @@ $foo = foo(
// comment
]
);

$foo = [
<<<HERE
HERE
,
<<<HERE
HERE
,
];
Original file line number Diff line number Diff line change
Expand Up @@ -399,3 +399,12 @@ $foo = foo(
// comment
]
);

$foo = [
<<<HERE
HERE
,
<<<HERE
HERE
,
];
2 changes: 2 additions & 0 deletions src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public function getErrorList($testFile='')
369 => 1,
370 => 1,
383 => 1,
394 => 1,
];
case 'ArrayDeclarationUnitTest.2.inc':
return [
Expand Down Expand Up @@ -186,6 +187,7 @@ public function getErrorList($testFile='')
357 => 1,
358 => 1,
372 => 1,
383 => 1,
];
default:
return [];
Expand Down

0 comments on commit a06ff3b

Please sign in to comment.