Total Complexity | 81 |
Total Lines | 399 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like ClassDefinitionFixer 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 ClassDefinitionFixer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
37 | final class ClassDefinitionFixer extends AbstractFixer implements ConfigurableFixerInterface, WhitespacesAwareFixerInterface |
||
38 | { |
||
39 | /** |
||
40 | * {@inheritdoc} |
||
41 | */ |
||
42 | public function getDefinition(): FixerDefinitionInterface |
||
43 | { |
||
44 | return new FixerDefinition( |
||
45 | 'Whitespace around the keywords of a class, trait or interfaces definition should be one space.', |
||
46 | [ |
||
47 | new CodeSample( |
||
48 | '<?php |
||
49 | |||
50 | class Foo extends Bar implements Baz, BarBaz |
||
51 | { |
||
52 | } |
||
53 | |||
54 | final class Foo extends Bar implements Baz, BarBaz |
||
55 | { |
||
56 | } |
||
57 | |||
58 | trait Foo |
||
59 | { |
||
60 | } |
||
61 | ' |
||
62 | ), |
||
63 | new VersionSpecificCodeSample( |
||
64 | '<?php |
||
65 | |||
66 | $foo = new class extends Bar implements Baz, BarBaz {}; |
||
67 | ', |
||
68 | new VersionSpecification(70100) |
||
69 | ), |
||
70 | new CodeSample( |
||
71 | '<?php |
||
72 | |||
73 | class Foo |
||
74 | extends Bar |
||
75 | implements Baz, BarBaz |
||
76 | {} |
||
77 | ', |
||
78 | ['single_line' => true] |
||
79 | ), |
||
80 | new CodeSample( |
||
81 | '<?php |
||
82 | |||
83 | class Foo |
||
84 | extends Bar |
||
85 | implements Baz |
||
86 | {} |
||
87 | ', |
||
88 | ['single_item_single_line' => true] |
||
89 | ), |
||
90 | new CodeSample( |
||
91 | '<?php |
||
92 | |||
93 | interface Bar extends |
||
94 | Bar, BarBaz, FooBarBaz |
||
95 | {} |
||
96 | ', |
||
97 | ['multi_line_extends_each_single_line' => true] |
||
98 | ), |
||
99 | new CodeSample( |
||
100 | '<?php |
||
101 | $foo = new class(){}; |
||
102 | ', |
||
103 | ['space_before_parenthesis' => true] |
||
104 | ), |
||
105 | ] |
||
106 | ); |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * {@inheritdoc} |
||
111 | * |
||
112 | * Must run before BracesFixer. |
||
113 | */ |
||
114 | public function getPriority(): int |
||
115 | { |
||
116 | return 36; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * {@inheritdoc} |
||
121 | */ |
||
122 | public function isCandidate(Tokens $tokens): bool |
||
123 | { |
||
124 | return $tokens->isAnyTokenKindsFound(Token::getClassyTokenKinds()); |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * {@inheritdoc} |
||
129 | */ |
||
130 | protected function applyFix(\SplFileInfo $file, Tokens $tokens): void |
||
131 | { |
||
132 | // -4, one for count to index, 3 because min. of tokens for a classy location. |
||
133 | for ($index = $tokens->getSize() - 4; $index > 0; --$index) { |
||
134 | if ($tokens[$index]->isClassy()) { |
||
135 | $this->fixClassyDefinition($tokens, $index); |
||
136 | } |
||
137 | } |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * {@inheritdoc} |
||
142 | */ |
||
143 | protected function createConfigurationDefinition(): FixerConfigurationResolverInterface |
||
144 | { |
||
145 | return new FixerConfigurationResolver([ |
||
146 | (new FixerOptionBuilder('multi_line_extends_each_single_line', 'Whether definitions should be multiline.')) |
||
147 | ->setAllowedTypes(['bool']) |
||
148 | ->setDefault(false) |
||
149 | ->getOption(), |
||
150 | (new FixerOptionBuilder('single_item_single_line', 'Whether definitions should be single line when including a single item.')) |
||
151 | ->setAllowedTypes(['bool']) |
||
152 | ->setDefault(false) |
||
153 | ->getOption(), |
||
154 | (new FixerOptionBuilder('single_line', 'Whether definitions should be single line.')) |
||
155 | ->setAllowedTypes(['bool']) |
||
156 | ->setDefault(false) |
||
157 | ->getOption(), |
||
158 | (new FixerOptionBuilder('space_before_parenthesis', 'Whether there should be a single space after the parenthesis of anonymous class (PSR12) or not.')) |
||
159 | ->setAllowedTypes(['bool']) |
||
160 | ->setDefault(false) |
||
161 | ->getOption(), |
||
162 | ]); |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * @param int $classyIndex Class definition token start index |
||
167 | */ |
||
168 | private function fixClassyDefinition(Tokens $tokens, int $classyIndex): void |
||
169 | { |
||
170 | $classDefInfo = $this->getClassyDefinitionInfo($tokens, $classyIndex); |
||
171 | |||
172 | // PSR2 4.1 Lists of implements MAY be split across multiple lines, where each subsequent line is indented once. |
||
173 | // When doing so, the first item in the list MUST be on the next line, and there MUST be only one interface per line. |
||
174 | |||
175 | if (false !== $classDefInfo['implements']) { |
||
176 | $classDefInfo['implements'] = $this->fixClassyDefinitionImplements( |
||
177 | $tokens, |
||
178 | $classDefInfo['open'], |
||
|
|||
179 | $classDefInfo['implements'] |
||
180 | ); |
||
181 | } |
||
182 | |||
183 | if (false !== $classDefInfo['extends']) { |
||
184 | $classDefInfo['extends'] = $this->fixClassyDefinitionExtends( |
||
185 | $tokens, |
||
186 | false === $classDefInfo['implements'] ? $classDefInfo['open'] : $classDefInfo['implements']['start'], |
||
187 | $classDefInfo['extends'] |
||
188 | ); |
||
189 | } |
||
190 | |||
191 | // PSR2: class definition open curly brace must go on a new line. |
||
192 | // PSR12: anonymous class curly brace on same line if not multi line implements. |
||
193 | |||
194 | $classDefInfo['open'] = $this->fixClassyDefinitionOpenSpacing($tokens, $classDefInfo); |
||
195 | |||
196 | if ($classDefInfo['implements']) { |
||
197 | $end = $classDefInfo['implements']['start']; |
||
198 | } elseif ($classDefInfo['extends']) { |
||
199 | $end = $classDefInfo['extends']['start']; |
||
200 | } else { |
||
201 | $end = $tokens->getPrevNonWhitespace($classDefInfo['open']); |
||
202 | } |
||
203 | |||
204 | // 4.1 The extends and implements keywords MUST be declared on the same line as the class name. |
||
205 | $this->makeClassyDefinitionSingleLine($tokens, $classDefInfo['start'], $end); |
||
206 | } |
||
207 | |||
208 | private function fixClassyDefinitionExtends(Tokens $tokens, int $classOpenIndex, array $classExtendsInfo): array |
||
209 | { |
||
210 | $endIndex = $tokens->getPrevNonWhitespace($classOpenIndex); |
||
211 | |||
212 | if ($this->configuration['single_line'] || false === $classExtendsInfo['multiLine']) { |
||
213 | $this->makeClassyDefinitionSingleLine($tokens, $classExtendsInfo['start'], $endIndex); |
||
214 | $classExtendsInfo['multiLine'] = false; |
||
215 | } elseif ($this->configuration['single_item_single_line'] && 1 === $classExtendsInfo['numberOfExtends']) { |
||
216 | $this->makeClassyDefinitionSingleLine($tokens, $classExtendsInfo['start'], $endIndex); |
||
217 | $classExtendsInfo['multiLine'] = false; |
||
218 | } elseif ($this->configuration['multi_line_extends_each_single_line'] && $classExtendsInfo['multiLine']) { |
||
219 | $this->makeClassyInheritancePartMultiLine($tokens, $classExtendsInfo['start'], $endIndex); |
||
220 | $classExtendsInfo['multiLine'] = true; |
||
221 | } |
||
222 | |||
223 | return $classExtendsInfo; |
||
224 | } |
||
225 | |||
226 | private function fixClassyDefinitionImplements(Tokens $tokens, int $classOpenIndex, array $classImplementsInfo): array |
||
227 | { |
||
228 | $endIndex = $tokens->getPrevNonWhitespace($classOpenIndex); |
||
229 | |||
230 | if ($this->configuration['single_line'] || false === $classImplementsInfo['multiLine']) { |
||
231 | $this->makeClassyDefinitionSingleLine($tokens, $classImplementsInfo['start'], $endIndex); |
||
232 | $classImplementsInfo['multiLine'] = false; |
||
233 | } elseif ($this->configuration['single_item_single_line'] && 1 === $classImplementsInfo['numberOfImplements']) { |
||
234 | $this->makeClassyDefinitionSingleLine($tokens, $classImplementsInfo['start'], $endIndex); |
||
235 | $classImplementsInfo['multiLine'] = false; |
||
236 | } else { |
||
237 | $this->makeClassyInheritancePartMultiLine($tokens, $classImplementsInfo['start'], $endIndex); |
||
238 | $classImplementsInfo['multiLine'] = true; |
||
239 | } |
||
240 | |||
241 | return $classImplementsInfo; |
||
242 | } |
||
243 | |||
244 | private function fixClassyDefinitionOpenSpacing(Tokens $tokens, array $classDefInfo): int |
||
245 | { |
||
246 | if ($classDefInfo['anonymousClass']) { |
||
247 | if (false !== $classDefInfo['implements']) { |
||
248 | $spacing = $classDefInfo['implements']['multiLine'] ? $this->whitespacesConfig->getLineEnding() : ' '; |
||
249 | } elseif (false !== $classDefInfo['extends']) { |
||
250 | $spacing = $classDefInfo['extends']['multiLine'] ? $this->whitespacesConfig->getLineEnding() : ' '; |
||
251 | } else { |
||
252 | $spacing = ' '; |
||
253 | } |
||
254 | } else { |
||
255 | $spacing = $this->whitespacesConfig->getLineEnding(); |
||
256 | } |
||
257 | |||
258 | $openIndex = $tokens->getNextTokenOfKind($classDefInfo['classy'], ['{']); |
||
259 | |||
260 | if (' ' !== $spacing && false !== strpos($tokens[$openIndex - 1]->getContent(), "\n")) { |
||
261 | return $openIndex; |
||
262 | } |
||
263 | |||
264 | if ($tokens[$openIndex - 1]->isWhitespace()) { |
||
265 | if (' ' !== $spacing || !$tokens[$tokens->getPrevNonWhitespace($openIndex - 1)]->isComment()) { |
||
266 | $tokens[$openIndex - 1] = new Token([T_WHITESPACE, $spacing]); |
||
267 | } |
||
268 | |||
269 | return $openIndex; |
||
270 | } |
||
271 | |||
272 | $tokens->insertAt($openIndex, new Token([T_WHITESPACE, $spacing])); |
||
273 | |||
274 | return $openIndex + 1; |
||
275 | } |
||
276 | |||
277 | private function getClassyDefinitionInfo(Tokens $tokens, int $classyIndex): array |
||
278 | { |
||
279 | $openIndex = $tokens->getNextTokenOfKind($classyIndex, ['{']); |
||
280 | $extends = false; |
||
281 | $implements = false; |
||
282 | $anonymousClass = false; |
||
283 | |||
284 | if (!$tokens[$classyIndex]->isGivenKind(T_TRAIT)) { |
||
285 | $extends = $tokens->findGivenKind(T_EXTENDS, $classyIndex, $openIndex); |
||
286 | $extends = \count($extends) ? $this->getClassyInheritanceInfo($tokens, key($extends), 'numberOfExtends') : false; |
||
287 | |||
288 | if (!$tokens[$classyIndex]->isGivenKind(T_INTERFACE)) { |
||
289 | $implements = $tokens->findGivenKind(T_IMPLEMENTS, $classyIndex, $openIndex); |
||
290 | $implements = \count($implements) ? $this->getClassyInheritanceInfo($tokens, key($implements), 'numberOfImplements') : false; |
||
291 | $tokensAnalyzer = new TokensAnalyzer($tokens); |
||
292 | $anonymousClass = $tokensAnalyzer->isAnonymousClass($classyIndex); |
||
293 | } |
||
294 | } |
||
295 | |||
296 | if ($anonymousClass) { |
||
297 | $startIndex = $tokens->getPrevMeaningfulToken($classyIndex); // go to "new" for anonymous class |
||
298 | } else { |
||
299 | $prev = $tokens->getPrevMeaningfulToken($classyIndex); |
||
300 | $startIndex = $tokens[$prev]->isGivenKind([T_FINAL, T_ABSTRACT]) ? $prev : $classyIndex; |
||
301 | } |
||
302 | |||
303 | return [ |
||
304 | 'start' => $startIndex, |
||
305 | 'classy' => $classyIndex, |
||
306 | 'open' => $openIndex, |
||
307 | 'extends' => $extends, |
||
308 | 'implements' => $implements, |
||
309 | 'anonymousClass' => $anonymousClass, |
||
310 | ]; |
||
311 | } |
||
312 | |||
313 | private function getClassyInheritanceInfo(Tokens $tokens, int $startIndex, string $label): array |
||
314 | { |
||
315 | $implementsInfo = ['start' => $startIndex, $label => 1, 'multiLine' => false]; |
||
316 | ++$startIndex; |
||
317 | $endIndex = $tokens->getNextTokenOfKind($startIndex, ['{', [T_IMPLEMENTS], [T_EXTENDS]]); |
||
318 | $endIndex = $tokens[$endIndex]->equals('{') ? $tokens->getPrevNonWhitespace($endIndex) : $endIndex; |
||
319 | |||
320 | for ($i = $startIndex; $i < $endIndex; ++$i) { |
||
321 | if ($tokens[$i]->equals(',')) { |
||
322 | ++$implementsInfo[$label]; |
||
323 | |||
324 | continue; |
||
325 | } |
||
326 | |||
327 | if (!$implementsInfo['multiLine'] && false !== strpos($tokens[$i]->getContent(), "\n")) { |
||
328 | $implementsInfo['multiLine'] = true; |
||
329 | } |
||
330 | } |
||
331 | |||
332 | return $implementsInfo; |
||
333 | } |
||
334 | |||
335 | private function makeClassyDefinitionSingleLine(Tokens $tokens, int $startIndex, int $endIndex): void |
||
336 | { |
||
337 | for ($i = $endIndex; $i >= $startIndex; --$i) { |
||
338 | if ($tokens[$i]->isWhitespace()) { |
||
339 | if ($tokens[$i - 1]->isComment() || $tokens[$i + 1]->isComment()) { |
||
340 | $content = $tokens[$i - 1]->getContent(); |
||
341 | |||
342 | if (!('#' === $content || '//' === substr($content, 0, 2))) { |
||
343 | $content = $tokens[$i + 1]->getContent(); |
||
344 | |||
345 | if (!('#' === $content || '//' === substr($content, 0, 2))) { |
||
346 | $tokens[$i] = new Token([T_WHITESPACE, ' ']); |
||
347 | } |
||
348 | } |
||
349 | |||
350 | continue; |
||
351 | } |
||
352 | |||
353 | if ($tokens[$i - 1]->isGivenKind(T_CLASS) && $tokens[$i + 1]->equals('(')) { |
||
354 | if (true === $this->configuration['space_before_parenthesis']) { |
||
355 | $tokens[$i] = new Token([T_WHITESPACE, ' ']); |
||
356 | } else { |
||
357 | $tokens->clearAt($i); |
||
358 | } |
||
359 | |||
360 | continue; |
||
361 | } |
||
362 | |||
363 | if (!$tokens[$i - 1]->equals(',') && $tokens[$i + 1]->equalsAny([',', ')']) || $tokens[$i - 1]->equals('(')) { |
||
364 | $tokens->clearAt($i); |
||
365 | |||
366 | continue; |
||
367 | } |
||
368 | |||
369 | $tokens[$i] = new Token([T_WHITESPACE, ' ']); |
||
370 | |||
371 | continue; |
||
372 | } |
||
373 | |||
374 | if ($tokens[$i]->equals(',') && !$tokens[$i + 1]->isWhitespace()) { |
||
375 | $tokens->insertAt($i + 1, new Token([T_WHITESPACE, ' '])); |
||
376 | |||
377 | continue; |
||
378 | } |
||
379 | |||
380 | if ($this->configuration['space_before_parenthesis'] && $tokens[$i]->isGivenKind(T_CLASS) && !$tokens[$i + 1]->isWhitespace()) { |
||
381 | $tokens->insertAt($i + 1, new Token([T_WHITESPACE, ' '])); |
||
382 | |||
383 | continue; |
||
384 | } |
||
385 | |||
386 | if (!$tokens[$i]->isComment()) { |
||
387 | continue; |
||
388 | } |
||
389 | |||
390 | if (!$tokens[$i + 1]->isWhitespace() && !$tokens[$i + 1]->isComment() && false === strpos($tokens[$i]->getContent(), "\n")) { |
||
391 | $tokens->insertAt($i + 1, new Token([T_WHITESPACE, ' '])); |
||
392 | } |
||
393 | |||
394 | if (!$tokens[$i - 1]->isWhitespace() && !$tokens[$i - 1]->isComment()) { |
||
395 | $tokens->insertAt($i, new Token([T_WHITESPACE, ' '])); |
||
396 | } |
||
397 | } |
||
398 | } |
||
399 | |||
400 | private function makeClassyInheritancePartMultiLine(Tokens $tokens, int $startIndex, int $endIndex): void |
||
436 | } |
||
437 | } |
||
438 | } |
||
439 |