Skip to content

Commit

Permalink
Fixed bug #2498 : Squiz.Arrays.ArrayDeclaration.MultiLineNotAllowed a…
Browse files Browse the repository at this point in the history
…utofix breaks heredoc
  • Loading branch information
gsherwood committed May 13, 2019
1 parent 9c90cbf commit 6868096
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 29 deletions.
1 change: 1 addition & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
-- Thanks to Juliette Reinders Folmer for the patch
- Fixed bug #2478 : FunctionCommentThrowTag.WrongNumber when exception is thrown once but built conditionally
- Fixed bug #2479 : Generic.WhiteSpace.ScopeIndent error when using array destructing with exact indent checking
- Fixed bug #2498 : Squiz.Arrays.ArrayDeclaration.MultiLineNotAllowed autofix breaks heredoc
</notes>
<contents>
<dir name="/">
Expand Down
62 changes: 33 additions & 29 deletions src/Standards/Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -530,43 +530,47 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
}

if ($singleValue === true) {
// Array cannot be empty, so this is a multi-line array with
// a single value. It should be defined on single line.
$error = 'Multi-line array contains a single value; use single-line array instead';
$errorCode = 'MultiLineNotAllowed';

$find = Tokens::$phpcsCommentTokens;
$find[] = T_COMMENT;
$comment = $phpcsFile->findNext($find, ($arrayStart + 1), $arrayEnd);
if ($comment === false) {
$fix = $phpcsFile->addFixableError($error, $stackPtr, $errorCode);
} else {
$fix = false;
$phpcsFile->addError($error, $stackPtr, $errorCode);
}
// Before we complain, make sure the single value isn't a here/nowdoc.
$next = $phpcsFile->findNext(Tokens::$heredocTokens, ($arrayStart + 1), ($arrayEnd - 1));
if ($next === false) {
// Array cannot be empty, so this is a multi-line array with
// a single value. It should be defined on single line.
$error = 'Multi-line array contains a single value; use single-line array instead';
$errorCode = 'MultiLineNotAllowed';

$find = Tokens::$phpcsCommentTokens;
$find[] = T_COMMENT;
$comment = $phpcsFile->findNext($find, ($arrayStart + 1), $arrayEnd);
if ($comment === false) {
$fix = $phpcsFile->addFixableError($error, $stackPtr, $errorCode);
} else {
$fix = false;
$phpcsFile->addError($error, $stackPtr, $errorCode);
}

if ($fix === true) {
$phpcsFile->fixer->beginChangeset();
for ($i = ($arrayStart + 1); $i < $arrayEnd; $i++) {
if ($tokens[$i]['code'] !== T_WHITESPACE) {
break;
if ($fix === true) {
$phpcsFile->fixer->beginChangeset();
for ($i = ($arrayStart + 1); $i < $arrayEnd; $i++) {
if ($tokens[$i]['code'] !== T_WHITESPACE) {
break;
}

$phpcsFile->fixer->replaceToken($i, '');
}

$phpcsFile->fixer->replaceToken($i, '');
}
for ($i = ($arrayEnd - 1); $i > $arrayStart; $i--) {
if ($tokens[$i]['code'] !== T_WHITESPACE) {
break;
}

for ($i = ($arrayEnd - 1); $i > $arrayStart; $i--) {
if ($tokens[$i]['code'] !== T_WHITESPACE) {
break;
$phpcsFile->fixer->replaceToken($i, '');
}

$phpcsFile->fixer->replaceToken($i, '');
$phpcsFile->fixer->endChangeset();
}

$phpcsFile->fixer->endChangeset();
}

return;
return;
}//end if
}//end if

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,12 @@ $paths = array(
Init::ROOT_DIR.'/цвет' => 'синий',
);

$foo = array(<<<JSON
{
}
JSON
);

// Intentional syntax error.
$a = array(
'a' =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,12 @@ $paths = array(
Init::ROOT_DIR.'/цвет' => 'синий',
);

$foo = array(<<<JSON
{
}
JSON
);

// Intentional syntax error.
$a = array(
'a' =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,12 @@ $paths = [
Init::ROOT_DIR.'/цвет' => 'синий',
];

$foo = [<<<JSON
{
}
JSON
];

// Intentional syntax error.
$a = [
'a' =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,12 @@ $paths = [
Init::ROOT_DIR.'/цвет' => 'синий',
];

$foo = [<<<JSON
{
}
JSON
];

// Intentional syntax error.
$a = [
'a' =>
Expand Down

0 comments on commit 6868096

Please sign in to comment.