Skip to content

Commit

Permalink
minor #3877 NamespacesAnalyzer - Optimize performance (stof)
Browse files Browse the repository at this point in the history
This PR was merged into the 2.12 branch.

Discussion
----------

NamespacesAnalyzer - Optimize performance

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.

When implementing #3876, I compared the NamespacesAnalyzer implementation with `\PhpCsFixer\Fixer\FunctionNotation\NativeFunctionInvocationFixer::getUserDefinedNamespaces`. The NamespacesAnalyzer also identifies the namespace name instead of skipping over it (and returns the global namespace too). But I found out that NativeFunctionInvocationFixer was more optimized, as it was not re-analyzing all tokens inside a namespace. This makes NamespacesAnalyzer implement the same optimization.

Commits
-------

8321c26 Optimize the namespaces analyzer
  • Loading branch information
keradus committed Jul 6, 2018
2 parents 6856b52 + 8321c26 commit bf1654b
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Tokenizer/Analyzer/NamespacesAnalyzer.php
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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)) {
Expand Down

0 comments on commit bf1654b

Please sign in to comment.