-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
366 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,10 +21,12 @@ | |
use PhpCsFixer\Preg; | ||
use PhpCsFixer\Tokenizer\Analyzer\Analysis\NamespaceAnalysis; | ||
use PhpCsFixer\Tokenizer\Analyzer\Analysis\NamespaceUseAnalysis; | ||
use PhpCsFixer\Tokenizer\Analyzer\GotoLabelAnalyzer; | ||
use PhpCsFixer\Tokenizer\Analyzer\NamespacesAnalyzer; | ||
use PhpCsFixer\Tokenizer\Analyzer\NamespaceUsesAnalyzer; | ||
use PhpCsFixer\Tokenizer\Token; | ||
use PhpCsFixer\Tokenizer\Tokens; | ||
use PhpCsFixer\Tokenizer\TokensAnalyzer; | ||
|
||
/** | ||
* @author Dariusz Rumiński <[email protected]> | ||
|
@@ -73,24 +75,18 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void | |
} | ||
|
||
foreach ((new NamespacesAnalyzer())->getDeclarations($tokens) as $namespace) { | ||
$currentNamespaceUseDeclarations = array_filter( | ||
$useDeclarations, | ||
static function (NamespaceUseAnalysis $useDeclaration) use ($namespace) { | ||
return | ||
$useDeclaration->getStartIndex() >= $namespace->getScopeStartIndex() | ||
&& $useDeclaration->getEndIndex() <= $namespace->getScopeEndIndex() | ||
; | ||
} | ||
); | ||
|
||
$usagesSearchIgnoredIndexes = []; | ||
$currentNamespaceUseDeclarations = []; | ||
$currentNamespaceUseDeclarationIndexes = []; | ||
|
||
foreach ($currentNamespaceUseDeclarations as $useDeclaration) { | ||
$usagesSearchIgnoredIndexes[$useDeclaration->getStartIndex()] = $useDeclaration->getEndIndex(); | ||
foreach ($useDeclarations as $useDeclaration) { | ||
if ($useDeclaration->getStartIndex() >= $namespace->getScopeStartIndex() && $useDeclaration->getEndIndex() <= $namespace->getScopeEndIndex()) { | ||
$currentNamespaceUseDeclarations[] = $useDeclaration; | ||
$currentNamespaceUseDeclarationIndexes[$useDeclaration->getStartIndex()] = $useDeclaration->getEndIndex(); | ||
} | ||
} | ||
|
||
foreach ($currentNamespaceUseDeclarations as $useDeclaration) { | ||
if (!$this->isImportUsed($tokens, $namespace, $usagesSearchIgnoredIndexes, $useDeclaration->getShortName())) { | ||
if (!$this->isImportUsed($tokens, $namespace, $useDeclaration, $currentNamespaceUseDeclarationIndexes)) { | ||
$this->removeUseDeclaration($tokens, $useDeclaration); | ||
} | ||
} | ||
|
@@ -100,10 +96,19 @@ static function (NamespaceUseAnalysis $useDeclaration) use ($namespace) { | |
} | ||
|
||
/** | ||
* @param array<int, int> $ignoredIndexes | ||
* @param array<int, int> $ignoredIndexes indexes of the use statements themselves that should not be checked as being "used" | ||
*/ | ||
private function isImportUsed(Tokens $tokens, NamespaceAnalysis $namespace, array $ignoredIndexes, string $shortName): bool | ||
private function isImportUsed(Tokens $tokens, NamespaceAnalysis $namespace, NamespaceUseAnalysis $import, array $ignoredIndexes): bool | ||
{ | ||
$analyzer = new TokensAnalyzer($tokens); | ||
$gotoLabelAnalyzer = new GotoLabelAnalyzer(); | ||
|
||
$tokensNotBeforeFunctionCall = [T_NEW]; | ||
// @TODO: drop condition when PHP 8.0+ is required | ||
if (\defined('T_ATTRIBUTE')) { | ||
$tokensNotBeforeFunctionCall[] = T_ATTRIBUTE; | ||
} | ||
|
||
$namespaceEndIndex = $namespace->getScopeEndIndex(); | ||
for ($index = $namespace->getScopeStartIndex(); $index <= $namespaceEndIndex; ++$index) { | ||
if (isset($ignoredIndexes[$index])) { | ||
|
@@ -115,6 +120,10 @@ private function isImportUsed(Tokens $tokens, NamespaceAnalysis $namespace, arra | |
$token = $tokens[$index]; | ||
|
||
if ($token->isGivenKind(T_STRING)) { | ||
if (0 !== strcasecmp($import->getShortName(), $token->getContent())) { | ||
continue; | ||
} | ||
|
||
$prevMeaningfulToken = $tokens[$tokens->getPrevMeaningfulToken($index)]; | ||
|
||
if ($prevMeaningfulToken->isGivenKind(T_NAMESPACE)) { | ||
|
@@ -124,10 +133,29 @@ private function isImportUsed(Tokens $tokens, NamespaceAnalysis $namespace, arra | |
} | ||
|
||
if ( | ||
0 === strcasecmp($shortName, $token->getContent()) | ||
&& !$prevMeaningfulToken->isGivenKind([T_NS_SEPARATOR, T_CONST, T_DOUBLE_COLON]) | ||
&& !$prevMeaningfulToken->isObjectOperator() | ||
$prevMeaningfulToken->isGivenKind([T_NS_SEPARATOR, T_FUNCTION, T_CONST, T_DOUBLE_COLON]) | ||
|| $prevMeaningfulToken->isObjectOperator() | ||
) { | ||
continue; | ||
} | ||
|
||
$nextMeaningfulIndex = $tokens->getNextMeaningfulToken($index); | ||
|
||
if ($gotoLabelAnalyzer->belongsToGoToLabel($tokens, $nextMeaningfulIndex)) { | ||
continue; | ||
} | ||
|
||
$nextMeaningfulToken = $tokens[$nextMeaningfulIndex]; | ||
|
||
if ($analyzer->isConstantInvocation($index)) { | ||
$type = NamespaceUseAnalysis::TYPE_CONSTANT; | ||
} elseif ($nextMeaningfulToken->equals('(') && !$prevMeaningfulToken->isGivenKind($tokensNotBeforeFunctionCall)) { | ||
$type = NamespaceUseAnalysis::TYPE_FUNCTION; | ||
} else { | ||
$type = NamespaceUseAnalysis::TYPE_CLASS; | ||
} | ||
|
||
if ($import->getType() === $type) { | ||
return true; | ||
} | ||
|
||
|
@@ -136,7 +164,7 @@ private function isImportUsed(Tokens $tokens, NamespaceAnalysis $namespace, arra | |
|
||
if ($token->isComment() | ||
&& Preg::match( | ||
'/(?<![[:alnum:]\$])(?<!\\\\)'.$shortName.'(?![[:alnum:]])/i', | ||
'/(?<![[:alnum:]\$])(?<!\\\\)'.$import->getShortName().'(?![[:alnum:]])/i', | ||
$token->getContent() | ||
) | ||
) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.