From b16c2f01ba17858e7271d1361162bfd606380b65 Mon Sep 17 00:00:00 2001 From: Christophe Coevoet Date: Thu, 5 Jul 2018 15:04:59 +0200 Subject: [PATCH] Use the NamespacesAnalyzer in the NativeFunctionInvocationFixer --- .../NativeFunctionInvocationFixer.php | 60 ++++--------------- 1 file changed, 11 insertions(+), 49 deletions(-) diff --git a/src/Fixer/FunctionNotation/NativeFunctionInvocationFixer.php b/src/Fixer/FunctionNotation/NativeFunctionInvocationFixer.php index de0afb215a4..2e4500977e6 100644 --- a/src/Fixer/FunctionNotation/NativeFunctionInvocationFixer.php +++ b/src/Fixer/FunctionNotation/NativeFunctionInvocationFixer.php @@ -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; @@ -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()); } } @@ -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 *