Skip to content

Commit 01bc6e0

Browse files
authored
Merge pull request #56 from PHPCSStandards/feature/passedparameters-fix-bc-for-short-arrays
Utils\PassedParameters: fix PHPC cross-version compatibility for short arrays
2 parents f65bd09 + 8cf8ee3 commit 01bc6e0

File tree

3 files changed

+47
-10
lines changed

3 files changed

+47
-10
lines changed

PHPCSUtils/Utils/PassedParameters.php

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use PHP_CodeSniffer\Exceptions\RuntimeException;
1414
use PHP_CodeSniffer\Files\File;
1515
use PHP_CodeSniffer\Util\Tokens;
16+
use PHPCSUtils\Utils\Arrays;
1617
use PHPCSUtils\Utils\GetTokensAsString;
1718

1819
/**
@@ -32,14 +33,16 @@ class PassedParameters
3233
* @var array <int|string> => <irrelevant>
3334
*/
3435
private static $allowedConstructs = [
35-
\T_STRING => true,
36-
\T_VARIABLE => true,
37-
\T_SELF => true,
38-
\T_STATIC => true,
39-
\T_ARRAY => true,
40-
\T_OPEN_SHORT_ARRAY => true,
41-
\T_ISSET => true,
42-
\T_UNSET => true,
36+
\T_STRING => true,
37+
\T_VARIABLE => true,
38+
\T_SELF => true,
39+
\T_STATIC => true,
40+
\T_ARRAY => true,
41+
\T_OPEN_SHORT_ARRAY => true,
42+
\T_ISSET => true,
43+
\T_UNSET => true,
44+
// BC for various short array tokenizer issues. See the Arrays class for more details.
45+
\T_OPEN_SQUARE_BRACKET => true,
4346
];
4447

4548
/**
@@ -101,13 +104,24 @@ public static function hasParameters(File $phpcsFile, $stackPtr)
101104
}
102105
}
103106

107+
if (($tokens[$stackPtr]['code'] === \T_OPEN_SHORT_ARRAY
108+
|| $tokens[$stackPtr]['code'] === \T_OPEN_SQUARE_BRACKET)
109+
&& Arrays::isShortArray($phpcsFile, $stackPtr) === false
110+
) {
111+
throw new RuntimeException(
112+
'The hasParameters() method expects a function call, array, isset or unset token to be passed.'
113+
);
114+
}
115+
104116
$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
105117
if ($next === false) {
106118
return false;
107119
}
108120

109121
// Deal with short array syntax.
110-
if ($tokens[$stackPtr]['code'] === \T_OPEN_SHORT_ARRAY) {
122+
if ($tokens[$stackPtr]['code'] === \T_OPEN_SHORT_ARRAY
123+
|| $tokens[$stackPtr]['code'] === \T_OPEN_SQUARE_BRACKET
124+
) {
111125
if ($next === $tokens[$stackPtr]['bracket_closer']) {
112126
// No parameters.
113127
return false;
@@ -167,7 +181,9 @@ public static function getParameters(File $phpcsFile, $stackPtr)
167181
$tokens = $phpcsFile->getTokens();
168182

169183
// Mark the beginning and end tokens.
170-
if ($tokens[$stackPtr]['code'] === \T_OPEN_SHORT_ARRAY) {
184+
if ($tokens[$stackPtr]['code'] === \T_OPEN_SHORT_ARRAY
185+
|| $tokens[$stackPtr]['code'] === \T_OPEN_SQUARE_BRACKET
186+
) {
171187
$opener = $stackPtr;
172188
$closer = $tokens[$stackPtr]['bracket_closer'];
173189
} else {

Tests/Utils/PassedParameters/HasParametersTest.inc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ class Foo {
1010
}
1111
}
1212

13+
/* testShortListNotShortArray */
14+
[ $a, $b ] = $array;
15+
1316
// Function calls: no parameters.
1417

1518
/* testNoParamsFunctionCall1 */

Tests/Utils/PassedParameters/HasParametersTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,24 @@ public function testNotACallToConstructor()
7070
PassedParameters::hasParameters(self::$phpcsFile, $self);
7171
}
7272

73+
/**
74+
* Test receiving an expected exception when T_OPEN_SHORT_ARRAY is passed but represents a short list.
75+
*
76+
* @return void
77+
*/
78+
public function testNotAShortArray()
79+
{
80+
$this->expectPhpcsException(
81+
'The hasParameters() method expects a function call, array, isset or unset token to be passed.'
82+
);
83+
84+
$self = $this->getTargetToken(
85+
'/* testShortListNotShortArray */',
86+
[\T_OPEN_SHORT_ARRAY, \T_OPEN_SQUARE_BRACKET]
87+
);
88+
PassedParameters::hasParameters(self::$phpcsFile, $self);
89+
}
90+
7391
/**
7492
* Test correctly identifying whether parameters were passed to a function call or construct.
7593
*

0 commit comments

Comments
 (0)