Loading coder_sniffer/Drupal/Sniffs/Arrays/ArraySniff.php +35 −3 Original line number Diff line number Diff line Loading @@ -116,12 +116,44 @@ class ArraySniff implements Sniff $arrayEnding = $tokens[$tokens[$stackPtr][$parenthesisCloser]]['column']; if ($arrayEnding > $this->lineLimit) { $comma1 = $phpcsFile->findNext(T_COMMA, ($stackPtr + 1), $tokens[$stackPtr][$parenthesisCloser]); if ($comma1 !== false) { // Nested arrays and function calls within array elements may contain commas so we // 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'; $phpcsFile->addError($error, $stackPtr, 'LongLineDeclaration', [$arrayEnding, $this->lineLimit]); } } }//end if // Only continue for multi line arrays. return; Loading tests/Drupal/Arrays/ArrayUnitTest.1.inc +7 −5 Original line number Diff line number Diff line Loading @@ -11,15 +11,17 @@ $array = array( 'inline' => array(), 'inline1' => array('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'), '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_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_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_not_ok' => func(['one-two'], ['second_array' => 'three', 'four-five' => 'six'], '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' => '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'), ); Loading
coder_sniffer/Drupal/Sniffs/Arrays/ArraySniff.php +35 −3 Original line number Diff line number Diff line Loading @@ -116,12 +116,44 @@ class ArraySniff implements Sniff $arrayEnding = $tokens[$tokens[$stackPtr][$parenthesisCloser]]['column']; if ($arrayEnding > $this->lineLimit) { $comma1 = $phpcsFile->findNext(T_COMMA, ($stackPtr + 1), $tokens[$stackPtr][$parenthesisCloser]); if ($comma1 !== false) { // Nested arrays and function calls within array elements may contain commas so we // 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'; $phpcsFile->addError($error, $stackPtr, 'LongLineDeclaration', [$arrayEnding, $this->lineLimit]); } } }//end if // Only continue for multi line arrays. return; Loading
tests/Drupal/Arrays/ArrayUnitTest.1.inc +7 −5 Original line number Diff line number Diff line Loading @@ -11,15 +11,17 @@ $array = array( 'inline' => array(), 'inline1' => array('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'), '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_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_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_not_ok' => func(['one-two'], ['second_array' => 'three', 'four-five' => 'six'], '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' => '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'), );