Complex classes like Helper 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 Helper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
6 | class Helper |
||
7 | { |
||
8 | protected static $methods = array(); |
||
9 | |||
10 | public static function addMethod($ruleName, $callback) |
||
20 | |||
21 | public static function methodExists($name) |
||
25 | |||
26 | public static function __callStatic($name, $arguments) |
||
33 | |||
34 | public static function callback($value, $callback, $context = array()) |
||
42 | |||
43 | public static function required($value) |
||
47 | |||
48 | public static function truthy($value) |
||
52 | |||
53 | public static function falsy($value) |
||
57 | |||
58 | public static function number($value) |
||
62 | |||
63 | public static function integer($value) |
||
67 | |||
68 | public static function lessThan($value, $max) |
||
78 | |||
79 | public static function greaterThan($value, $min) |
||
89 | |||
90 | public static function between($value, $min, $max) |
||
101 | |||
102 | public static function exactly($value, $otherValue) |
||
106 | |||
107 | public static function not($value, $otherValue) |
||
111 | |||
112 | public static function alpha($value) |
||
118 | |||
119 | public static function alphanumeric($value) |
||
125 | |||
126 | public static function alphanumhyphen($value) |
||
132 | |||
133 | public static function minLength($value, $min) |
||
143 | |||
144 | public static function maxLength($value, $max) |
||
154 | |||
155 | public static function length($value, $min, $max) |
||
166 | |||
167 | public static function setMinSize($value, $min) |
||
177 | |||
178 | public static function setMaxSize($value, $max) |
||
188 | |||
189 | public static function setSize($value, $min, $max) |
||
200 | |||
201 | public static function inList($value, $values) |
||
211 | |||
212 | public static function notInList($value, $values) |
||
222 | |||
223 | public static function regex($value, $pattern) |
||
233 | |||
234 | public static function notRegex($value, $pattern) |
||
244 | |||
245 | public static function equalTo($value, $otherElementOrValue, $context = null) |
||
253 | |||
254 | public static function date($value, $format = 'Y-m-d') |
||
264 | |||
265 | public static function dateTime($value, $format = 'Y-m-d H:i:s') |
||
275 | |||
276 | public static function time($value, $format = 'H:i:s') |
||
286 | |||
287 | public static function website($value) |
||
293 | |||
294 | public static function url($value) |
||
300 | |||
301 | /** |
||
302 | * Test if a variable is a valid IP address |
||
303 | * |
||
304 | * @param string $value |
||
305 | * |
||
306 | * @return bool |
||
307 | */ |
||
308 | public static function ipAddress($value) |
||
314 | |||
315 | public static function email($value) |
||
321 | |||
322 | /** |
||
323 | * Test if a variable is a full name |
||
324 | * Criterias: at least 6 characters, 2 words |
||
325 | * |
||
326 | * @param mixed $value |
||
327 | * |
||
328 | * @return bool |
||
329 | */ |
||
330 | public static function fullName($value) |
||
336 | |||
337 | /** |
||
338 | * Test if the domain of an email address is available |
||
339 | * |
||
340 | * @param string $value |
||
341 | * |
||
342 | * @return bool |
||
343 | */ |
||
344 | public static function emailDomain($value) |
||
350 | } |
||
351 |