Complex classes like Common 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 Common, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class Common |
||
16 | { |
||
17 | |||
18 | /** |
||
19 | * An array of variable types for param/var we will check. |
||
20 | * |
||
21 | * @var string[] |
||
22 | */ |
||
23 | public static $allowedTypes = array( |
||
24 | 'array', |
||
25 | 'boolean', |
||
26 | 'float', |
||
27 | 'integer', |
||
28 | 'mixed', |
||
29 | 'object', |
||
30 | 'string', |
||
31 | 'resource', |
||
32 | 'callable', |
||
33 | ); |
||
34 | |||
35 | |||
36 | /** |
||
37 | * CodeSniffer alternative for realpath. |
||
38 | * |
||
39 | * Allows for PHAR support. |
||
40 | * |
||
41 | * @param string $path The path to use. |
||
42 | * |
||
43 | * @return mixed |
||
44 | */ |
||
45 | public static function realpath($path) |
||
59 | |||
60 | |||
61 | /** |
||
62 | * Removes a base path from the front of a file path. |
||
63 | * |
||
64 | * @param string $path The path of the file. |
||
65 | * @param string $basepath The base path to remove. This should not end |
||
66 | * with a directory separator. |
||
67 | * |
||
68 | * @return string |
||
69 | */ |
||
70 | public static function stripBasepath($path, $basepath) |
||
89 | |||
90 | |||
91 | /** |
||
92 | * Detects the EOL character being used in a string. |
||
93 | * |
||
94 | * @param string $contents The contents to check. |
||
95 | * |
||
96 | * @return string |
||
97 | */ |
||
98 | public static function detectLineEndings($contents) |
||
110 | |||
111 | |||
112 | /** |
||
113 | * Prepares token content for output to screen. |
||
114 | * |
||
115 | * Replaces invisible characters so they are visible. On non-Windows |
||
116 | * OSes it will also colour the invisible characters. |
||
117 | * |
||
118 | * @param string $content The content to prepare. |
||
119 | * @param string[] $exclude A list of characters to leave invisible. |
||
120 | * Can contain \r, \n, \t and a space. |
||
121 | * |
||
122 | * @return string |
||
123 | */ |
||
124 | public static function prepareForOutput($content, $exclude=array()) |
||
159 | |||
160 | |||
161 | /** |
||
162 | * Returns true if the specified string is in the camel caps format. |
||
163 | * |
||
164 | * @param string $string The string the verify. |
||
165 | * @param boolean $classFormat If true, check to see if the string is in the |
||
166 | * class format. Class format strings must start |
||
167 | * with a capital letter and contain no |
||
168 | * underscores. |
||
169 | * @param boolean $public If true, the first character in the string |
||
170 | * must be an a-z character. If false, the |
||
171 | * character must be an underscore. This |
||
172 | * argument is only applicable if $classFormat |
||
173 | * is false. |
||
174 | * @param boolean $strict If true, the string must not have two capital |
||
175 | * letters next to each other. If false, a |
||
176 | * relaxed camel caps policy is used to allow |
||
177 | * for acronyms. |
||
178 | * |
||
179 | * @return boolean |
||
180 | */ |
||
181 | public static function isCamelCaps( |
||
244 | |||
245 | |||
246 | /** |
||
247 | * Returns true if the specified string is in the underscore caps format. |
||
248 | * |
||
249 | * @param string $string The string to verify. |
||
250 | * |
||
251 | * @return boolean |
||
252 | */ |
||
253 | public static function isUnderscoreName($string) |
||
282 | |||
283 | |||
284 | /** |
||
285 | * Returns a valid variable type for param/var tag. |
||
286 | * |
||
287 | * If type is not one of the standard type, it must be a custom type. |
||
288 | * Returns the correct type name suggestion if type name is invalid. |
||
289 | * |
||
290 | * @param string $varType The variable type to process. |
||
291 | * |
||
292 | * @return string |
||
293 | */ |
||
294 | public static function suggestType($varType) |
||
352 | |||
353 | |||
354 | /** |
||
355 | * Given a sniff class name, returns the code for the sniff. |
||
356 | * |
||
357 | * @param string $sniffClass The fully qualified sniff class name. |
||
358 | * |
||
359 | * @return string |
||
360 | */ |
||
361 | public static function getSniffCode($sniffClass) |
||
381 | |||
382 | |||
383 | }//end class |
||
384 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.