Skip to content

Commit

Permalink
Use the NamespacesAnalyzer in the NativeFunctionInvocationFixer
Browse files Browse the repository at this point in the history
  • Loading branch information
stof committed Jul 5, 2018
1 parent 4cee75b commit 2a55582
Showing 1 changed file with 11 additions and 49 deletions.
60 changes: 11 additions & 49 deletions src/Fixer/FunctionNotation/NativeFunctionInvocationFixer.php
Expand Up @@ -18,6 +18,8 @@
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\Tokenizer\Analyzer\Analysis\NamespaceAnalysis;
use PhpCsFixer\Tokenizer\Analyzer\NamespacesAnalyzer;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
Expand Down Expand Up @@ -184,9 +186,16 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
return;
}

$namespaces = (new NamespacesAnalyzer())->getDeclarations($tokens);

// 'scope' is 'namespaced' here
foreach (\array_reverse($this->getUserDefinedNamespaces($tokens)) as $namespace) {
$this->fixFunctionCalls($tokens, $this->functionFilter, $namespace['open'], $namespace['close']);
/** @var NamespaceAnalysis $namespace */
foreach (\array_reverse($namespaces) as $namespace) {
if ('' === $namespace->getFullName()) {
continue;
}

$this->fixFunctionCalls($tokens, $this->functionFilter, $namespace->getScopeStartIndex(), $namespace->getScopeEndIndex());
}
}

Expand Down Expand Up @@ -392,53 +401,6 @@ private function getAllInternalFunctionsNormalized()
return $this->normalizeFunctionNames(\get_defined_functions()['internal']);
}

/**
* Returns array<'open'|'close', int>[].
*
* @param Tokens $tokens
*
* @return array
*/
private function getUserDefinedNamespaces(Tokens $tokens)
{
$namespaces = [];
for ($index = 1, $count = \count($tokens); $index < $count; ++$index) {
if (!$tokens[$index]->isGivenKind(T_NAMESPACE)) {
continue;
}

$index = $tokens->getNextMeaningfulToken($index);
if ($tokens[$index]->equals('{')) { // global namespace
$index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $index);

continue;
}

while (!$tokens[++$index]->equalsAny(['{', ';', [T_CLOSE_TAG]])) {
// no-op
}

if ($tokens[$index]->equals('{')) {
// namespace ends at block end of `{`
$namespaces[] = ['open' => $index, 'close' => $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $index)];

continue;
}

// namespace ends at next T_NAMESPACE or EOF
$close = $tokens->getNextTokenOfKind($index, [[T_NAMESPACE]], false);
if (null === $close) {
$namespaces[] = ['open' => $index, 'close' => \count($tokens) - 1];

break;
}

$namespaces[] = ['open' => $index, 'close' => $close];
}

return $namespaces;
}

/**
* @param string[] $functionNames
*
Expand Down

0 comments on commit 2a55582

Please sign in to comment.