Total Complexity | 48 |
Total Lines | 249 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like NoUnusedImportsFixer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use NoUnusedImportsFixer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
34 | final class NoUnusedImportsFixer extends AbstractFixer |
||
35 | { |
||
36 | /** |
||
37 | * {@inheritdoc} |
||
38 | */ |
||
39 | public function getDefinition(): FixerDefinitionInterface |
||
40 | { |
||
41 | return new FixerDefinition( |
||
42 | 'Unused `use` statements must be removed.', |
||
43 | [new CodeSample("<?php\nuse \\DateTime;\nuse \\Exception;\n\nnew DateTime();\n")] |
||
44 | ); |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * {@inheritdoc} |
||
49 | * |
||
50 | * Must run before BlankLineAfterNamespaceFixer, NoExtraBlankLinesFixer, NoLeadingImportSlashFixer, SingleLineAfterImportsFixer. |
||
51 | * Must run after ClassKeywordRemoveFixer, GlobalNamespaceImportFixer, PhpUnitFqcnAnnotationFixer, SingleImportPerStatementFixer. |
||
52 | */ |
||
53 | public function getPriority(): int |
||
54 | { |
||
55 | return -10; |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * {@inheritdoc} |
||
60 | */ |
||
61 | public function isCandidate(Tokens $tokens): bool |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * {@inheritdoc} |
||
68 | */ |
||
69 | protected function applyFix(\SplFileInfo $file, Tokens $tokens): void |
||
70 | { |
||
71 | $useDeclarations = (new NamespaceUsesAnalyzer())->getDeclarationsFromTokens($tokens); |
||
72 | |||
73 | if (0 === \count($useDeclarations)) { |
||
74 | return; |
||
75 | } |
||
76 | |||
77 | foreach ((new NamespacesAnalyzer())->getDeclarations($tokens) as $namespace) { |
||
78 | $currentNamespaceUseDeclarations = []; |
||
79 | $currentNamespaceUseDeclarationIndexes = []; |
||
80 | |||
81 | foreach ($useDeclarations as $useDeclaration) { |
||
82 | if ($useDeclaration->getStartIndex() >= $namespace->getScopeStartIndex() && $useDeclaration->getEndIndex() <= $namespace->getScopeEndIndex()) { |
||
83 | $currentNamespaceUseDeclarations[] = $useDeclaration; |
||
84 | $currentNamespaceUseDeclarationIndexes[$useDeclaration->getStartIndex()] = $useDeclaration->getEndIndex(); |
||
85 | } |
||
86 | } |
||
87 | |||
88 | foreach ($currentNamespaceUseDeclarations as $useDeclaration) { |
||
89 | if (!$this->isImportUsed($tokens, $namespace, $useDeclaration, $currentNamespaceUseDeclarationIndexes)) { |
||
90 | $this->removeUseDeclaration($tokens, $useDeclaration); |
||
91 | } |
||
92 | } |
||
93 | |||
94 | $this->removeUsesInSameNamespace($tokens, $currentNamespaceUseDeclarations, $namespace); |
||
95 | } |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * @param array<int, int> $ignoredIndexes indexes of the use statements themselves that should not be checked as being "used" |
||
100 | */ |
||
101 | private function isImportUsed(Tokens $tokens, NamespaceAnalysis $namespace, NamespaceUseAnalysis $import, array $ignoredIndexes): bool |
||
102 | { |
||
103 | $analyzer = new TokensAnalyzer($tokens); |
||
104 | $gotoLabelAnalyzer = new GotoLabelAnalyzer(); |
||
105 | |||
106 | $tokensNotBeforeFunctionCall = [T_NEW]; |
||
107 | // @TODO: drop condition when PHP 8.0+ is required |
||
108 | if (\defined('T_ATTRIBUTE')) { |
||
109 | $tokensNotBeforeFunctionCall[] = T_ATTRIBUTE; |
||
110 | } |
||
111 | |||
112 | $namespaceEndIndex = $namespace->getScopeEndIndex(); |
||
113 | for ($index = $namespace->getScopeStartIndex(); $index <= $namespaceEndIndex; ++$index) { |
||
114 | if (isset($ignoredIndexes[$index])) { |
||
115 | $index = $ignoredIndexes[$index]; |
||
116 | |||
117 | continue; |
||
118 | } |
||
119 | |||
120 | $token = $tokens[$index]; |
||
121 | |||
122 | if ($token->isGivenKind(T_STRING)) { |
||
123 | if (0 !== strcasecmp($import->getShortName(), $token->getContent())) { |
||
124 | continue; |
||
125 | } |
||
126 | |||
127 | $prevMeaningfulToken = $tokens[$tokens->getPrevMeaningfulToken($index)]; |
||
128 | |||
129 | if ($prevMeaningfulToken->isGivenKind(T_NAMESPACE)) { |
||
130 | $index = $tokens->getNextTokenOfKind($index, [';', '{', [T_CLOSE_TAG]]); |
||
131 | |||
132 | continue; |
||
133 | } |
||
134 | |||
135 | if ( |
||
136 | $prevMeaningfulToken->isGivenKind([T_NS_SEPARATOR, T_FUNCTION, T_CONST, T_DOUBLE_COLON]) |
||
137 | || $prevMeaningfulToken->isObjectOperator() |
||
138 | ) { |
||
139 | continue; |
||
140 | } |
||
141 | |||
142 | $nextMeaningfulIndex = $tokens->getNextMeaningfulToken($index); |
||
143 | |||
144 | if ($gotoLabelAnalyzer->belongsToGoToLabel($tokens, $nextMeaningfulIndex)) { |
||
|
|||
145 | continue; |
||
146 | } |
||
147 | |||
148 | $nextMeaningfulToken = $tokens[$nextMeaningfulIndex]; |
||
149 | |||
150 | if ($analyzer->isConstantInvocation($index)) { |
||
151 | $type = NamespaceUseAnalysis::TYPE_CONSTANT; |
||
152 | } elseif ($nextMeaningfulToken->equals('(') && !$prevMeaningfulToken->isGivenKind($tokensNotBeforeFunctionCall)) { |
||
153 | $type = NamespaceUseAnalysis::TYPE_FUNCTION; |
||
154 | } else { |
||
155 | $type = NamespaceUseAnalysis::TYPE_CLASS; |
||
156 | } |
||
157 | |||
158 | if ($import->getType() === $type) { |
||
159 | return true; |
||
160 | } |
||
161 | |||
162 | continue; |
||
163 | } |
||
164 | |||
165 | if ($token->isComment() |
||
166 | && Preg::match( |
||
167 | '/(?<![[:alnum:]\$])(?<!\\\\)'.$import->getShortName().'(?![[:alnum:]])/i', |
||
168 | $token->getContent() |
||
169 | ) |
||
170 | ) { |
||
171 | return true; |
||
172 | } |
||
173 | } |
||
174 | |||
175 | return false; |
||
176 | } |
||
177 | |||
178 | private function removeUseDeclaration(Tokens $tokens, NamespaceUseAnalysis $useDeclaration): void |
||
260 | } |
||
261 | } |
||
262 | |||
263 | private function removeUsesInSameNamespace(Tokens $tokens, array $useDeclarations, NamespaceAnalysis $namespaceDeclaration): void |
||
283 | } |
||
284 | } |
||
287 |