Skip to content

Commit

Permalink
Merge 8cf8ee3 into f65bd09
Browse files Browse the repository at this point in the history
  • Loading branch information
jrfnl committed Jan 23, 2020
2 parents f65bd09 + 8cf8ee3 commit 36a82bc
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
36 changes: 26 additions & 10 deletions PHPCSUtils/Utils/PassedParameters.php
Expand Up @@ -13,6 +13,7 @@
use PHP_CodeSniffer\Exceptions\RuntimeException;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Util\Tokens;
use PHPCSUtils\Utils\Arrays;
use PHPCSUtils\Utils\GetTokensAsString;

/**
Expand All @@ -32,14 +33,16 @@ class PassedParameters
* @var array <int|string> => <irrelevant>
*/
private static $allowedConstructs = [
\T_STRING => true,
\T_VARIABLE => true,
\T_SELF => true,
\T_STATIC => true,
\T_ARRAY => true,
\T_OPEN_SHORT_ARRAY => true,
\T_ISSET => true,
\T_UNSET => true,
\T_STRING => true,
\T_VARIABLE => true,
\T_SELF => true,
\T_STATIC => true,
\T_ARRAY => true,
\T_OPEN_SHORT_ARRAY => true,
\T_ISSET => true,
\T_UNSET => true,
// BC for various short array tokenizer issues. See the Arrays class for more details.
\T_OPEN_SQUARE_BRACKET => true,
];

/**
Expand Down Expand Up @@ -101,13 +104,24 @@ public static function hasParameters(File $phpcsFile, $stackPtr)
}
}

if (($tokens[$stackPtr]['code'] === \T_OPEN_SHORT_ARRAY
|| $tokens[$stackPtr]['code'] === \T_OPEN_SQUARE_BRACKET)
&& Arrays::isShortArray($phpcsFile, $stackPtr) === false
) {
throw new RuntimeException(
'The hasParameters() method expects a function call, array, isset or unset token to be passed.'
);
}

$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
if ($next === false) {
return false;
}

// Deal with short array syntax.
if ($tokens[$stackPtr]['code'] === \T_OPEN_SHORT_ARRAY) {
if ($tokens[$stackPtr]['code'] === \T_OPEN_SHORT_ARRAY
|| $tokens[$stackPtr]['code'] === \T_OPEN_SQUARE_BRACKET
) {
if ($next === $tokens[$stackPtr]['bracket_closer']) {
// No parameters.
return false;
Expand Down Expand Up @@ -167,7 +181,9 @@ public static function getParameters(File $phpcsFile, $stackPtr)
$tokens = $phpcsFile->getTokens();

// Mark the beginning and end tokens.
if ($tokens[$stackPtr]['code'] === \T_OPEN_SHORT_ARRAY) {
if ($tokens[$stackPtr]['code'] === \T_OPEN_SHORT_ARRAY
|| $tokens[$stackPtr]['code'] === \T_OPEN_SQUARE_BRACKET
) {
$opener = $stackPtr;
$closer = $tokens[$stackPtr]['bracket_closer'];
} else {
Expand Down
3 changes: 3 additions & 0 deletions Tests/Utils/PassedParameters/HasParametersTest.inc
Expand Up @@ -10,6 +10,9 @@ class Foo {
}
}

/* testShortListNotShortArray */
[ $a, $b ] = $array;

// Function calls: no parameters.

/* testNoParamsFunctionCall1 */
Expand Down
18 changes: 18 additions & 0 deletions Tests/Utils/PassedParameters/HasParametersTest.php
Expand Up @@ -70,6 +70,24 @@ public function testNotACallToConstructor()
PassedParameters::hasParameters(self::$phpcsFile, $self);
}

/**
* Test receiving an expected exception when T_OPEN_SHORT_ARRAY is passed but represents a short list.
*
* @return void
*/
public function testNotAShortArray()
{
$this->expectPhpcsException(
'The hasParameters() method expects a function call, array, isset or unset token to be passed.'
);

$self = $this->getTargetToken(
'/* testShortListNotShortArray */',
[\T_OPEN_SHORT_ARRAY, \T_OPEN_SQUARE_BRACKET]
);
PassedParameters::hasParameters(self::$phpcsFile, $self);
}

/**
* Test correctly identifying whether parameters were passed to a function call or construct.
*
Expand Down

0 comments on commit 36a82bc

Please sign in to comment.