Skip to content

Commit 08824f3

Browse files
committed
T_USE tokens now contain parenthesis information if they are being used to pass variables to a closure (ref #2593)
1 parent 8eb94c6 commit 08824f3

File tree

5 files changed

+18
-19
lines changed

5 files changed

+18
-19
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ The file documents changes to the PHP_CodeSniffer project.
3838
- The `--report-file` functionality remains untouched
3939
- Composer installs no longer include any test files
4040
- The `Config::setConfigData()` method is no longer static
41+
- T_USE tokens now contain parenthesis information if they are being used to pass variables to a closure
42+
- Previously, you had to find the opening and closing parenthesis by looking forward through the token stack
43+
- Now, you can use the `parenthesis_opener` and `parenthesis_closer` array indexes
4144

4245
### Removed
4346
- Removed support for installing via PEAR

src/Files/File.php

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,20 +1265,13 @@ public function getMethodParameters($stackPtr)
12651265
throw new RuntimeException('$stackPtr must be of type T_FUNCTION or T_CLOSURE or T_USE or T_FN');
12661266
}
12671267

1268-
if ($this->tokens[$stackPtr]['code'] === T_USE) {
1269-
$opener = $this->findNext(T_OPEN_PARENTHESIS, ($stackPtr + 1));
1270-
if ($opener === false || isset($this->tokens[$opener]['parenthesis_owner']) === true) {
1271-
throw new RuntimeException('$stackPtr was not a valid T_USE');
1272-
}
1273-
} else {
1274-
if (isset($this->tokens[$stackPtr]['parenthesis_opener']) === false) {
1275-
// Live coding or syntax error, so no params to find.
1276-
return [];
1277-
}
1278-
1279-
$opener = $this->tokens[$stackPtr]['parenthesis_opener'];
1268+
if (isset($this->tokens[$stackPtr]['parenthesis_opener']) === false) {
1269+
// Live coding or syntax error, so no params to find.
1270+
return [];
12801271
}
12811272

1273+
$opener = $this->tokens[$stackPtr]['parenthesis_opener'];
1274+
12821275
if (isset($this->tokens[$opener]['parenthesis_closer']) === false) {
12831276
// Live coding or syntax error, so no params to find.
12841277
return [];

src/Sniffs/AbstractVariableSniff.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,19 +118,13 @@ final protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $cu
118118
if ($inFunction === false && isset($tokens[$stackPtr]['nested_parenthesis']) === true) {
119119
foreach ($tokens[$stackPtr]['nested_parenthesis'] as $opener => $closer) {
120120
if (isset($tokens[$opener]['parenthesis_owner']) === false) {
121-
// Check if this is a USE statement for a closure.
122-
$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($opener - 1), null, true);
123-
if ($tokens[$prev]['code'] === T_USE) {
124-
$inFunction = true;
125-
break;
126-
}
127-
128121
continue;
129122
}
130123

131124
$owner = $tokens[$opener]['parenthesis_owner'];
132125
if ($tokens[$owner]['code'] === T_FUNCTION
133126
|| $tokens[$owner]['code'] === T_CLOSURE
127+
|| $tokens[$owner]['code'] === T_USE
134128
) {
135129
$inFunction = true;
136130
break;

src/Tokenizers/Tokenizer.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,10 @@ private function createTokenMap()
700700
if (PHP_CODESNIFFER_VERBOSITY > 1) {
701701
Common::printStatusMessage("=> Found square bracket opener at $i", (count($squareOpeners) + count($curlyOpeners)));
702702
}
703+
704+
if ($openOwner !== null) {
705+
$openOwner = null;
706+
}
703707
break;
704708
case T_OPEN_CURLY_BRACKET:
705709
if (isset($this->tokens[$i]['scope_closer']) === false) {
@@ -709,6 +713,10 @@ private function createTokenMap()
709713
Common::printStatusMessage("=> Found curly bracket opener at $i", (count($squareOpeners) + count($curlyOpeners)));
710714
}
711715
}
716+
717+
if ($openOwner !== null) {
718+
$openOwner = null;
719+
}
712720
break;
713721
case T_CLOSE_SQUARE_BRACKET:
714722
if (empty($squareOpeners) === false) {

src/Util/Tokens.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ final class Tokens
351351
T_LIST => T_LIST,
352352
T_FUNCTION => T_FUNCTION,
353353
T_CLOSURE => T_CLOSURE,
354+
T_USE => T_USE,
354355
T_ANON_CLASS => T_ANON_CLASS,
355356
T_WHILE => T_WHILE,
356357
T_FOR => T_FOR,

0 commit comments

Comments
 (0)