Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion PHPCSUtils/Utils/Arrays.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,8 @@ public static function getDoubleArrowPtr(File $phpcsFile, $start, $end)
);
}

$targets = self::$doubleArrowTargets + Collections::$closedScopes;
$targets = self::$doubleArrowTargets;
$targets += Collections::$closedScopes;
$targets += Collections::arrowFunctionTokensBC();

$doubleArrow = ($start - 1);
Expand All @@ -285,6 +286,14 @@ public static function getDoubleArrowPtr(File $phpcsFile, $start, $end)
return $doubleArrow;
}

/*
* BC: work-around a bug in PHPCS 3.5.4 where the double arrow is incorrectly tokenized as T_STRING.
* @link https://github.com/squizlabs/PHP_CodeSniffer/issues/2865
*/
if ($tokens[$doubleArrow]['code'] === \T_STRING && $tokens[$doubleArrow]['content'] === '=>') {
return $doubleArrow;
}

// Skip over closed scopes which may contain foreach structures or generators.
if (isset(Collections::$closedScopes[$tokens[$doubleArrow]['code']]) === true
&& isset($tokens[$doubleArrow]['scope_closer']) === true
Expand Down
5 changes: 4 additions & 1 deletion Tests/Utils/Arrays/GetDoubleArrowPtrTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ $array = [
/* testTstringKeyNotFnFunction */
CONSTANT_NAME => 'value',

/* testPropertyAccessPHPCS353-354 */
/* testKeyPropertyAccessFnPHPCS353-354 */
($obj->fn) => 'value',

/* testDoubleArrowTokenizedAsTstring-PHPCS2865 */
$obj->fn => 'value',
];
6 changes: 5 additions & 1 deletion Tests/Utils/Arrays/GetDoubleArrowPtrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,13 @@ public function dataGetDoubleArrowPtr()
],
// Test specifically for PHPCS 3.5.3 and 3.5.4 in which all "fn" tokens were tokenized as T_FN.
'test-arrow-access-to-property-named-fn-as-key-phpcs-3.5.3-3.5.4' => [
'/* testPropertyAccessPHPCS353-354 */',
'/* testKeyPropertyAccessFnPHPCS353-354 */',
12,
],
'test-double-arrow-incorrectly-tokenized-phpcs-issue-2865' => [
'/* testDoubleArrowTokenizedAsTstring-PHPCS2865 */',
10,
],
];
}
}