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 | class CodingStandard_Sniffs_WhiteSpace_ControlStructureSpacingSniff implements PHP_CodeSniffer_Sniff |
||
|
|||
30 | { |
||
31 | |||
32 | /** |
||
33 | * A list of tokenizers this sniff supports. |
||
34 | * |
||
35 | * @var array |
||
36 | */ |
||
37 | public $supportedTokenizers = array( |
||
38 | 'PHP', |
||
39 | 'JS', |
||
40 | ); |
||
41 | |||
42 | /** |
||
43 | * How many spaces should follow the opening bracket. |
||
44 | * |
||
45 | * @var int |
||
46 | */ |
||
47 | public $requiredSpacesAfterOpen = 1; |
||
48 | |||
49 | /** |
||
50 | * How many spaces should precede the closing bracket. |
||
51 | * |
||
52 | * @var int |
||
53 | */ |
||
54 | public $requiredSpacesBeforeClose = 1; |
||
55 | |||
56 | |||
57 | /** |
||
58 | * Returns an array of tokens this test wants to listen for. |
||
59 | * |
||
60 | * @return integer[] |
||
61 | */ |
||
62 | 1 | public function register() |
|
78 | |||
79 | |||
80 | /** |
||
81 | * Processes this test, when one of its tokens is encountered. |
||
82 | * |
||
83 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
84 | * @param int $stackPtr The position of the current token |
||
85 | * in the stack passed in $tokens. |
||
86 | * |
||
87 | * @return void |
||
88 | */ |
||
89 | 1 | public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
106 | |||
107 | |||
108 | /** |
||
109 | * Checks bracket spacing. |
||
110 | * |
||
111 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
112 | * @param int $stackPtr The position of the current token |
||
113 | * in the stack passed in $tokens. |
||
114 | * |
||
115 | * @return void |
||
116 | */ |
||
117 | 1 | protected function checkBracketSpacing(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
180 | |||
181 | |||
182 | /** |
||
183 | * Checks content inside. |
||
184 | * |
||
185 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
186 | * @param int $stackPtr The position of the current token |
||
187 | * in the stack passed in $tokens. |
||
188 | * |
||
189 | * @return void |
||
190 | */ |
||
191 | 1 | protected function checkContentInside(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
284 | |||
285 | |||
286 | /** |
||
287 | * Checks leading content. |
||
288 | * |
||
289 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
290 | * @param int $stackPtr The position of the current token |
||
291 | * in the stack passed in $tokens. |
||
292 | * |
||
293 | * @return void |
||
294 | */ |
||
295 | 1 | protected function checkLeadingContent(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
372 | |||
373 | |||
374 | /** |
||
375 | * Returns leading non-whitespace/comment token. |
||
376 | * |
||
377 | * @param PHP_CodeSniffer_File $phpcsFile All the tokens found in the document. |
||
378 | * @param int $stackPtr The position of the current token |
||
379 | * in the stack passed in $tokens. |
||
380 | * |
||
381 | * @return int|bool |
||
382 | */ |
||
383 | 1 | protected function getLeadingContent(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
398 | |||
399 | /** |
||
400 | * Returns leading comment or self. |
||
401 | * |
||
402 | * @param PHP_CodeSniffer_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 | 1 | protected function getLeadingCommentOrSelf(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
434 | |||
435 | /** |
||
436 | * Checks trailing content. |
||
437 | * |
||
438 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
439 | * @param int $stackPtr The position of the current token |
||
440 | * in the stack passed in $tokens. |
||
441 | * |
||
442 | * @return void |
||
443 | */ |
||
444 | 1 | protected function checkTrailingContent(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
523 | |||
524 | |||
525 | /** |
||
526 | * Returns scope closer with special check for "do...while" statements. |
||
527 | * |
||
528 | * @param PHP_CodeSniffer_File $phpcsFile All the tokens found in the document. |
||
529 | * @param int $stackPtr The position of the current token |
||
530 | * in the stack passed in $tokens. |
||
531 | * |
||
532 | * @return int|bool |
||
533 | */ |
||
534 | 1 | protected function getScopeCloser(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
561 | |||
562 | |||
563 | /** |
||
564 | * Returns trailing content token. |
||
565 | * |
||
566 | * @param PHP_CodeSniffer_File $phpcsFile All the tokens found in the document. |
||
567 | * @param int $stackPtr The position of the current token |
||
568 | * in the stack passed in $tokens. |
||
569 | * |
||
570 | * @return int|bool |
||
571 | */ |
||
572 | 1 | protected function getTrailingContent(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
587 | |||
588 | |||
589 | /** |
||
590 | * Returns trailing comment or self. |
||
591 | * |
||
592 | * @param PHP_CodeSniffer_File $phpcsFile All the tokens found in the document. |
||
593 | * @param int $stackPtr The position of the current token |
||
594 | * in the stack passed in $tokens. |
||
595 | * |
||
596 | * @return bool|int |
||
597 | */ |
||
598 | 1 | protected function getTrailingCommentOrSelf(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
624 | |||
625 | |||
626 | /** |
||
627 | * Finds first token on a line. |
||
628 | * |
||
629 | * @param PHP_CodeSniffer_File $phpcsFile All the tokens found in the document. |
||
630 | * @param int $start Start from token. |
||
631 | * |
||
632 | * @return int | bool |
||
633 | */ |
||
634 | public function findFirstOnLine(PHP_CodeSniffer_File $phpcsFile, $start) |
||
649 | |||
650 | |||
651 | /** |
||
652 | * Detects, that we're at the edge (beginning or ending) of CASE/DEFAULT with SWITCH statement. |
||
653 | * |
||
654 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
655 | * @param int $stackPtr The position of the current token |
||
656 | * in the stack passed in $tokens. |
||
657 | * |
||
658 | * @return bool |
||
659 | */ |
||
660 | 1 | protected function insideSwitchCase(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
672 | |||
673 | |||
674 | /** |
||
675 | * Detects, that it is a closing brace of IF/ELSEIF. |
||
676 | * |
||
677 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
678 | * @param int $stackPtr The position of the current token |
||
679 | * in the stack passed in $tokens. |
||
680 | * |
||
681 | * @return bool |
||
682 | */ |
||
683 | 1 | protected function ifOrElseIf(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
688 | |||
689 | |||
690 | /** |
||
691 | * Detects, that it is a closing brace of ELSE/ELSEIF. |
||
692 | * |
||
693 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
694 | * @param int $stackPtr The position of the current token |
||
695 | * in the stack passed in $tokens. |
||
696 | * |
||
697 | * @return bool |
||
698 | */ |
||
699 | 1 | protected function elseOrElseIf(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
704 | |||
705 | |||
706 | /** |
||
707 | * Detects, that it is a closing brace of TRY/CATCH. |
||
708 | * |
||
709 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
710 | * @param int $stackPtr The position of the current token |
||
711 | * in the stack passed in $tokens. |
||
712 | * |
||
713 | * @return bool |
||
714 | */ |
||
715 | 1 | protected function isTryOrCatch(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
720 | |||
721 | |||
722 | /** |
||
723 | * Detects, that it is a closing brace of TRY. |
||
724 | * |
||
725 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
726 | * @param int $stackPtr The position of the current token |
||
727 | * in the stack passed in $tokens. |
||
728 | * |
||
729 | * @return bool |
||
730 | */ |
||
731 | protected function isTry(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
736 | |||
737 | |||
738 | /** |
||
739 | * Detects, that it is a closing brace of CATCH. |
||
740 | * |
||
741 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
742 | * @param int $stackPtr The position of the current token |
||
743 | * in the stack passed in $tokens. |
||
744 | * |
||
745 | * @return bool |
||
746 | */ |
||
747 | 1 | protected function isCatch(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
752 | |||
753 | |||
754 | /** |
||
755 | * Determines that a function is located at given position. |
||
756 | * |
||
757 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
758 | * @param int $stackPtr The position of the current token |
||
759 | * in the stack passed in $tokens. |
||
760 | * |
||
761 | * @return bool |
||
762 | */ |
||
763 | 1 | protected function isFunction(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
768 | |||
769 | |||
770 | /** |
||
771 | * Determines that a closure is located at given position. |
||
772 | * |
||
773 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
774 | * @param int $stackPtr The position of the current token. |
||
775 | * in the stack passed in $tokens. |
||
776 | * @param int $scopeConditionPtr Position of scope condition. |
||
777 | * |
||
778 | * @return bool |
||
779 | */ |
||
780 | 1 | protected function isClosure(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $scopeConditionPtr) |
|
795 | |||
796 | |||
797 | /** |
||
798 | * Detects, that it is a closing brace of ELSE/ELSEIF. |
||
799 | * |
||
800 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
801 | * @param int $stackPtr The position of the current token |
||
802 | * in the stack passed in $tokens. |
||
803 | * @param int|array $types The type(s) of tokens to search for. |
||
804 | * |
||
805 | * @return bool |
||
806 | */ |
||
807 | 1 | protected function isScopeCondition(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $types) |
|
822 | |||
823 | |||
824 | }//end class |
||
825 |
Classes in PHP are usually named in CamelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.
Thus the name database provider becomes
DatabaseProvider
.