Skip to content

Commit e11d8f9

Browse files
authored
Merge pull request #381 from PHPCSStandards/tokens/collection-add-new-shortarraylistopentokensbc
Collections: add new `shortArrayListOpenTokensBC()` token array
2 parents 18f5d8f + 8ee11f2 commit e11d8f9

13 files changed

+46
-25
lines changed

PHPCSUtils/Tokens/Collections.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
* @method static array objectOperators() Object operator tokens.
4646
* @method static array phpOpenTags() Tokens which open PHP.
4747
* @method static array propertyModifierKeywords() Modifier keywords which can be used for a property declaration.
48+
* @method static array shortArrayListOpenTokensBC() Tokens which can open a short array or short list
49+
* (PHPCS cross-version compatible).
4850
* @method static array shortArrayTokens() Tokens which are used for short arrays.
4951
* @method static array shortArrayTokensBC() Tokens which are used for short arrays
5052
* (PHPCS cross-version compatible).
@@ -577,6 +579,22 @@ final class Collections
577579
\T_TYPE_INTERSECTION => \T_TYPE_INTERSECTION,
578580
];
579581

582+
/**
583+
* Tokens which can open a short array or short list (PHPCS cross-version compatible).
584+
*
585+
* Includes `T_OPEN_SQUARE_BRACKET` to allow for handling intermittent tokenizer issues related
586+
* to the retokenization to `T_OPEN_SHORT_ARRAY`.
587+
* Should only be used selectively.
588+
*
589+
* @since 1.0.0-alpha4 Use the {@see Collections::shortArrayListOpenTokensBC()} method for access.
590+
*
591+
* @return array <int|string> => <int|string>
592+
*/
593+
private static $shortArrayListOpenTokensBC = [
594+
\T_OPEN_SHORT_ARRAY => \T_OPEN_SHORT_ARRAY,
595+
\T_OPEN_SQUARE_BRACKET => \T_OPEN_SQUARE_BRACKET,
596+
];
597+
580598
/**
581599
* DEPRECATED: Tokens which are used for short arrays.
582600
*

PHPCSUtils/Utils/Lists.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ public static function isShortList(File $phpcsFile, $stackPtr)
232232
$parentOpen = $opener;
233233
do {
234234
$parentOpen = $phpcsFile->findPrevious(
235-
[\T_OPEN_SHORT_ARRAY, \T_OPEN_SQUARE_BRACKET], // BC: PHPCS#1971.
235+
Collections::shortArrayListOpenTokensBC(), // BC: PHPCS#1971.
236236
($parentOpen - 1),
237237
null,
238238
false,

PHPCSUtils/Utils/Namespaces.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,10 @@ public static function getType(File $phpcsFile, $stackPtr)
6464
+ Tokens::$castTokens
6565
+ Tokens::$blockOpeners
6666
+ Collections::incrementDecrementOperators()
67-
+ Collections::objectOperators();
67+
+ Collections::objectOperators()
68+
+ Collections::shortArrayListOpenTokensBC();
6869

69-
$findAfter[\T_OPEN_CURLY_BRACKET] = \T_OPEN_CURLY_BRACKET;
70-
$findAfter[\T_OPEN_SQUARE_BRACKET] = \T_OPEN_SQUARE_BRACKET;
71-
$findAfter[\T_OPEN_SHORT_ARRAY] = \T_OPEN_SHORT_ARRAY;
70+
$findAfter[\T_OPEN_CURLY_BRACKET] = \T_OPEN_CURLY_BRACKET;
7271
}
7372

7473
$tokens = $phpcsFile->getTokens();

PHPCSUtils/Utils/PassedParameters.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,7 @@ public static function hasParameters(File $phpcsFile, $stackPtr, $isShortArray =
103103
}
104104
}
105105

106-
if (($tokens[$stackPtr]['code'] === \T_OPEN_SHORT_ARRAY
107-
|| $tokens[$stackPtr]['code'] === \T_OPEN_SQUARE_BRACKET)
106+
if (isset(Collections::shortArrayListOpenTokensBC()[$tokens[$stackPtr]['code']]) === true
108107
&& $isShortArray !== true
109108
&& Arrays::isShortArray($phpcsFile, $stackPtr) === false
110109
) {
@@ -119,9 +118,7 @@ public static function hasParameters(File $phpcsFile, $stackPtr, $isShortArray =
119118
}
120119

121120
// Deal with short array syntax.
122-
if ($tokens[$stackPtr]['code'] === \T_OPEN_SHORT_ARRAY
123-
|| $tokens[$stackPtr]['code'] === \T_OPEN_SQUARE_BRACKET
124-
) {
121+
if (isset(Collections::shortArrayListOpenTokensBC()[$tokens[$stackPtr]['code']]) === true) {
125122
if ($next === $tokens[$stackPtr]['bracket_closer']) {
126123
// No parameters.
127124
return false;
@@ -224,9 +221,7 @@ public static function getParameters(File $phpcsFile, $stackPtr, $limit = 0, $is
224221
$tokens = $phpcsFile->getTokens();
225222

226223
// Mark the beginning and end tokens.
227-
if ($tokens[$stackPtr]['code'] === \T_OPEN_SHORT_ARRAY
228-
|| $tokens[$stackPtr]['code'] === \T_OPEN_SQUARE_BRACKET
229-
) {
224+
if (isset(Collections::shortArrayListOpenTokensBC()[$tokens[$stackPtr]['code']]) === true) {
230225
$opener = $stackPtr;
231226
$closer = $tokens[$stackPtr]['bracket_closer'];
232227
} else {

Tests/Tokens/Collections/PropertyBasedTokenArraysTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public function dataPropertyBasedTokenArrays()
7575
'objectOperators',
7676
'phpOpenTags',
7777
'propertyModifierKeywords',
78+
'shortArrayListOpenTokensBC',
7879
'shortArrayTokens',
7980
'shortArrayTokensBC',
8081
'shortListTokens',

Tests/Utils/Arrays/GetOpenCloseTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace PHPCSUtils\Tests\Utils\Arrays;
1212

1313
use PHPCSUtils\TestUtils\UtilityMethodTestCase;
14+
use PHPCSUtils\Tokens\Collections;
1415
use PHPCSUtils\Utils\Arrays;
1516

1617
/**
@@ -46,7 +47,7 @@ public function testNonExistentToken()
4647
*/
4748
public function testNotArrayToken($testMarker)
4849
{
49-
$target = $this->getTargetToken($testMarker, [\T_OPEN_SHORT_ARRAY, \T_OPEN_SQUARE_BRACKET]);
50+
$target = $this->getTargetToken($testMarker, Collections::shortArrayListOpenTokensBC());
5051
$this->assertFalse(Arrays::getOpenClose(self::$phpcsFile, $target));
5152
}
5253

