Complex classes like ControlStructureSpacingSniff 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 ControlStructureSpacingSniff, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | * @author Greg Sherwood <[email protected]> |
||
30 | * @author Marc McIntyre <[email protected]> |
||
31 | * @author Alexander Obuhovich <[email protected]> |
||
32 | * @license https://github.com/aik099/CodingStandard/blob/master/LICENSE BSD 3-Clause |
||
33 | * @link https://github.com/aik099/CodingStandard |
||
34 | */ |
||
35 | class ControlStructureSpacingSniff implements Sniff |
||
36 | { |
||
37 | |||
38 | /** |
||
39 | * A list of tokenizers this sniff supports. |
||
40 | * |
||
41 | * @var array |
||
42 | */ |
||
43 | public $supportedTokenizers = array( |
||
44 | 'PHP', |
||
45 | 'JS', |
||
46 | ); |
||
47 | |||
48 | /** |
||
49 | * How many spaces should follow the opening bracket. |
||
50 | * |
||
51 | * @var int |
||
52 | */ |
||
53 | public $requiredSpacesAfterOpen = 1; |
||
54 | |||
55 | /** |
||
56 | * How many spaces should precede the closing bracket. |
||
57 | * |
||
58 | * @var int |
||
59 | */ |
||
60 | public $requiredSpacesBeforeClose = 1; |
||
61 | |||
62 | |||
63 | /** |
||
64 | * Returns an array of tokens this test wants to listen for. |
||
65 | * |
||
66 | * @return integer[] |
||
67 | */ |
||
68 | public function register() |
||
83 | |||
84 | |||
85 | /** |
||
86 | * Processes this test, when one of its tokens is encountered. |
||
87 | * |
||
88 | * @param File $phpcsFile The file being scanned. |
||
89 | * @param int $stackPtr The position of the current token in the |
||
90 | * stack passed in $tokens. |
||
91 | * |
||
92 | * @return void |
||
93 | */ |
||
94 | public function process(File $phpcsFile, $stackPtr) |
||
110 | |||
111 | |||
112 | /** |
||
113 | * Checks bracket spacing. |
||
114 | * |
||
115 | * @param File $phpcsFile The file being scanned. |
||
116 | * @param int $stackPtr The position of the current token |
||
117 | * in the stack passed in $tokens. |
||
118 | * |
||
119 | * @return void |
||
120 | */ |
||
121 | protected function checkBracketSpacing(File $phpcsFile, $stackPtr) |
||
183 | |||
184 | |||
185 | /** |
||
186 | * Checks content inside. |
||
187 | * |
||
188 | * @param File $phpcsFile The file being scanned. |
||
189 | * @param int $stackPtr The position of the current token |
||
190 | * in the stack passed in $tokens. |
||
191 | * |
||
192 | * @return void |
||
193 | */ |
||
194 | protected function checkContentInside(File $phpcsFile, $stackPtr) |
||
286 | |||
287 | |||
288 | /** |
||
289 | * Checks leading content. |
||
290 | * |
||
291 | * @param File $phpcsFile The file being scanned. |
||
292 | * @param int $stackPtr The position of the current token |
||
293 | * in the stack passed in $tokens. |
||
294 | * |
||
295 | * @return void |
||
296 | */ |
||
297 | protected function checkLeadingContent(File $phpcsFile, $stackPtr) |
||
373 | |||
374 | |||
375 | /** |
||
376 | * Returns leading non-whitespace/comment token. |
||
377 | * |
||
378 | * @param File $phpcsFile All the tokens found in the document. |
||
379 | * @param int $stackPtr The position of the current token |
||
380 | * in the stack passed in $tokens. |
||
381 | * |
||
382 | * @return int|bool |
||
383 | */ |
||
384 | protected function getLeadingContent(File $phpcsFile, $stackPtr) |
||
398 | |||
399 | /** |
||
400 | * Returns leading comment or self. |
||
401 | * |
||
402 | * @param File $phpcsFile All the tokens found in the document. |
||
403 | * @param int $stackPtr The position of the current token |
||
404 | * in the stack passed in $tokens. |
||
405 | * |
||
406 | * @return bool|int |
||
407 | */ |
||
408 | protected function getLeadingCommentOrSelf(File $phpcsFile, $stackPtr) |
||
433 | |||
434 | /** |
||
435 | * Checks trailing content. |
||
436 | * |
||
437 | * @param File $phpcsFile The file being scanned. |
||
438 | * @param int $stackPtr The position of the current token |
||
439 | * in the stack passed in $tokens. |
||
440 | * |
||
441 | * @return void |
||
442 | */ |
||
443 | protected function checkTrailingContent(File $phpcsFile, $stackPtr) |
||
521 | |||
522 | |||
523 | /** |
||
524 | * Returns scope closer with special check for "do...while" statements. |
||
525 | * |
||
526 | * @param File $phpcsFile All the tokens found in the document. |
||
527 | * @param int $stackPtr The position of the current token |
||
528 | * in the stack passed in $tokens. |
||
529 | * |
||
530 | * @return int|bool |
||
531 | */ |
||
532 | protected function getScopeCloser(File $phpcsFile, $stackPtr) |
||
558 | |||
559 | |||
560 | /** |
||
561 | * Returns trailing content token. |
||
562 | * |
||
563 | * @param File $phpcsFile All the tokens found in the document. |
||
564 | * @param int $stackPtr The position of the current token |
||
565 | * in the stack passed in $tokens. |
||
566 | * |
||
567 | * @return int|bool |
||
568 | */ |
||
569 | protected function getTrailingContent(File $phpcsFile, $stackPtr) |
||
583 | |||
584 | |||
585 | /** |
||
586 | * Returns trailing comment or self. |
||
587 | * |
||
588 | * @param File $phpcsFile All the tokens found in the document. |
||
589 | * @param int $stackPtr The position of the current token |
||
590 | * in the stack passed in $tokens. |
||
591 | * |
||
592 | * @return bool|int |
||
593 | */ |
||
594 | protected function getTrailingCommentOrSelf(File $phpcsFile, $stackPtr) |
||
619 | |||
620 | |||
621 | /** |
||
622 | * Finds first token on a line. |
||
623 | * |
||
624 | * @param File $phpcsFile All the tokens found in the document. |
||
625 | * @param int $start Start from token. |
||
626 | * |
||
627 | * @return int | bool |
||
628 | */ |
||
629 | public function findFirstOnLine(File $phpcsFile, $start) |
||
643 | |||
644 | |||
645 | /** |
||
646 | * Detects, that we're at the edge (beginning or ending) of CASE/DEFAULT with SWITCH statement. |
||
647 | * |
||
648 | * @param File $phpcsFile The file being scanned. |
||
649 | * @param int $stackPtr The position of the current token |
||
650 | * in the stack passed in $tokens. |
||
651 | * |
||
652 | * @return bool |
||
653 | */ |
||
654 | protected function insideSwitchCase(File $phpcsFile, $stackPtr) |
||
665 | |||
666 | |||
667 | /** |
||
668 | * Detects, that it is a closing brace of IF/ELSEIF. |
||
669 | * |
||
670 | * @param File $phpcsFile The file being scanned. |
||
671 | * @param int $stackPtr The position of the current token |
||
672 | * in the stack passed in $tokens. |
||
673 | * |
||
674 | * @return bool |
||
675 | */ |
||
676 | protected function ifOrElseIf(File $phpcsFile, $stackPtr) |
||
680 | |||
681 | |||
682 | /** |
||
683 | * Detects, that it is a closing brace of ELSE/ELSEIF. |
||
684 | * |
||
685 | * @param File $phpcsFile The file being scanned. |
||
686 | * @param int $stackPtr The position of the current token |
||
687 | * in the stack passed in $tokens. |
||
688 | * |
||
689 | * @return bool |
||
690 | */ |
||
691 | protected function elseOrElseIf(File $phpcsFile, $stackPtr) |
||
695 | |||
696 | |||
697 | /** |
||
698 | * Detects, that it is a closing brace of TRY/CATCH. |
||
699 | * |
||
700 | * @param File $phpcsFile The file being scanned. |
||
701 | * @param int $stackPtr The position of the current token |
||
702 | * in the stack passed in $tokens. |
||
703 | * |
||
704 | * @return bool |
||
705 | */ |
||
706 | protected function isTryOrCatch(File $phpcsFile, $stackPtr) |
||
710 | |||
711 | |||
712 | /** |
||
713 | * Detects, that it is a closing brace of TRY. |
||
714 | * |
||
715 | * @param File $phpcsFile The file being scanned. |
||
716 | * @param int $stackPtr The position of the current token |
||
717 | * in the stack passed in $tokens. |
||
718 | * |
||
719 | * @return bool |
||
720 | */ |
||
721 | protected function isTry(File $phpcsFile, $stackPtr) |
||
725 | |||
726 | |||
727 | /** |
||
728 | * Detects, that it is a closing brace of CATCH. |
||
729 | * |
||
730 | * @param File $phpcsFile The file being scanned. |
||
731 | * @param int $stackPtr The position of the current token |
||
732 | * in the stack passed in $tokens. |
||
733 | * |
||
734 | * @return bool |
||
735 | */ |
||
736 | protected function isCatch(File $phpcsFile, $stackPtr) |
||
740 | |||
741 | |||
742 | /** |
||
743 | * Determines that a function is located at given position. |
||
744 | * |
||
745 | * @param File $phpcsFile The file being scanned. |
||
746 | * @param int $stackPtr The position of the current token |
||
747 | * in the stack passed in $tokens. |
||
748 | * |
||
749 | * @return bool |
||
750 | */ |
||
751 | protected function isFunction(File $phpcsFile, $stackPtr) |
||
755 | |||
756 | |||
757 | /** |
||
758 | * Determines that a closure is located at given position. |
||
759 | * |
||
760 | * @param File $phpcsFile The file being scanned. |
||
761 | * @param int $stackPtr The position of the current token. |
||
762 | * in the stack passed in $tokens. |
||
763 | * @param int $scopeConditionPtr Position of scope condition. |
||
764 | * |
||
765 | * @return bool |
||
766 | */ |
||
767 | protected function isClosure(File $phpcsFile, $stackPtr, $scopeConditionPtr) |
||
781 | |||
782 | |||
783 | /** |
||
784 | * Detects, that it is a closing brace of ELSE/ELSEIF. |
||
785 | * |
||
786 | * @param File $phpcsFile The file being scanned. |
||
787 | * @param int $stackPtr The position of the current token |
||
788 | * in the stack passed in $tokens. |
||
789 | * @param int|array $types The type(s) of tokens to search for. |
||
790 | * |
||
791 | * @return bool |
||
792 | */ |
||
793 | protected function isScopeCondition(File $phpcsFile, $stackPtr, $types) |
||
807 | }//end class |
||
808 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.