Complex classes like AbstractFileDecorator 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 AbstractFileDecorator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | abstract class AbstractFileDecorator extends File |
||
19 | { |
||
20 | /** |
||
21 | * The CodeSniffer file |
||
22 | * |
||
23 | * @var File |
||
24 | */ |
||
25 | private $baseFile; |
||
26 | |||
27 | /** |
||
28 | * File constructor. |
||
29 | * |
||
30 | * @param File $baseFile CodeSniffer file |
||
31 | */ |
||
32 | public function __construct(File $baseFile) |
||
38 | |||
39 | /** |
||
40 | * Returns the wrapped classes method result. |
||
41 | * |
||
42 | * @param string $method |
||
43 | * @param array $args |
||
44 | * |
||
45 | * @return mixed |
||
46 | */ |
||
47 | public function __call(string $method, array $args = []) |
||
51 | |||
52 | /** |
||
53 | * Records an error against a specific token in the file. |
||
54 | * |
||
55 | * @param string $error The error message. |
||
56 | * @param int $stackPtr The stack position where the error occurred. |
||
57 | * @param string $code A violation code unique to the sniff message. |
||
58 | * @param array $data Replacements for the error message. |
||
59 | * @param int $severity The severity level for this error. A value of 0 |
||
60 | * will be converted into the default severity level. |
||
61 | * @param boolean $fixable Can the error be fixed by the sniff? |
||
62 | * |
||
63 | * @return boolean |
||
64 | */ |
||
65 | public function addError( |
||
75 | |||
76 | /** |
||
77 | * Records an error against a specific line in the file. |
||
78 | * |
||
79 | * @param string $error The error message. |
||
80 | * @param int $line The line on which the error occurred. |
||
81 | * @param string $code A violation code unique to the sniff message. |
||
82 | * @param array $data Replacements for the error message. |
||
83 | * @param int $severity The severity level for this error. A value of 0 |
||
84 | * will be converted into the default severity level. |
||
85 | * |
||
86 | * @return boolean |
||
87 | */ |
||
88 | public function addErrorOnLine( |
||
97 | |||
98 | /** |
||
99 | * Records a fixable error against a specific token in the file. |
||
100 | * |
||
101 | * Returns true if the error was recorded and should be fixed. |
||
102 | * |
||
103 | * @param string $error The error message. |
||
104 | * @param int $stackPtr The stack position where the error occurred. |
||
105 | * @param string $code A violation code unique to the sniff message. |
||
106 | * @param array $data Replacements for the error message. |
||
107 | * @param int $severity The severity level for this error. A value of 0 |
||
108 | * will be converted into the default severity level. |
||
109 | * |
||
110 | * @return boolean |
||
111 | */ |
||
112 | public function addFixableError( |
||
121 | |||
122 | /** |
||
123 | * Records a fixable warning against a specific token in the file. |
||
124 | * |
||
125 | * Returns true if the warning was recorded and should be fixed. |
||
126 | * |
||
127 | * @param string $warning The error message. |
||
128 | * @param int $stackPtr The stack position where the error occurred. |
||
129 | * @param string $code A violation code unique to the sniff message. |
||
130 | * @param array $data Replacements for the warning message. |
||
131 | * @param int $severity The severity level for this warning. A value of 0 |
||
132 | * will be converted into the default severity level. |
||
133 | * |
||
134 | * @return boolean |
||
135 | */ |
||
136 | public function addFixableWarning( |
||
145 | |||
146 | /** |
||
147 | * Adds an error to the error stack. |
||
148 | * |
||
149 | * @param boolean $error Is this an error message? |
||
150 | * @param string $message The text of the message. |
||
151 | * @param int $line The line on which the message occurred. |
||
152 | * @param int $column The column at which the message occurred. |
||
153 | * @param string $code A violation code unique to the sniff message. |
||
154 | * @param array $data Replacements for the message. |
||
155 | * @param int $severity The severity level for this message. A value of 0 |
||
156 | * will be converted into the default severity level. |
||
157 | * @param boolean $fixable Can the problem be fixed by the sniff? |
||
158 | * |
||
159 | * @return boolean |
||
160 | */ |
||
161 | protected function addMessage($error, $message, $line, $column, $code, $data, $severity, $fixable) |
||
165 | |||
166 | /** |
||
167 | * Records a warning against a specific token in the file. |
||
168 | * |
||
169 | * @param string $warning The error message. |
||
170 | * @param int $stackPtr The stack position where the error occurred. |
||
171 | * @param string $code A violation code unique to the sniff message. |
||
172 | * @param array $data Replacements for the warning message. |
||
173 | * @param int $severity The severity level for this warning. A value of 0 |
||
174 | * will be converted into the default severity level. |
||
175 | * @param boolean $fixable Can the warning be fixed by the sniff? |
||
176 | * |
||
177 | * @return boolean |
||
178 | */ |
||
179 | public function addWarning( |
||
189 | |||
190 | /** |
||
191 | * Records a warning against a specific token in the file. |
||
192 | * |
||
193 | * @param string $warning The error message. |
||
194 | * @param int $line The line on which the warning occurred. |
||
195 | * @param string $code A violation code unique to the sniff message. |
||
196 | * @param array $data Replacements for the warning message. |
||
197 | * @param int $severity The severity level for this warning. A value of 0 will |
||
198 | * will be converted into the default severity level. |
||
199 | * |
||
200 | * @return boolean |
||
201 | */ |
||
202 | public function addWarningOnLine( |
||
211 | |||
212 | /** |
||
213 | * Remove vars stored in this file that are no longer required. |
||
214 | * |
||
215 | * @return void |
||
216 | */ |
||
217 | public function cleanUp() |
||
221 | |||
222 | /** |
||
223 | * Disables caching of this file. |
||
224 | * |
||
225 | * @return void |
||
226 | */ |
||
227 | public function disableCaching() |
||
231 | |||
232 | /** |
||
233 | * Returns the position of the last non-whitespace token in a statement. |
||
234 | * |
||
235 | * @param int $start The position to start searching from in the token stack. |
||
236 | * @param int|array $ignore Token types that should not be considered stop points. |
||
237 | * |
||
238 | * @return int |
||
239 | */ |
||
240 | public function findEndOfStatement($start, $ignore = null) |
||
244 | |||
245 | /** |
||
246 | * Returns the name of the class that the specified class extends. |
||
247 | * (works for classes, anonymous classes and interfaces) |
||
248 | * |
||
249 | * Returns FALSE on error or if there is no extended class name. |
||
250 | * |
||
251 | * @param int $stackPtr The stack position of the class. |
||
252 | * |
||
253 | * @return string|false |
||
254 | */ |
||
255 | public function findExtendedClassName($stackPtr) |
||
259 | |||
260 | /** |
||
261 | * Returns the position of the first token on a line, matching given type. |
||
262 | * |
||
263 | * Returns false if no token can be found. |
||
264 | * |
||
265 | * @param int|array $types The type(s) of tokens to search for. |
||
266 | * @param int $start The position to start searching from in the |
||
267 | * token stack. The first token matching on |
||
268 | * this line before this token will be returned. |
||
269 | * @param bool $exclude If true, find the token that is NOT of |
||
270 | * the types specified in $types. |
||
271 | * @param string $value The value that the token must be equal to. |
||
272 | * If value is omitted, tokens with any value will |
||
273 | * be returned. |
||
274 | * |
||
275 | * @return int | bool |
||
276 | */ |
||
277 | public function findFirstOnLine($types, $start, $exclude = false, $value = null) |
||
281 | |||
282 | /** |
||
283 | * Returns the names of the interfaces that the specified class implements. |
||
284 | * |
||
285 | * Returns FALSE on error or if there are no implemented interface names. |
||
286 | * |
||
287 | * @param int $stackPtr The stack position of the class. |
||
288 | * |
||
289 | * @return array|false |
||
290 | */ |
||
291 | public function findImplementedInterfaceNames($stackPtr) |
||
295 | |||
296 | /** |
||
297 | * Returns the position of the next specified token(s). |
||
298 | * |
||
299 | * If a value is specified, the next token of the specified type(s) |
||
300 | * containing the specified value will be returned. |
||
301 | * |
||
302 | * Returns false if no token can be found. |
||
303 | * |
||
304 | * @param int|array $types The type(s) of tokens to search for. |
||
305 | * @param int $start The position to start searching from in the |
||
306 | * token stack. |
||
307 | * @param int $end The end position to fail if no token is found. |
||
308 | * if not specified or null, end will default to |
||
309 | * the end of the token stack. |
||
310 | * @param bool $exclude If true, find the next token that is NOT of |
||
311 | * a type specified in $types. |
||
312 | * @param string $value The value that the token(s) must be equal to. |
||
313 | * If value is omitted, tokens with any value will |
||
314 | * be returned. |
||
315 | * @param bool $local If true, tokens outside the current statement |
||
316 | * will not be checked. i.e., checking will stop |
||
317 | * at the next semi-colon found. |
||
318 | * |
||
319 | * @return int|bool |
||
320 | * @see findPrevious() |
||
321 | */ |
||
322 | public function findNext( |
||
332 | |||
333 | /** |
||
334 | * Returns the position of the previous specified token(s). |
||
335 | * |
||
336 | * If a value is specified, the previous token of the specified type(s) |
||
337 | * containing the specified value will be returned. |
||
338 | * |
||
339 | * Returns false if no token can be found. |
||
340 | * |
||
341 | * @param int|array $types The type(s) of tokens to search for. |
||
342 | * @param int $start The position to start searching from in the |
||
343 | * token stack. |
||
344 | * @param int $end The end position to fail if no token is found. |
||
345 | * if not specified or null, end will default to |
||
346 | * the start of the token stack. |
||
347 | * @param bool $exclude If true, find the previous token that is NOT of |
||
348 | * the types specified in $types. |
||
349 | * @param string $value The value that the token(s) must be equal to. |
||
350 | * If value is omitted, tokens with any value will |
||
351 | * be returned. |
||
352 | * @param bool $local If true, tokens outside the current statement |
||
353 | * will not be checked. IE. checking will stop |
||
354 | * at the previous semi-colon found. |
||
355 | * |
||
356 | * @return int|bool |
||
357 | * @see findNext() |
||
358 | */ |
||
359 | public function findPrevious( |
||
369 | |||
370 | /** |
||
371 | * Returns the position of the first non-whitespace token in a statement. |
||
372 | * |
||
373 | * @param int $start The position to start searching from in the token stack. |
||
374 | * @param int|array $ignore Token types that should not be considered stop points. |
||
375 | * |
||
376 | * @return int |
||
377 | */ |
||
378 | public function findStartOfStatement($start, $ignore = null) |
||
382 | |||
383 | /** |
||
384 | * Returns the original file for this decorator |
||
385 | * |
||
386 | * @return File The original file wrapper. |
||
387 | */ |
||
388 | public function getBaseFile(): File |
||
392 | |||
393 | /** |
||
394 | * Returns the visibility and implementation properties of a class. |
||
395 | * |
||
396 | * The format of the array is: |
||
397 | * <code> |
||
398 | * array( |
||
399 | * 'is_abstract' => false, // true if the abstract keyword was found. |
||
400 | * 'is_final' => false, // true if the final keyword was found. |
||
401 | * ); |
||
402 | * </code> |
||
403 | * |
||
404 | * @param int $stackPtr The position in the stack of the T_CLASS token to |
||
405 | * acquire the properties for. |
||
406 | * |
||
407 | * @return array |
||
408 | * @throws \PHP_CodeSniffer\Exceptions\TokenizerException If the specified position is not a |
||
409 | * T_CLASS token. |
||
410 | */ |
||
411 | public function getClassProperties($stackPtr) |
||
415 | |||
416 | /** |
||
417 | * Return the position of the condition for the passed token. |
||
418 | * |
||
419 | * Returns FALSE if the token does not have the condition. |
||
420 | * |
||
421 | * @param int $stackPtr The position of the token we are checking. |
||
422 | * @param int $type The type of token to search for. |
||
423 | * |
||
424 | * @return int |
||
425 | */ |
||
426 | public function getCondition($stackPtr, $type) |
||
430 | |||
431 | /** |
||
432 | * Returns the declaration names for classes, interfaces, traits, and functions. |
||
433 | * |
||
434 | * @param int $stackPtr The position of the declaration token which |
||
435 | * declared the class, interface, trait, or function. |
||
436 | * |
||
437 | * @return string|null The name of the class, interface, trait, or function; |
||
438 | * or NULL if the function or class is anonymous. |
||
439 | * @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the specified token is not of type |
||
440 | * T_FUNCTION, T_CLASS, T_ANON_CLASS, |
||
441 | * T_CLOSURE, T_TRAIT, or T_INTERFACE. |
||
442 | */ |
||
443 | public function getDeclarationName($stackPtr) |
||
447 | |||
448 | /** |
||
449 | * Returns the number of errors raised. |
||
450 | * |
||
451 | * @return int |
||
452 | */ |
||
453 | public function getErrorCount() |
||
457 | |||
458 | /** |
||
459 | * Returns the errors raised from processing this file. |
||
460 | * |
||
461 | * @return array |
||
462 | */ |
||
463 | public function getErrors() |
||
467 | |||
468 | /** |
||
469 | * Returns the absolute filename of this file. |
||
470 | * |
||
471 | * @return string |
||
472 | */ |
||
473 | public function getFilename() |
||
477 | |||
478 | /** |
||
479 | * Returns the number of fixable errors/warnings raised. |
||
480 | * |
||
481 | * @return int |
||
482 | */ |
||
483 | public function getFixableCount() |
||
487 | |||
488 | /** |
||
489 | * Returns the number of fixed errors/warnings. |
||
490 | * |
||
491 | * @return int |
||
492 | */ |
||
493 | public function getFixedCount() |
||
497 | |||
498 | /** |
||
499 | * Returns the list of ignored lines. |
||
500 | * |
||
501 | * @return array |
||
502 | */ |
||
503 | public function getIgnoredLines() |
||
507 | |||
508 | /** |
||
509 | * Returns the visibility and implementation properties of the class member |
||
510 | * variable found at the specified position in the stack. |
||
511 | * |
||
512 | * The format of the array is: |
||
513 | * |
||
514 | * <code> |
||
515 | * array( |
||
516 | * 'scope' => 'public', // public protected or protected. |
||
517 | * 'scope_specified' => false, // true if the scope was explicitely specified. |
||
518 | * 'is_static' => false, // true if the static keyword was found. |
||
519 | * ); |
||
520 | * </code> |
||
521 | * |
||
522 | * @param int $stackPtr The position in the stack of the T_VARIABLE token to |
||
523 | * acquire the properties for. |
||
524 | * |
||
525 | * @return array |
||
526 | * @throws \PHP_CodeSniffer\Exceptions\TokenizerException If the specified position is not a |
||
527 | * T_VARIABLE token, or if the position is not |
||
528 | * a class member variable. |
||
529 | */ |
||
530 | public function getMemberProperties($stackPtr) |
||
534 | |||
535 | /** |
||
536 | * Returns the method parameters for the specified function token. |
||
537 | * |
||
538 | * Each parameter is in the following format: |
||
539 | * |
||
540 | * <code> |
||
541 | * 0 => array( |
||
542 | * 'name' => '$var', // The variable name. |
||
543 | * 'token' => integer, // The stack pointer to the variable name. |
||
544 | * 'content' => string, // The full content of the variable definition. |
||
545 | * 'pass_by_reference' => boolean, // Is the variable passed by reference? |
||
546 | * 'variable_length' => boolean, // Is the param of variable length through use of `...` ? |
||
547 | * 'type_hint' => string, // The type hint for the variable. |
||
548 | * 'type_hint_token' => integer, // The stack pointer to the type hint |
||
549 | * // or false if there is no type hint. |
||
550 | * 'nullable_type' => boolean, // Is the variable using a nullable type? |
||
551 | * ) |
||
552 | * </code> |
||
553 | * |
||
554 | * Parameters with default values have an additional array index of |
||
555 | * 'default' with the value of the default as a string. |
||
556 | * |
||
557 | * @param int $stackPtr The position in the stack of the function token |
||
558 | * to acquire the parameters for. |
||
559 | * |
||
560 | * @return array |
||
561 | * @throws \PHP_CodeSniffer\Exceptions\TokenizerException If the specified $stackPtr is not of |
||
562 | * type T_FUNCTION or T_CLOSURE. |
||
563 | */ |
||
564 | public function getMethodParameters($stackPtr) |
||
568 | |||
569 | |||
570 | /** |
||
571 | * Returns the visibility and implementation properties of a method. |
||
572 | * |
||
573 | * The format of the array is: |
||
574 | * <code> |
||
575 | * array( |
||
576 | * 'scope' => 'public', // public protected or protected |
||
577 | * 'scope_specified' => true, // true is scope keyword was found. |
||
578 | * 'return_type' => '', // the return type of the method. |
||
579 | * 'return_type_token' => integer, // The stack pointer to the start of the return type |
||
580 | * // or false if there is no return type. |
||
581 | * 'nullable_return_type' => false, // true if the return type is nullable. |
||
582 | * 'is_abstract' => false, // true if the abstract keyword was found. |
||
583 | * 'is_final' => false, // true if the final keyword was found. |
||
584 | * 'is_static' => false, // true if the static keyword was found. |
||
585 | * ); |
||
586 | * </code> |
||
587 | * |
||
588 | * @param int $stackPtr The position in the stack of the function token to |
||
589 | * acquire the properties for. |
||
590 | * |
||
591 | * @return array |
||
592 | * @throws \PHP_CodeSniffer\Exceptions\TokenizerException If the specified position is not a |
||
593 | * T_FUNCTION token. |
||
594 | */ |
||
595 | public function getMethodProperties($stackPtr) |
||
599 | |||
600 | /** |
||
601 | * Returns the metrics found while processing this file. |
||
602 | * |
||
603 | * @return array |
||
604 | */ |
||
605 | public function getMetrics() |
||
609 | |||
610 | /** |
||
611 | * Returns the number of successes recorded. |
||
612 | * |
||
613 | * @return int |
||
614 | */ |
||
615 | public function getSuccessCount() |
||
619 | |||
620 | /** |
||
621 | * Returns the token stack for this file. |
||
622 | * |
||
623 | * @return array |
||
624 | */ |
||
625 | public function getTokens() |
||
629 | |||
630 | /** |
||
631 | * Returns the content of the tokens from the specified start position in |
||
632 | * the token stack for the specified length. |
||
633 | * |
||
634 | * @param int $start The position to start from in the token stack. |
||
635 | * @param int $length The length of tokens to traverse from the start pos. |
||
636 | * @param int $origContent Whether the original content or the tab replaced |
||
637 | * content should be used. |
||
638 | * |
||
639 | * @return string The token contents. |
||
640 | */ |
||
641 | public function getTokensAsString($start, $length, $origContent = false) |
||
645 | |||
646 | /** |
||
647 | * Returns the number of warnings raised. |
||
648 | * |
||
649 | * @return int |
||
650 | */ |
||
651 | public function getWarningCount() |
||
655 | |||
656 | /** |
||
657 | * Returns the warnings raised from processing this file. |
||
658 | * |
||
659 | * @return array |
||
660 | */ |
||
661 | public function getWarnings() |
||
665 | |||
666 | /** |
||
667 | * Determine if the passed token has a condition of one of the passed types. |
||
668 | * |
||
669 | * @param int $stackPtr The position of the token we are checking. |
||
670 | * @param int|array $types The type(s) of tokens to search for. |
||
671 | * |
||
672 | * @return boolean |
||
673 | */ |
||
674 | public function hasCondition($stackPtr, $types) |
||
678 | |||
679 | /** |
||
680 | * Determine if the passed token is a reference operator. |
||
681 | * |
||
682 | * Returns true if the specified token position represents a reference. |
||
683 | * Returns false if the token represents a bitwise operator. |
||
684 | * |
||
685 | * @param int $stackPtr The position of the T_BITWISE_AND token. |
||
686 | * |
||
687 | * @return boolean |
||
688 | */ |
||
689 | public function isReference($stackPtr) |
||
693 | |||
694 | /** |
||
695 | * Tokenizes the file and prepares it for the test run. |
||
696 | * |
||
697 | * @return void |
||
698 | */ |
||
699 | public function parse() |
||
703 | |||
704 | /** |
||
705 | * Starts the stack traversal and tells listeners when tokens are found. |
||
706 | * |
||
707 | * @return void |
||
708 | */ |
||
709 | public function process() |
||
713 | |||
714 | /** |
||
715 | * Record a metric about the file being examined. |
||
716 | * |
||
717 | * @param int $stackPtr The stack position where the metric was recorded. |
||
718 | * @param string $metric The name of the metric being recorded. |
||
719 | * @param string $value The value of the metric being recorded. |
||
720 | * |
||
721 | * @return boolean |
||
722 | */ |
||
723 | public function recordMetric($stackPtr, $metric, $value) |
||
727 | |||
728 | /** |
||
729 | * Reloads the content of the file. |
||
730 | * |
||
731 | * By default, we have no idea where our content comes from, so we can't do anything. |
||
732 | * |
||
733 | * @return void |
||
734 | */ |
||
735 | public function reloadContent() |
||
739 | |||
740 | /** |
||
741 | * Set the content of the file. |
||
742 | * |
||
743 | * Setting the content also calculates the EOL char being used. |
||
744 | * |
||
745 | * @param string $content The file content. |
||
746 | * |
||
747 | * @return void |
||
748 | */ |
||
749 | public function setContent($content) |
||
753 | |||
754 | /** |
||
755 | * We need to clone the properties of the base file to this. A magic getter on inherited props does not work. |
||
756 | * |
||
757 | * @param File $baseFile |
||
758 | * |
||
759 | * @return void |
||
760 | */ |
||
761 | private function takeProperties(File $baseFile): void |
||
769 | } |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: