Complex classes like ForbiddenNamesSniff 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 ForbiddenNamesSniff, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class ForbiddenNamesSniff extends Sniff |
||
27 | { |
||
28 | |||
29 | /** |
||
30 | * A list of keywords that can not be used as function, class and namespace name or constant name. |
||
31 | * Mentions since which version it's not allowed. |
||
32 | * |
||
33 | * @var array(string => string) |
||
34 | */ |
||
35 | protected $invalidNames = array( |
||
36 | 'abstract' => '5.0', |
||
37 | 'and' => 'all', |
||
38 | 'array' => 'all', |
||
39 | 'as' => 'all', |
||
40 | 'break' => 'all', |
||
41 | 'callable' => '5.4', |
||
42 | 'case' => 'all', |
||
43 | 'catch' => '5.0', |
||
44 | 'class' => 'all', |
||
45 | 'clone' => '5.0', |
||
46 | 'const' => 'all', |
||
47 | 'continue' => 'all', |
||
48 | 'declare' => 'all', |
||
49 | 'default' => 'all', |
||
50 | 'do' => 'all', |
||
51 | 'else' => 'all', |
||
52 | 'elseif' => 'all', |
||
53 | 'enddeclare' => 'all', |
||
54 | 'endfor' => 'all', |
||
55 | 'endforeach' => 'all', |
||
56 | 'endif' => 'all', |
||
57 | 'endswitch' => 'all', |
||
58 | 'endwhile' => 'all', |
||
59 | 'extends' => 'all', |
||
60 | 'final' => '5.0', |
||
61 | 'finally' => '5.5', |
||
62 | 'for' => 'all', |
||
63 | 'foreach' => 'all', |
||
64 | 'function' => 'all', |
||
65 | 'global' => 'all', |
||
66 | 'goto' => '5.3', |
||
67 | 'if' => 'all', |
||
68 | 'implements' => '5.0', |
||
69 | 'interface' => '5.0', |
||
70 | 'instanceof' => '5.0', |
||
71 | 'insteadof' => '5.4', |
||
72 | 'namespace' => '5.3', |
||
73 | 'new' => 'all', |
||
74 | 'or' => 'all', |
||
75 | 'private' => '5.0', |
||
76 | 'protected' => '5.0', |
||
77 | 'public' => '5.0', |
||
78 | 'static' => 'all', |
||
79 | 'switch' => 'all', |
||
80 | 'throw' => '5.0', |
||
81 | 'trait' => '5.4', |
||
82 | 'try' => '5.0', |
||
83 | 'use' => 'all', |
||
84 | 'var' => 'all', |
||
85 | 'while' => 'all', |
||
86 | 'xor' => 'all', |
||
87 | '__class__' => 'all', |
||
88 | '__dir__' => '5.3', |
||
89 | '__file__' => 'all', |
||
90 | '__function__' => 'all', |
||
91 | '__method__' => 'all', |
||
92 | '__namespace__' => '5.3', |
||
93 | ); |
||
94 | |||
95 | /** |
||
96 | * A list of keywords that can follow use statements. |
||
97 | * |
||
98 | * @var array(string => string) |
||
99 | */ |
||
100 | protected $validUseNames = array( |
||
101 | 'const' => true, |
||
102 | 'function' => true, |
||
103 | ); |
||
104 | |||
105 | /** |
||
106 | * Whether PHPCS 1.x is used or not. |
||
107 | * |
||
108 | * @var bool |
||
109 | */ |
||
110 | protected $isLowPHPCS = false; |
||
111 | |||
112 | /** |
||
113 | * Scope modifiers and other keywords allowed in trait use statements. |
||
114 | * |
||
115 | * @var array |
||
116 | */ |
||
117 | private $allowedModifiers = array(); |
||
118 | |||
119 | /** |
||
120 | * Targeted tokens. |
||
121 | * |
||
122 | * @var array |
||
123 | */ |
||
124 | protected $targetedTokens = array( |
||
125 | T_CLASS, |
||
126 | T_FUNCTION, |
||
127 | T_NAMESPACE, |
||
128 | T_STRING, |
||
129 | T_CONST, |
||
130 | T_USE, |
||
131 | T_AS, |
||
132 | T_EXTENDS, |
||
133 | T_INTERFACE, |
||
134 | ); |
||
135 | |||
136 | /** |
||
137 | * Returns an array of tokens this test wants to listen for. |
||
138 | * |
||
139 | * @return array |
||
140 | */ |
||
141 | public function register() |
||
165 | |||
166 | /** |
||
167 | * Processes this test, when one of its tokens is encountered. |
||
168 | * |
||
169 | * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
170 | * @param int $stackPtr The position of the current token in the |
||
171 | * stack passed in $tokens. |
||
172 | * |
||
173 | * @return void |
||
174 | */ |
||
175 | public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
188 | |||
189 | /** |
||
190 | * Processes this test, when one of its tokens is encountered. |
||
191 | * |
||
192 | * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
193 | * @param int $stackPtr The position of the current token in the |
||
194 | * stack passed in $tokens. |
||
195 | * @param array $tokens The stack of tokens that make up |
||
196 | * the file. |
||
197 | * |
||
198 | * @return void |
||
199 | */ |
||
200 | public function processNonString(\PHP_CodeSniffer_File $phpcsFile, $stackPtr, $tokens) |
||
331 | |||
332 | /** |
||
333 | * Processes this test, when one of its tokens is encountered. |
||
334 | * |
||
335 | * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
336 | * @param int $stackPtr The position of the current token in the |
||
337 | * stack passed in $tokens. |
||
338 | * @param array $tokens The stack of tokens that make up |
||
339 | * the file. |
||
340 | * |
||
341 | * @return void |
||
342 | */ |
||
343 | public function processString(\PHP_CodeSniffer_File $phpcsFile, $stackPtr, $tokens) |
||
380 | |||
381 | |||
382 | /** |
||
383 | * Add the error message. |
||
384 | * |
||
385 | * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
386 | * @param int $stackPtr The position of the current token in the |
||
387 | * stack passed in $tokens. |
||
388 | * @param string $content The token content found. |
||
389 | * @param array $data The data to pass into the error message. |
||
390 | * |
||
391 | * @return void |
||
392 | */ |
||
393 | protected function addError($phpcsFile, $stackPtr, $content, $data) |
||
399 | |||
400 | |||
401 | /** |
||
402 | * Check if the current token code is for a token which can be considered |
||
403 | * the end of a (partial) use statement. |
||
404 | * |
||
405 | * @param int $token The current token information. |
||
406 | * |
||
407 | * @return bool |
||
408 | */ |
||
409 | protected function isEndOfUseStatement($token) |
||
413 | }//end class |
||
414 |