Skip to content

Commit 87e7a62

Browse files
committed
Various sniffs + File methods: remove redundant code/simplify for T_USE parenthesis owner
As `T_USE` tokens are now parenthesis owners and will have the `parenthesis_*` keys, sniffs do not need to token walk anymore to find the parentheses (or the `T_USE` token). It has been confirmed that the changes in this commit are all covered by pre-existing tests. Replaces squizlabs/PHP_CodeSniffer 3338 Closes 43
1 parent de8442a commit 87e7a62

File tree

8 files changed

+32
-58
lines changed

8 files changed

+32
-58
lines changed

src/Files/File.php

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1698,18 +1698,15 @@ public function getMethodProperties($stackPtr)
16981698
break;
16991699
}
17001700

1701+
// Skip over closure use statements.
17011702
if ($this->tokens[$i]['code'] === T_USE) {
1702-
// Skip over closure use statements.
1703-
for ($j = ($i + 1); $j < $this->numTokens && isset(Tokens::$emptyTokens[$this->tokens[$j]['code']]) === true; $j++);
1704-
if ($this->tokens[$j]['code'] === T_OPEN_PARENTHESIS) {
1705-
if (isset($this->tokens[$j]['parenthesis_closer']) === false) {
1706-
// Live coding/parse error, stop parsing.
1707-
break;
1708-
}
1709-
1710-
$i = $this->tokens[$j]['parenthesis_closer'];
1711-
continue;
1703+
if (isset($this->tokens[$i]['parenthesis_closer']) === false) {
1704+
// Live coding/parse error, stop parsing.
1705+
break;
17121706
}
1707+
1708+
$i = $this->tokens[$i]['parenthesis_closer'];
1709+
continue;
17131710
}
17141711

17151712
if ($this->tokens[$i]['code'] === T_NULLABLE) {
@@ -2067,6 +2064,7 @@ public function isReference($stackPtr)
20672064
if ($owner['code'] === T_FUNCTION
20682065
|| $owner['code'] === T_CLOSURE
20692066
|| $owner['code'] === T_FN
2067+
|| $owner['code'] === T_USE
20702068
) {
20712069
$params = $this->getMethodParameters($this->tokens[$lastBracket]['parenthesis_owner']);
20722070
foreach ($params as $param) {
@@ -2076,19 +2074,6 @@ public function isReference($stackPtr)
20762074
}
20772075
}
20782076
}//end if
2079-
} else {
2080-
$prev = false;
2081-
for ($t = ($this->tokens[$lastBracket]['parenthesis_opener'] - 1); $t >= 0; $t--) {
2082-
if ($this->tokens[$t]['code'] !== T_WHITESPACE) {
2083-
$prev = $t;
2084-
break;
2085-
}
2086-
}
2087-
2088-
if ($prev !== false && $this->tokens[$prev]['code'] === T_USE) {
2089-
// Closure use by reference.
2090-
return true;
2091-
}
20922077
}//end if
20932078
}//end if
20942079