Tests/Utils/Lists/GetAssignmentsTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
use PHPCSUtils\Internal\Cache;
1414
use PHPCSUtils\TestUtils\UtilityMethodTestCase;
15+
use PHPCSUtils\Tokens\Collections;
1516
use PHPCSUtils\Utils\Lists;
1617

1718
/**
@@ -539,7 +540,7 @@ public function dataGetAssignments()
539540
],
540541
'short-list-with-keys-nested-lists' => [
541542
'testMarker' => '/* testShortListWithKeysNestedLists */',
542-
'targetToken' => [\T_OPEN_SHORT_ARRAY, \T_OPEN_SQUARE_BRACKET],
543+
'targetToken' => Collections::shortArrayListOpenTokensBC(),
543544
'expected' => [
544545
0 => [
545546
'raw' => '\'a\' => [&$a, $b]',

Tests/Utils/Lists/GetOpenCloseTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace PHPCSUtils\Tests\Utils\Lists;
1212

1313
use PHPCSUtils\TestUtils\UtilityMethodTestCase;
14+
use PHPCSUtils\Tokens\Collections;
1415
use PHPCSUtils\Utils\Lists;
1516

1617
/**
@@ -46,7 +47,7 @@ public function testNonExistentToken()
4647
*/
4748
public function testNotListToken($testMarker)
4849
{
49-
$target = $this->getTargetToken($testMarker, [\T_OPEN_SHORT_ARRAY, \T_OPEN_SQUARE_BRACKET]);
50+
$target = $this->getTargetToken($testMarker, Collections::shortArrayListOpenTokensBC());
5051
$this->assertFalse(Lists::getOpenClose(self::$phpcsFile, $target));
5152
}
5253

Tests/Utils/Lists/IsShortArrayOrListTokenizerBC1Test.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace PHPCSUtils\Tests\Utils\Lists;
1212

1313
use PHPCSUtils\TestUtils\UtilityMethodTestCase;
14+
use PHPCSUtils\Tokens\Collections;
1415
use PHPCSUtils\Utils\Arrays;
1516
use PHPCSUtils\Utils\Lists;
1617

@@ -63,7 +64,7 @@ public function testValidDataProvider($ignore, $data)
6364
*/
6465
public function testIsShortArray($testMarker, $expected)
6566
{
66-
$stackPtr = $this->getTargetToken($testMarker, [\T_OPEN_SHORT_ARRAY, \T_OPEN_SQUARE_BRACKET]);
67+
$stackPtr = $this->getTargetToken($testMarker, Collections::shortArrayListOpenTokensBC());
6768
$result = Arrays::isShortArray(self::$phpcsFile, $stackPtr);
6869

6970
$this->assertSame($expected['array'], $result);
@@ -83,7 +84,7 @@ public function testIsShortArray($testMarker, $expected)
8384
*/
8485
public function testIsShortList($testMarker, $expected)
8586
{
86-
$stackPtr = $this->getTargetToken($testMarker, [\T_OPEN_SHORT_ARRAY, \T_OPEN_SQUARE_BRACKET]);
87+
$stackPtr = $this->getTargetToken($testMarker, Collections::shortArrayListOpenTokensBC());
8788
$result = Lists::isShortList(self::$phpcsFile, $stackPtr);
8889

8990
$this->assertSame($expected['list'], $result);

Tests/Utils/Lists/IsShortArrayOrListTokenizerBC2Test.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace PHPCSUtils\Tests\Utils\Lists;
1212

1313
use PHPCSUtils\TestUtils\UtilityMethodTestCase;
14+
use PHPCSUtils\Tokens\Collections;
1415
use PHPCSUtils\Utils\Arrays;
1516
use PHPCSUtils\Utils\Lists;
1617

@@ -63,7 +64,7 @@ public function testValidDataProvider($ignore, $data)
6364
*/
6465
public function testIsShortArray($testMarker, $expected)
6566
{
66-
$stackPtr = $this->getTargetToken($testMarker, [\T_OPEN_SHORT_ARRAY, \T_OPEN_SQUARE_BRACKET]);
67+
$stackPtr = $this->getTargetToken($testMarker, Collections::shortArrayListOpenTokensBC());
6768
$result = Arrays::isShortArray(self::$phpcsFile, $stackPtr);
6869

6970
$this->assertSame($expected['array'], $result);
@@ -83,7 +84,7 @@ public function testIsShortArray($testMarker, $expected)
8384
*/
8485
public function testIsShortList($testMarker, $expected)
8586
{
86-
$stackPtr = $this->getTargetToken($testMarker, [\T_OPEN_SHORT_ARRAY, \T_OPEN_SQUARE_BRACKET]);
87+
$stackPtr = $this->getTargetToken($testMarker, Collections::shortArrayListOpenTokensBC());
8788
$result = Lists::isShortList(self::$phpcsFile, $stackPtr);
8889

8990
$this->assertSame($expected['list'], $result);

0 commit comments

Comments
 (0)