Skip to content

Commit

Permalink
Merge pull request #166 from PHPCSStandards/feature/php8-isshortarray…
Browse files Browse the repository at this point in the history
…-isshortlist

Arrays::isShortArray(): work round incorrect tokenization of PHP8 magic constant dereferencing
  • Loading branch information
jrfnl committed Jul 10, 2020
2 parents e6bafb0 + 94e1880 commit ab418be
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
28 changes: 21 additions & 7 deletions PHPCSUtils/Utils/Arrays.php
Expand Up @@ -71,7 +71,7 @@ public static function isShortArray(File $phpcsFile, $stackPtr)
return false;
}

// All known tokenizer bugs are in PHPCS versions before 3.3.0.
// All known tokenizer bugs are in PHPCS versions before 3.x.x (?) - PHPCS#3013.
$phpcsVersion = Helper::getVersion();

/*
Expand Down Expand Up @@ -131,20 +131,34 @@ public static function isShortArray(File $phpcsFile, $stackPtr)
/*
* Deal with short array brackets which may be incorrectly tokenized plain square brackets.
*/
if (\version_compare($phpcsVersion, '2.9.0', '<')) {
$opener = $stackPtr;
if ($tokens[$stackPtr]['code'] === \T_CLOSE_SHORT_ARRAY) {
$opener = $tokens[$stackPtr]['bracket_opener'];
}
$opener = $stackPtr;
if ($tokens[$stackPtr]['code'] === \T_CLOSE_SHORT_ARRAY) {
$opener = $tokens[$stackPtr]['bracket_opener'];
}

$prevNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($opener - 1), null, true);

// if (\version_compare($phpcsVersion, '3.x.x', '<')) {
/*
* BC: Work around a bug in the tokenizer of PHPCS < 3.x.x (?) where dereferencing
* of magic constants (PHP 8+) would be incorrectly tokenized as short array.
* I.e. the square brackets in `__FILE__[0]` would be tokenized as short array.
*
* @link https://github.com/squizlabs/PHP_CodeSniffer/pull/3013
*/
if (isset(Collections::$magicConstants[$tokens[$prevNonEmpty]['code']]) === true) {
return false;
}
// }

if (\version_compare($phpcsVersion, '2.9.0', '<')) {
/*
* BC: Work around a bug in the tokenizer of PHPCS < 2.9.0 where array dereferencing
* of short array and string literals would be incorrectly tokenized as short array.
* I.e. the square brackets in `'PHP'[0]` would be tokenized as short array.
*
* @link https://github.com/squizlabs/PHP_CodeSniffer/issues/1381
*/
$prevNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($opener - 1), null, true);
if ($tokens[$prevNonEmpty]['code'] === \T_CLOSE_SHORT_ARRAY
|| $tokens[$prevNonEmpty]['code'] === \T_CONSTANT_ENCAPSED_STRING
) {
Expand Down
4 changes: 4 additions & 0 deletions Tests/Utils/Arrays/IsShortArrayTokenizerBC1Test.php
Expand Up @@ -124,6 +124,10 @@ public function dataIsShortArray()
'/* testTokenizerIssue1284PHPCSlt280D */',
false,
],
'issue-3013-magic-constant-dereferencing' => [
'/* testTokenizerIssue3013PHPCSlt3xx */',
false,
],
];
}
}
3 changes: 3 additions & 0 deletions Tests/Utils/Lists/IsShortListTokenizerBC1Test.inc
Expand Up @@ -29,3 +29,6 @@ $foo = ${$bar}['key'];

/* testTokenizerIssue1284PHPCSlt280D */
$c->{$var}[ ] = 2;

/* testTokenizerIssue3013PHPCSlt3xx */
$var = __FILE__[0];
4 changes: 4 additions & 0 deletions Tests/Utils/Lists/IsShortListTokenizerBC1Test.php
Expand Up @@ -102,6 +102,10 @@ public function dataIsShortList()
'/* testTokenizerIssue1284PHPCSlt280D */',
false,
],
'issue-3013-magic-constant-dereferencing' => [
'/* testTokenizerIssue3013PHPCSlt3xx */',
false,
],
];
}
}

0 comments on commit ab418be

Please sign in to comment.