From 6e377c078fe3dd2981048d8732d17f6736b27eab Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 5 Sep 2020 20:19:31 +0200 Subject: [PATCH 1/3] PHPCS 4.x | UseStatements::getType(): efficiency tweak for closure use `T_USE` tokens used for closure use statements are parentheses owners as of PHPCS 4.x. So, as of PHPCS 4.x, checking for the `parenthesis_owner` index being set is a simpler and more efficient way to determine if a `use` token is a closure use. Refs: * squizlabs/PHP_CodeSniffer 2593 * https://github.com/squizlabs/PHP_CodeSniffer/commit/08824f327ce5e20d7528c5e838dd7fade3dcd11c# * squizlabs/PHP_CodeSniffer 3104 --- PHPCSUtils/Utils/UseStatements.php | 7 +++++++ 1 file changed, 7 insertions(+) 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 From ff705f0ef9f6d2e767dcb5e1ff76e388990ba65b Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 5 Sep 2020 20:25:04 +0200 Subject: [PATCH 2/3] PHPCS 4.x | BCFile::getMethodParameters(): T_USE is now a parenthesis owner `T_USE` tokens used for closure use statements are parentheses owners as of PHPCS 4.x. This fixes compatibility of the `BCFile::getMethodParameters()` method with PHPCS 4.x. The existing unit tests already cover this. Refs: * squizlabs/PHP_CodeSniffer 2593 * https://github.com/squizlabs/PHP_CodeSniffer/commit/08824f327ce5e20d7528c5e838dd7fade3dcd11c# * squizlabs/PHP_CodeSniffer 3104 --- PHPCSUtils/BackCompat/BCFile.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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) { From 56388c57ecf097e23494442776b88277d5fa7718 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 5 Sep 2020 20:39:26 +0200 Subject: [PATCH 3/3] PHPCS 4.x | FunctionDeclarations::getParameters(): document T_USE handling `T_USE` tokens used for closure use statements are parentheses owners as of PHPCS 4.x. The `FunctionDeclarations::getParameters()` method already handles this correctly cross-version. This just adds documentation to confirm this has been looked at and no changes are needed for cross-version compatibility. Refs: * squizlabs/PHP_CodeSniffer 2593 * https://github.com/squizlabs/PHP_CodeSniffer/commit/08824f327ce5e20d7528c5e838dd7fade3dcd11c# * squizlabs/PHP_CodeSniffer 3104 --- PHPCSUtils/Utils/FunctionDeclarations.php | 1 + 1 file changed, 1 insertion(+) 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