From 8321c26f58f49e89e7ad67ef16916aa206dd2e51 Mon Sep 17 00:00:00 2001 From: Christophe Coevoet Date: Thu, 5 Jul 2018 14:58:34 +0200 Subject: [PATCH] Optimize the namespaces analyzer Instead of looping on all tokens, even inside the namespace it identified, the analyzer now continues the analysis after the end of the identified namespace, thanks to the fact that namespaces cannot be nested. --- src/Tokenizer/Analyzer/NamespacesAnalyzer.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Tokenizer/Analyzer/NamespacesAnalyzer.php b/src/Tokenizer/Analyzer/NamespacesAnalyzer.php index 6e2fa139828..75302b2d859 100644 --- a/src/Tokenizer/Analyzer/NamespacesAnalyzer.php +++ b/src/Tokenizer/Analyzer/NamespacesAnalyzer.php @@ -29,7 +29,9 @@ public function getDeclarations(Tokens $tokens) { $namespaces = []; - foreach ($tokens as $index => $token) { + for ($index = 1, $count = \count($tokens); $index < $count; ++$index) { + $token = $tokens[$index]; + if (!$token->isGivenKind(T_NAMESPACE)) { continue; } @@ -57,6 +59,9 @@ public function getDeclarations(Tokens $tokens) $index, $scopeEndIndex ); + + // Continue the analysis after the end of this namespace to find the next one + $index = $scopeEndIndex; } if (0 === count($namespaces)) {