diff --git a/PHPCSUtils/BackCompat/BCFile.php b/PHPCSUtils/BackCompat/BCFile.php index cef6c9a2..e3aaa681 100644 --- a/PHPCSUtils/BackCompat/BCFile.php +++ b/PHPCSUtils/BackCompat/BCFile.php @@ -298,7 +298,11 @@ public static function getMethodParameters(File $phpcsFile, $stackPtr) if ($tokens[$stackPtr]['code'] === T_USE) { $opener = $phpcsFile->findNext(T_OPEN_PARENTHESIS, ($stackPtr + 1)); - if ($opener === false || isset($tokens[$opener]['parenthesis_owner']) === true) { + if ($opener === false + || (isset($tokens[$opener]['parenthesis_owner']) === true + // BC: as of PHPCS 4.x, closure use tokens are parentheses owners. + && $tokens[$opener]['parenthesis_owner'] !== $stackPtr) + ) { throw new RuntimeException('$stackPtr was not a valid T_USE'); } } elseif ($arrowOpenClose !== false) { diff --git a/PHPCSUtils/Utils/FunctionDeclarations.php b/PHPCSUtils/Utils/FunctionDeclarations.php index 69257aec..1b123c31 100644 --- a/PHPCSUtils/Utils/FunctionDeclarations.php +++ b/PHPCSUtils/Utils/FunctionDeclarations.php @@ -436,6 +436,7 @@ public static function getParameters(File $phpcsFile, $stackPtr) } if ($tokens[$stackPtr]['code'] === \T_USE) { + // This will work PHPCS 3.x/4.x cross-version without much overhead. $opener = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); if ($opener === false || $tokens[$opener]['code'] !== \T_OPEN_PARENTHESIS diff --git a/PHPCSUtils/Utils/UseStatements.php b/PHPCSUtils/Utils/UseStatements.php index 06187b7b..7e371da6 100644 --- a/PHPCSUtils/Utils/UseStatements.php +++ b/PHPCSUtils/Utils/UseStatements.php @@ -59,6 +59,13 @@ public static function getType(File $phpcsFile, $stackPtr) return ''; } + // More efficient & simpler check for closure use in PHPCS 4.x. + if (isset($tokens[$stackPtr]['parenthesis_owner']) + && $tokens[$stackPtr]['parenthesis_owner'] === $stackPtr + ) { + return 'closure'; + } + $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true); if ($prev !== false && $tokens[$prev]['code'] === \T_CLOSE_PARENTHESIS && Parentheses::isOwnerIn($phpcsFile, $prev, \T_CLOSURE) === true