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 | public static $CUSTOM_SESSION = null; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Reset the internal arrays to default values. |
||
| 25 | */ |
||
| 26 | 1 | public static function reset_all_custom_arrays() |
|
| 34 | |||
| 35 | /** |
||
| 36 | * Get the value from the HTTP POST return the $default_value. |
||
| 37 | * @param string $key The form field's name to search in the $_POST array for. |
||
| 38 | * @param mixed $default_value Optional. If the $key cannot be found the value to return. Default null. |
||
| 39 | * @return mixed The value of the HTTP POST for the given $key or the $default. |
||
| 40 | */ |
||
| 41 | 1 | public static function get_post_value( $key, $default_value = '' ) |
|
| 45 | |||
| 46 | /** |
||
| 47 | * Get the value from the HTTP GET return the $default_value. |
||
| 48 | * @param string $key The form field's name to search in the $_GET array for. |
||
| 49 | * @param mixed $default_value Optional. If the $key cannot be found the value to return. Default null. |
||
| 50 | * @return mixed The value of the HTTP GET for the given $key or the $default. |
||
| 51 | */ |
||
| 52 | 1 | public static function get_get_value( $key, $default_value = '' ) |
|
| 56 | |||
| 57 | /** |
||
| 58 | * Get the value from the HTTP COOKIE return the $default_value. |
||
| 59 | * @param string $key The form field's name to search in the $_COOKIE array for. |
||
| 60 | * @param mixed $default_value Optional. If the $key cannot be found the value to return. Default null. |
||
| 61 | * @return mixed The value of the HTTP COOKIE for the given $key or the $default. |
||
| 62 | */ |
||
| 63 | 1 | public static function get_cookie_value( $key, $default_value = '' ) |
|
| 67 | |||
| 68 | /** |
||
| 69 | * Get the value from the HTTP SERVER return the $default_value. |
||
| 70 | * @param string $key The form field's name to search in the $_SERVER array for. |
||
| 71 | * @param mixed $default_value Optional. If the $key cannot be found the value to return. Default null. |
||
| 72 | * @return mixed The value of the HTTP SERVER for the given $key or the $default. |
||
| 73 | */ |
||
| 74 | 1 | public static function get_server_value( $key, $default_value = '' ) |
|
| 78 | |||
| 79 | /** |
||
| 80 | * Get the value from the SESSION return the $default_value. |
||
| 81 | * @param string $key The form field's name to search in the $_SESSION array for. |
||
| 82 | * @param mixed $default_value Optional. If the $key cannot be found the value to return. Default null. |
||
| 83 | * @return mixed The value of the SESSION for the given $key or the $default. |
||
| 84 | */ |
||
| 85 | public static function get_session_value( $key, $default_value = '' ) |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Get the first non-null value from the supplied list of sources. |
||
| 92 | * |
||
| 93 | * @param string $key The form field's name to search in the each source array for. |
||
| 94 | * @param array $sources Array of sources (GET, POST, SERVER, or COOKIE) in the order to check. |
||
| 95 | * @param mixed $default_value Optional. If the $key cannot be found the value to return. Default null. |
||
| 96 | * @return mixed The value of the source for the given $key or the $default. |
||
| 97 | */ |
||
| 98 | 3 | public static function get_value_multiple_sources( $key, array $sources, $default_value = null ) |
|
| 111 | |||
| 112 | /** |
||
| 113 | * Get the value from the HTTP POST as an integer or return the $default_value. |
||
| 114 | * @param string $key The form field's name to search in the $_POST array for. |
||
| 115 | * @param integer|mixed $default_value Optional. If the $key cannot be found the value to return. Default null. |
||
| 116 | * @return integer|mixed The value of the HTTP POST for the given $key or the $default. |
||
| 117 | */ |
||
| 118 | 1 | public static function get_post_value_int( $key, $default_value = null ) |
|
| 122 | |||
| 123 | /** |
||
| 124 | * Get the value from the HTTP GET as an integer or return the $default_value. |
||
| 125 | * @param string $key The form field's name to search in the $_GET array for. |
||
| 126 | * @param integer|mixed $default_value Optional. If the $key cannot be found the value to return. Default null. |
||
| 127 | * @return integer|mixed The value of the HTTP GET for the given $key or the $default. |
||
| 128 | */ |
||
| 129 | 1 | public static function get_get_value_int( $key, $default_value = null ) |
|
| 133 | |||
| 134 | /** |
||
| 135 | * Get the value from the HTTP COOKIE as an integer or return the $default_value. |
||
| 136 | * @param string $key The form field's name to search in the $_COOKIE array for. |
||
| 137 | * @param integer|mixed $default_value Optional. If the $key cannot be found the value to return. Default null. |
||
| 138 | * @return integer|mixed The value of the HTTP COOKIE for the given $key or the $default. |
||
| 139 | */ |
||
| 140 | 1 | public static function get_cookie_value_int( $key, $default_value = null ) |
|
| 144 | |||
| 145 | /** |
||
| 146 | * Get the value from the HTTP SERVER as an integer or return the $default_value. |
||
| 147 | * @param string $key The form field's name to search in the $_SERVER array for. |
||
| 148 | * @param integer|mixed $default_value Optional. If the $key cannot be found the value to return. Default null. |
||
| 149 | * @return integer|mixed The value of the HTTP SERVER for the given $key or the $default. |
||
| 150 | */ |
||
| 151 | 1 | public static function get_server_value_int( $key, $default_value = null ) |
|
| 155 | |||
| 156 | /** |
||
| 157 | * Get the value from the SESSION as an integer or return the $default_value. |
||
| 158 | * @param string $key The form field's name to search in the $_SESSION array for. |
||
| 159 | * @param integer|mixed $default_value Optional. If the $key cannot be found the value to return. Default null. |
||
| 160 | * @return integer|mixed The value of the SESSION for the given $key or the $default. |
||
| 161 | */ |
||
| 162 | public static function get_session_value_int( $key, $default_value = null ) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Get the value from the supplied request as an integer or return the $default_value. |
||
| 169 | * @param string $request_method The server method to use, one of GET, POST, SERVER or COOKIE. |
||
| 170 | * @param string $key The form field's name to search in the $_SERVER array for. |
||
| 171 | * @param integer|mixed $default_value Optional. If the $key cannot be found the value to return. Default null. |
||
| 172 | * @return integer|mixed The value of the request method for the given $key or the $default. |
||
| 173 | */ |
||
| 174 | 4 | public static function get_request_value_int( $request_method, $key, $default_value = null ) |
|
| 184 | |||
| 185 | /** |
||
| 186 | * Get the value from the supplied request or return the $default_value. |
||
| 187 | * @param string $request_method The server method to use, one of GET, POST, SERVER or COOKIE. |
||
| 188 | * @param string $key The form field's name to search in the $_SERVER array for. |
||
| 189 | * @param integer|mixed $default_value Optional. If the $key cannot be found the value to return. Default null. |
||
| 190 | * @return integer|mixed The value of the request method for the given $key or the $default. |
||
| 191 | */ |
||
| 192 | 4 | public static function get_request_value( $request_method, $key, $default_value = null ) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Get the current request array or mock array if set. |
||
| 213 | * |
||
| 214 | * @param string The server method to use, one of GET, POST, SERVER or COOKIE. |
||
| 215 | * @return array|null The requested array or null. |
||
| 216 | */ |
||
| 217 | 5 | public static function get_request_object( $request_method ) |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Test if we're in a certain type of HTTP request. |
||
| 263 | * |
||
| 264 | * @param string $method The server method to test for. Generally one of GET, POST, HEAD, PUT, DELETE. |
||
| 265 | * @return boolean Returns true if the REQUEST_METHOD server variable is set to the supplied $method, otherwise false. |
||
| 266 | */ |
||
| 267 | 1 | public static function is_request_method( $method ) |
|
| 271 | |||
| 272 | /** |
||
| 273 | * Check to see if we're in a post. |
||
| 274 | * |
||
| 275 | * Unit tests were failing because REQUEST_METHOD wasn't always being set. This should be used |
||
| 276 | * for all POST checks. |
||
| 277 | * |
||
| 278 | * \Vendi\Forms\utils::is_post() |
||
| 279 | * |
||
| 280 | * @return boolean Returns true if the REQUEST_METHOD server variable is set to POST, otherwise false. |
||
| 281 | */ |
||
| 282 | 1 | public static function is_post( ) |
|
| 286 | |||
| 287 | /** |
||
| 288 | * Test if the given $input can be converted to an int excluding booleans. |
||
| 289 | * |
||
| 290 | * \Vendi\Forms\utils::is_integer_like( value ) |
||
| 291 | * |
||
| 292 | * @param mixed $input The value to test. |
||
| 293 | * @return boolean True if $input is an integer or a string that contains only digits possibly starting with a dash. |
||
| 294 | */ |
||
| 295 | 1 | public static function is_integer_like( $input ) |
|
| 306 | |||
| 307 | } |
||
| 308 |