Skip to content

Commit

Permalink
minor #3878 NativeFunctionInvocationFixer - use the NamespacesAnalyze…
Browse files Browse the repository at this point in the history
…r to remove duplicated code (stof)

This PR was merged into the 2.12 branch.

Discussion
----------

NativeFunctionInvocationFixer - use the NamespacesAnalyzer to remove duplicated code

Instead of maintaining 2 different analyzers to find the beginning and the end of a namespace block, this reuses the existing NamespacesAnalyzer.

See #3877 which optimizes it based on existing optimization in the implementation I'm deleting here.

Commits
-------

b16c2f0 Use the NamespacesAnalyzer in the NativeFunctionInvocationFixer
  • Loading branch information
keradus committed Jul 6, 2018
2 parents bf1654b + b16c2f0 commit 922ba6e
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 922ba6e

Please sign in to comment.