Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arrays::getDoubleArrowPtr(): allow for (keyed) lists in array values #485

Merged
merged 1 commit into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions PHPCSUtils/Utils/Arrays.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ final class Arrays
\T_OPEN_SHORT_ARRAY => \T_OPEN_SHORT_ARRAY,

// Inline function, control structures and other things to skip over.
\T_LIST => \T_LIST,
\T_FN => \T_FN,
\T_MATCH => \T_MATCH,
\T_ATTRIBUTE => \T_ATTRIBUTE,
Expand Down Expand Up @@ -208,6 +209,14 @@ public static function getDoubleArrowPtr(File $phpcsFile, $start, $end)
continue;
}

// Skip over potentially keyed long lists.
if ($tokens[$doubleArrow]['code'] === \T_LIST
&& isset($tokens[$doubleArrow]['parenthesis_closer'])
) {
$doubleArrow = $tokens[$doubleArrow]['parenthesis_closer'];
continue;
}

// Start of nested long/short array.
break;
} while ($doubleArrow < $end);
Expand Down
6 changes: 6 additions & 0 deletions Tests/Utils/Arrays/GetDoubleArrowPtrTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ $array = [
FOO => BAR,
default => [0 => 10],
} => 'value',

/* testNoArrowKeyedLongListInValue */
list( 'key1' => $a, 'key2' => $b ) = $array,

/* testNoArrowKeyedShortListInValue */
[ 'key1' => $a, 'key2' => $b ] = $array,

/* testNoArrowValueClosureWithAttribute */
#[MyAttribute([0 => 'value'])] function() { /* do something */ }(),
Expand Down
10 changes: 10 additions & 0 deletions Tests/Utils/Arrays/GetDoubleArrowPtrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,16 @@ public static function dataGetDoubleArrowPtr()
'expected' => 38,
],

// Safeguard that PHP 7.2 keyed lists in values are handled correctly.
'test-no-arrow-value-keyed-long-list' => [
'testMarker' => '/* testNoArrowKeyedLongListInValue */',
'expected' => false,
],
'test-no-arrow-value-keyed-short-list' => [
'testMarker' => '/* testNoArrowKeyedShortListInValue */',
'expected' => false,
],

// Safeguard that double arrows in PHP 8.0 attributes are disregarded.
'test-no-arrow-value-closure-with-attached-attribute-containing-arrow' => [
'testMarker' => '/* testNoArrowValueClosureWithAttribute */',
Expand Down