Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
coder
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
coder
Commits
e46990e7
Unverified
Commit
e46990e7
authored
4 years ago
by
Jonathan Smith
Committed by
GitHub
4 years ago
Browse files
Options
Downloads
Patches
Plain Diff
fix(Array): Incorrect counting of array elements by comma (
#3183715
)
parent
3a1d6bb9
No related branches found
No related tags found
1 merge request
!4
Issue #3217297: False positive in closure usage of USE keyword.
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
coder_sniffer/Drupal/Sniffs/Arrays/ArraySniff.php
+35
-3
35 additions, 3 deletions
coder_sniffer/Drupal/Sniffs/Arrays/ArraySniff.php
tests/Drupal/Arrays/ArrayUnitTest.1.inc
+7
-5
7 additions, 5 deletions
tests/Drupal/Arrays/ArrayUnitTest.1.inc
with
42 additions
and
8 deletions
coder_sniffer/Drupal/Sniffs/Arrays/ArraySniff.php
+
35
−
3
View file @
e46990e7
...
@@ -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
;
...
...
This diff is collapsed.
Click to expand it.
tests/Drupal/Arrays/ArrayUnitTest.1.inc
+
7
−
5
View file @
e46990e7
...
@@ -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'
,
'
thisisa
verylongstring'
,
'th
isisaverylongstring
'
),
'inline3
_not_ok
'
=>
array
(
'thisisaverylongstring'
,
'thisisaverylongstring'
,
'verylongstring'
,
'th
e 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_n
ot_
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-
extend
s-beyond-the-limit'
),
'inline_two_elements_not_ok'
=>
array
(
'one-two-three-four-five'
,
'2nd-element-
goe
s-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'
),
);
);
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment