Skip to content

Commit 7b98484

Browse files
committed
Generic/ConstructorName: minor efficiency tweak
As things were, the `$startIndex` would be the `function` keyword in the function declaration, which means that the complete declaration (parameters, defaults etc) would be walked, while we only really want to look _inside_ the function body. Fixed by starting the `while` loop on the `scope_opener` instead. Additionally, while the `$startIndex` would be moved forward on each loop, it would still examine one token too much (`scope_opener` cannot be a `T_DOUBLE_COLON`, `T_STRING` after `T_DOUBLE_COLON` cannot be a `T_DOUBLE_COLON`). Also fixed now.
1 parent a3a67d8 commit 7b98484

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

src/Standards/Generic/Sniffs/NamingConventions/ConstructorNameSniff.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop
9191
}
9292

9393
// Stop if the constructor doesn't have a body, like when it is abstract.
94-
if (isset($tokens[$stackPtr]['scope_closer']) === false) {
94+
if (isset($tokens[$stackPtr]['scope_opener'], $tokens[$stackPtr]['scope_closer']) === false) {
9595
return;
9696
}
9797

@@ -103,8 +103,8 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop
103103
$parentClassNameLc = strtolower($parentClassName);
104104

105105
$endFunctionIndex = $tokens[$stackPtr]['scope_closer'];
106-
$startIndex = $stackPtr;
107-
while (($doubleColonIndex = $phpcsFile->findNext(T_DOUBLE_COLON, $startIndex, $endFunctionIndex)) !== false) {
106+
$startIndex = $tokens[$stackPtr]['scope_opener'];
107+
while (($doubleColonIndex = $phpcsFile->findNext(T_DOUBLE_COLON, ($startIndex + 1), $endFunctionIndex)) !== false) {
108108
$nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($doubleColonIndex + 1), null, true);
109109
if ($tokens[$nextNonEmpty]['code'] !== T_STRING
110110
|| strtolower($tokens[$nextNonEmpty]['content']) !== $parentClassNameLc

0 commit comments

Comments
 (0)