src/Standards/Generic/Sniffs/Functions/OpeningFunctionBraceBsdAllmanSniff.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,8 @@ public function process(File $phpcsFile, $stackPtr)
7575
$closeBracket = $tokens[$stackPtr]['parenthesis_closer'];
7676
if ($tokens[$stackPtr]['code'] === T_CLOSURE) {
7777
$use = $phpcsFile->findNext(T_USE, ($closeBracket + 1), $tokens[$stackPtr]['scope_opener']);
78-
if ($use !== false) {
79-
$openBracket = $phpcsFile->findNext(T_OPEN_PARENTHESIS, ($use + 1));
80-
$closeBracket = $tokens[$openBracket]['parenthesis_closer'];
78+
if ($use !== false && isset($tokens[$use]['parenthesis_closer']) === true) {
79+
$closeBracket = $tokens[$use]['parenthesis_closer'];
8180
}
8281
}
8382

src/Standards/Generic/Sniffs/WhiteSpace/ArbitraryParenthesesSpacingSniff.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ public function register()
5757
$this->ignoreTokens[T_CLOSE_SQUARE_BRACKET] = T_CLOSE_SQUARE_BRACKET;
5858
$this->ignoreTokens[T_CLOSE_SHORT_ARRAY] = T_CLOSE_SHORT_ARRAY;
5959

60-
$this->ignoreTokens[T_USE] = T_USE;
6160
$this->ignoreTokens[T_THROW] = T_THROW;
6261
$this->ignoreTokens[T_YIELD] = T_YIELD;
6362
$this->ignoreTokens[T_YIELD_FROM] = T_YIELD_FROM;

src/Standards/PEAR/Sniffs/Functions/FunctionDeclarationSniff.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,7 @@ public function isMultiLineDeclaration($phpcsFile, $stackPtr, $openBracket, $tok
223223
if ($use !== false) {
224224
// If the opening and closing parenthesis of the use statement
225225
// are also on the same line, this is a single line declaration.
226-
$open = $phpcsFile->findNext(T_OPEN_PARENTHESIS, ($use + 1));
227-
$close = $tokens[$open]['parenthesis_closer'];
228-
if ($tokens[$open]['line'] !== $tokens[$close]['line']) {
226+
if ($tokens[$tokens[$use]['parenthesis_opener']]['line'] !== $tokens[$tokens[$use]['parenthesis_closer']]['line']) {
229227
return true;
230228
}
231229
}
@@ -281,9 +279,8 @@ public function processMultiLineDeclaration($phpcsFile, $stackPtr, $tokens)
281279
$closeBracket = $tokens[$stackPtr]['parenthesis_closer'];
282280
if ($tokens[$stackPtr]['code'] === T_CLOSURE) {
283281
$use = $phpcsFile->findNext(T_USE, ($closeBracket + 1), $tokens[$stackPtr]['scope_opener']);
284-
if ($use !== false) {
285-
$open = $phpcsFile->findNext(T_OPEN_PARENTHESIS, ($use + 1));
286-
$closeBracket = $tokens[$open]['parenthesis_closer'];
282+
if ($use !== false && isset($tokens[$use]['parenthesis_closer']) === true) {
283+
$closeBracket = $tokens[$use]['parenthesis_closer'];
287284
}
288285
}
289286

@@ -420,9 +417,8 @@ public function processArgumentList($phpcsFile, $stackPtr, $indent, $type='funct
420417
// of the USE statement.
421418
if ($tokens[$stackPtr]['code'] === T_CLOSURE) {
422419
$use = $phpcsFile->findNext(T_USE, ($closeBracket + 1), $tokens[$stackPtr]['scope_opener']);
423-
if ($use !== false) {
424-
$open = $phpcsFile->findNext(T_OPEN_PARENTHESIS, ($use + 1));
425-
$closeBracket = $tokens[$open]['parenthesis_closer'];
420+
if ($use !== false && isset($tokens[$use]['parenthesis_closer']) === true) {
421+
$closeBracket = $tokens[$use]['parenthesis_closer'];
426422

427423
$prev = $phpcsFile->findPrevious(
428424
T_WHITESPACE,

src/Standards/PSR12/Sniffs/Files/ImportStatementSniff.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ public function process(File $phpcsFile, $stackPtr)
4343
$tokens = $phpcsFile->getTokens();
4444

4545
// Make sure this is not a closure USE group.
46-
$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
47-
if ($tokens[$next]['code'] === T_OPEN_PARENTHESIS) {
46+
if (isset($tokens[$stackPtr]['parenthesis_owner']) === true) {
4847
return;
4948
}
5049

@@ -53,6 +52,7 @@ public function process(File $phpcsFile, $stackPtr)
5352
return;
5453
}
5554

55+
$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
5656
if ($tokens[$next]['code'] === T_STRING
5757
&& (strtolower($tokens[$next]['content']) === 'function'
5858
|| strtolower($tokens[$next]['content']) === 'const')

src/Standards/PSR2/Sniffs/Namespaces/UseDeclarationSniff.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,14 @@ private function shouldIgnoreUse($phpcsFile, $stackPtr)
278278
{
279279
$tokens = $phpcsFile->getTokens();
280280

281-
// Ignore USE keywords inside closures and during live coding.
281+
// Ignore USE keywords for closures.
282+
if (isset($tokens[$stackPtr]['parenthesis_owner']) === true) {
283+
return true;
284+
}
285+
286+
// Ignore USE keywords during live coding.
282287
$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
283-
if ($next === false || $tokens[$next]['code'] === T_OPEN_PARENTHESIS) {
288+
if ($next === false) {
284289
return true;
285290
}
286291

src/Standards/Squiz/Sniffs/Functions/FunctionDeclarationArgumentSpacingSniff.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,8 @@ public function process(File $phpcsFile, $stackPtr)
8484

8585
if ($tokens[$stackPtr]['code'] === T_CLOSURE) {
8686
$use = $phpcsFile->findNext(T_USE, ($tokens[$stackPtr]['parenthesis_closer'] + 1), $tokens[$stackPtr]['scope_opener']);
87-
if ($use !== false) {
88-
$openBracket = $phpcsFile->findNext(T_OPEN_PARENTHESIS, ($use + 1), null);
89-
$this->processBracket($phpcsFile, $openBracket);
87+
if ($use !== false && isset($tokens[$use]['parenthesis_opener']) === true) {
88+
$this->processBracket($phpcsFile, $tokens[$use]['parenthesis_opener']);
9089
}
9190
}
9291

@@ -108,13 +107,8 @@ public function processBracket($phpcsFile, $openBracket)
108107
$closeBracket = $tokens[$openBracket]['parenthesis_closer'];
109108
$multiLine = ($tokens[$openBracket]['line'] !== $tokens[$closeBracket]['line']);
110109

111-
if (isset($tokens[$openBracket]['parenthesis_owner']) === true) {
112-
$stackPtr = $tokens[$openBracket]['parenthesis_owner'];
113-
} else {
114-
$stackPtr = $phpcsFile->findPrevious(T_USE, ($openBracket - 1));
115-
}
116-
117-
$params = $phpcsFile->getMethodParameters($stackPtr);
110+
$stackPtr = $tokens[$openBracket]['parenthesis_owner'];
111+
$params = $phpcsFile->getMethodParameters($stackPtr);
118112

119113
if (empty($params) === true) {
120114
// Check spacing around parenthesis.

src/Standards/Squiz/Sniffs/Functions/MultiLineFunctionDeclarationSniff.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,8 @@ public function isMultiLineDeclaration($phpcsFile, $stackPtr, $openBracket, $tok
3636
// Closures may use the USE keyword and so be multi-line in this way.
3737
if ($tokens[$stackPtr]['code'] === T_CLOSURE) {
3838
$use = $phpcsFile->findNext(T_USE, ($tokens[$openBracket]['parenthesis_closer'] + 1), $tokens[$stackPtr]['scope_opener']);
39-
if ($use !== false) {
40-
$open = $phpcsFile->findNext(T_OPEN_PARENTHESIS, ($use + 1));
41-
if ($open !== false) {
42-
$bracketsToCheck[$use] = $open;
43-
}
39+
if ($use !== false && isset($tokens[$use]['parenthesis_opener']) === true) {
40+
$bracketsToCheck[$use] = $tokens[$use]['parenthesis_opener'];
4441
}
4542
}
4643

@@ -155,12 +152,11 @@ public function processMultiLineDeclaration($phpcsFile, $stackPtr, $tokens)
155152
}
156153

157154
$use = $phpcsFile->findNext(T_USE, ($tokens[$stackPtr]['parenthesis_closer'] + 1), $tokens[$stackPtr]['scope_opener']);
158-
if ($use === false) {
155+
if ($use === false || isset($tokens[$use]['parenthesis_opener']) === false) {
159156
return;
160157
}
161158

162-
$openBracket = $phpcsFile->findNext(T_OPEN_PARENTHESIS, ($use + 1), null);
163-
$this->processBracket($phpcsFile, $openBracket, $tokens, 'use');
159+
$this->processBracket($phpcsFile, $tokens[$use]['parenthesis_opener'], $tokens, 'use');
164160

165161
}//end processMultiLineDeclaration()
166162

0 commit comments

Comments
 (0)