Complex classes like utils 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 utils, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class utils |
||
12 | { |
||
13 | public static $CUSTOM_POST = null; |
||
14 | |||
15 | public static $CUSTOM_GET = null; |
||
16 | |||
17 | public static $CUSTOM_COOKIE = null; |
||
18 | |||
19 | public static $CUSTOM_SERVER = null; |
||
20 | |||
21 | /** |
||
22 | * Reset the internal arrays to default values. |
||
23 | */ |
||
24 | public static function reset_all_custom_arrays() |
||
31 | |||
32 | /** |
||
33 | * Get the value from the HTTP POST return the $default_value. |
||
34 | * @param string $key The form field's name to search in the $_POST array for. |
||
35 | * @param mixed $default_value Optional. If the $key cannot be found the value to return. Default null. |
||
36 | * @return mixed The value of the HTTP POST for the given $key or the $default. |
||
37 | */ |
||
38 | public static function get_post_value( $key, $default_value = '' ) |
||
42 | |||
43 | /** |
||
44 | * Get the value from the HTTP GET return the $default_value. |
||
45 | * @param string $key The form field's name to search in the $_GET array for. |
||
46 | * @param mixed $default_value Optional. If the $key cannot be found the value to return. Default null. |
||
47 | * @return mixed The value of the HTTP GET for the given $key or the $default. |
||
48 | */ |
||
49 | public static function get_get_value( $key, $default_value = '' ) |
||
53 | |||
54 | /** |
||
55 | * Get the value from the HTTP COOKIE return the $default_value. |
||
56 | * @param string $key The form field's name to search in the $_COOKIE array for. |
||
57 | * @param mixed $default_value Optional. If the $key cannot be found the value to return. Default null. |
||
58 | * @return mixed The value of the HTTP COOKIE for the given $key or the $default. |
||
59 | */ |
||
60 | public static function get_cookie_value( $key, $default_value = '' ) |
||
64 | |||
65 | /** |
||
66 | * Get the value from the HTTP SERVER return the $default_value. |
||
67 | * @param string $key The form field's name to search in the $_SERVER array for. |
||
68 | * @param mixed $default_value Optional. If the $key cannot be found the value to return. Default null. |
||
69 | * @return mixed The value of the HTTP SERVER for the given $key or the $default. |
||
70 | */ |
||
71 | public static function get_server_value( $key, $default_value = '' ) |
||
75 | |||
76 | /** |
||
77 | * Get the first non-null value from the supplied list of sources. |
||
78 | * |
||
79 | * @param string $key The form field's name to search in the each source array for. |
||
80 | * @param array $sources Array of sources (GET, POST, SERVER, or COOKIE) in the order to check. |
||
81 | * @param mixed $default_value Optional. If the $key cannot be found the value to return. Default null. |
||
82 | * @return mixed The value of the source for the given $key or the $default. |
||
83 | */ |
||
84 | public static function get_value_multiple_sources( $key, array $sources, $default_value = null ) |
||
97 | |||
98 | /** |
||
99 | * Get the value from the HTTP POST as an integer or return the $default_value. |
||
100 | * @param string $key The form field's name to search in the $_POST array for. |
||
101 | * @param integer|mixed $default_value Optional. If the $key cannot be found the value to return. Default null. |
||
102 | * @return integer|mixed The value of the HTTP POST for the given $key or the $default. |
||
103 | */ |
||
104 | public static function get_post_value_int( $key, $default_value = null ) |
||
108 | |||
109 | /** |
||
110 | * Get the value from the HTTP GET as an integer or return the $default_value. |
||
111 | * @param string $key The form field's name to search in the $_GET array for. |
||
112 | * @param integer|mixed $default_value Optional. If the $key cannot be found the value to return. Default null. |
||
113 | * @return integer|mixed The value of the HTTP GET for the given $key or the $default. |
||
114 | */ |
||
115 | public static function get_get_value_int( $key, $default_value = null ) |
||
119 | |||
120 | /** |
||
121 | * Get the value from the HTTP COOKIE as an integer or return the $default_value. |
||
122 | * @param string $key The form field's name to search in the $_COOKIE array for. |
||
123 | * @param integer|mixed $default_value Optional. If the $key cannot be found the value to return. Default null. |
||
124 | * @return integer|mixed The value of the HTTP COOKIE for the given $key or the $default. |
||
125 | */ |
||
126 | public static function get_cookie_value_int( $key, $default_value = null ) |
||
130 | |||
131 | /** |
||
132 | * Get the value from the HTTP SERVER as an integer or return the $default_value. |
||
133 | * @param string $key The form field's name to search in the $_SERVER array for. |
||
134 | * @param integer|mixed $default_value Optional. If the $key cannot be found the value to return. Default null. |
||
135 | * @return integer|mixed The value of the HTTP SERVER for the given $key or the $default. |
||
136 | */ |
||
137 | public static function get_server_value_int( $key, $default_value = null ) |
||
141 | |||
142 | /** |
||
143 | * Get the value from the supplied request as an integer or return the $default_value. |
||
144 | * @param string $request_method The server method to use, one of GET, POST, SERVER or COOKIE. |
||
145 | * @param string $key The form field's name to search in the $_SERVER array for. |
||
146 | * @param integer|mixed $default_value Optional. If the $key cannot be found the value to return. Default null. |
||
147 | * @return integer|mixed The value of the request method for the given $key or the $default. |
||
148 | */ |
||
149 | public static function get_request_value_int( $request_method, $key, $default_value = null ) |
||
159 | |||
160 | /** |
||
161 | * Get the value from the supplied request or return the $default_value. |
||
162 | * @param string $request_method The server method to use, one of GET, POST, SERVER or COOKIE. |
||
163 | * @param string $key The form field's name to search in the $_SERVER array for. |
||
164 | * @param integer|mixed $default_value Optional. If the $key cannot be found the value to return. Default null. |
||
165 | * @return integer|mixed The value of the request method for the given $key or the $default. |
||
166 | */ |
||
167 | public static function get_request_value( $request_method, $key, $default_value = null ) |
||
185 | |||
186 | /** |
||
187 | * Get the current request array or mock array if set. |
||
188 | * |
||
189 | * @param string The server method to use, one of GET, POST, SERVER or COOKIE. |
||
190 | * @return arra|null The requested array or null. |
||
|
|||
191 | */ |
||
192 | public static function get_request_object( $request_method ) |
||
229 | |||
230 | /** |
||
231 | * Test if we're in a certain type of HTTP request. |
||
232 | * |
||
233 | * @param string $method The server method to test for. Generally one of GET, POST, HEAD, PUT, DELETE. |
||
234 | * @return boolean Returns true if the REQUEST_METHOD server variable is set to the supplied $method, otherwise false. |
||
235 | */ |
||
236 | public static function is_request_method( $method ) |
||
240 | |||
241 | /** |
||
242 | * Check to see if we're in a post. |
||
243 | * |
||
244 | * Unit tests were failing because REQUEST_METHOD wasn't always being set. This should be used |
||
245 | * for all POST checks. |
||
246 | * |
||
247 | * \Vendi\Forms\utils::is_post() |
||
248 | * |
||
249 | * @return boolean Returns true if the REQUEST_METHOD server variable is set to POST, otherwise false. |
||
250 | */ |
||
251 | public static function is_post( ) |
||
255 | |||
256 | /** |
||
257 | * Test if the given $input can be converted to an int excluding booleans. |
||
258 | * |
||
259 | * \Vendi\Forms\utils::is_integer_like( value ) |
||
260 | * |
||
261 | * @param mixed $input The value to test. |
||
262 | * @return boolean True if $input is an integer or a string that contains only digits possibly starting with a dash. |
||
263 | */ |
||
264 | public static function is_integer_like( $input ) |
||
275 | |||
276 | /** |
||
277 | * Convert the result of urlpieces back to a URL. |
||
278 | * |
||
279 | * @see http://php.net/manual/en/function.parse-url.php#106731 |
||
280 | * |
||
281 | * @param array $parsed_url An array created by urlpieces. |
||
282 | * @return string A URL string. |
||
283 | */ |
||
284 | public static function unparse_url( $parsed_url ) { |
||
300 | |||
301 | } |
||
302 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.If the return type contains the type array, this check recommends the use of a more specific type like
String[]
orarray<String>
.