Skip to content

Commit

Permalink
Merge pull request #251 from PHPCSStandards/phpcs-4.x/handle-closure-…
Browse files Browse the repository at this point in the history
…use-parenthesis-owner

PHPCS 4.x | Handle closure use being a parenthesis owner
  • Loading branch information
jrfnl committed May 12, 2021
2 parents cb5f20c + 56388c5 commit 089c165
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
6 changes: 5 additions & 1 deletion PHPCSUtils/BackCompat/BCFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions PHPCSUtils/Utils/FunctionDeclarations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions PHPCSUtils/Utils/UseStatements.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 089c165

Please sign in to comment.