Skip to content
Snippets Groups Projects
Unverified Commit e46990e7 authored by Jonathan Smith's avatar Jonathan Smith Committed by GitHub
Browse files

fix(Array): Incorrect counting of array elements by comma (#3183715)

parent 3a1d6bb9
No related branches found
No related tags found
1 merge request!4Issue #3217297: False positive in closure usage of USE keyword.
...@@ -116,12 +116,44 @@ class ArraySniff implements Sniff ...@@ -116,12 +116,44 @@ class ArraySniff implements Sniff
$arrayEnding = $tokens[$tokens[$stackPtr][$parenthesisCloser]]['column']; $arrayEnding = $tokens[$tokens[$stackPtr][$parenthesisCloser]]['column'];
if ($arrayEnding > $this->lineLimit) { if ($arrayEnding > $this->lineLimit) {
$comma1 = $phpcsFile->findNext(T_COMMA, ($stackPtr + 1), $tokens[$stackPtr][$parenthesisCloser]); // Nested arrays and function calls within array elements may contain commas so we
if ($comma1 !== false) { // cannot simply search for the next comma as evidence that the main array has more
// than one item. So we examine the comma's nested_parenthesis info, and break out
// of the loop when a valid comma is found, otherwise look for the next one.
$pos = ($stackPtr + 1);
while (($comma = $phpcsFile->findNext(T_COMMA, $pos, $tokens[$stackPtr][$parenthesisCloser])) > 0) {
// If the comma has no nested information then it is part of
// the main array being tested. No need to search for more.
if (isset($tokens[$comma]['nested_parenthesis']) === false) {
break;
}
// Get the last key and value from nested_parenthesis array. If these match the
// array opener and closer then the comma a valid part of the array being tested.
$end = array_slice($tokens[$comma]['nested_parenthesis'], -1, 1, true);
if ($end === [$tokens[$stackPtr][$parenthesisOpener] => $tokens[$stackPtr][$parenthesisCloser]]) {
break;
}
// If the comma nested information is identical to the $lastItem nested info
// then it is part of the array.
if (isset($tokens[$lastItem]['nested_parenthesis']) === true
&& $tokens[$comma]['nested_parenthesis'] === $tokens[$lastItem]['nested_parenthesis']
) {
break;
}
// If none of the breaks above have been executed then the comma is not part of the
// array being tested and does not indicate a second element. Look for the next one.
$pos = ($comma + 1);
}//end while
;
if ($comma !== false) {
$error = 'The array declaration extends to column %s (the limit is %s). The array content should be split up over multiple lines'; $error = 'The array declaration extends to column %s (the limit is %s). The array content should be split up over multiple lines';
$phpcsFile->addError($error, $stackPtr, 'LongLineDeclaration', [$arrayEnding, $this->lineLimit]); $phpcsFile->addError($error, $stackPtr, 'LongLineDeclaration', [$arrayEnding, $this->lineLimit]);
} }
} }//end if
// Only continue for multi line arrays. // Only continue for multi line arrays.
return; return;
......
...@@ -11,15 +11,17 @@ $array = array( ...@@ -11,15 +11,17 @@ $array = array(
'inline' => array(), 'inline' => array(),
'inline1' => array('thisisaverylongstring', 'thisisaverylongstring'), 'inline1' => array('thisisaverylongstring', 'thisisaverylongstring'),
'inline2' => array('thisisaverylongstring', 'thisisaverylongstring', 'thisisaverylongstring'), 'inline2' => array('thisisaverylongstring', 'thisisaverylongstring', 'thisisaverylongstring'),
'inline3' => array('thisisaverylongstring', 'thisisaverylongstring', 'thisisaverylongstring', 'thisisaverylongstring'), 'inline3_not_ok' => array('thisisaverylongstring', 'thisisaverylongstring', 'verylongstring', 'the array ends at 120'),
'inline4' => array('thisisaverylongstringwithallotoftext', 'thisisaverylongstringwithallotoftext'), 'inline4' => array('thisisaverylongstringwithallotoftext', 'thisisaverylongstringwithallotoftext'),
'inline_long_ok' => array('one', 'two', 'three', 'four', 'five', 'six', 'seven'), 'inline_long_ok' => array('one', 'two', 'three', 'four', 'five', 'six', 'seven'),
'inline_long_nok' => array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten'), 'inline_long_not_ok' => array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'array ends at 107'),
'inline_two_elements_ok' => array('one-two-three', 'the-2nd-element-is-within-the-limit'), 'inline_two_elements_ok' => array('one-two-three', 'the-2nd-element-is-within-the-limit'),
'inline_two_elements_ok2' => array('one-two-three-four', 'the-2nd-element-is-right-on-the-limit'), 'inline_two_elements_ok2' => array('one-two-three-four', 'the-2nd-element-is-right-on-the-limit'),
'inline_two_elements_not_ok' => array('one-two-three-four-five', 'the-2nd-element-extends-beyond-the-limit'), 'inline_two_elements_not_ok' => array('one-two-three-four-five', '2nd-element-goes-beyond-the-limit-to-110'),
'inline_two_elements_ok3' => func(['one-two-three-four', 'five'], 'other text which goes past the limit'), 'inline_two_elements_ok3' => func(['one-two-three-four', 'five'], 'other text which goes past the limit'),
'inline_two_elements_ok4' => func(['one-two-three-four', 'this-2nd-element-is-right-on-the-limit'], 'other text'), 'inline_two_elements_ok4' => func(['one-two-three-four', 'this-2nd-element-is-right-on-the-limit'], 'other text'),
'inline_two_elements_ok5' => func(['one-two-three-four'], ['second_array' => 'this-is-ok'], 'other text'), 'inline_two_elements_ok5' => func(['one-two-three-four'], ['second_array' => 'ends at 92'], 'func ends at 113'),
'inline_two_elements_not_ok' => func(['one-two'], ['second_array' => 'three', 'four-five' => 'six'], 'other text'), 'inline_two_elements_not_ok' => func(['one-two'], ['second_array' => 'stops', 'at' => 'column 101'], 'other text'),
'inline_with_nested_functions_but_array_has_one_element_ok' => t('Tags: @tags', ['@tags' => implode(', ', $tags)]),
'inline_one_element_ok' => 'Extends beyond the limit but ok as there is only one element. The array ends at 115'),
); );
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment