Complex classes like Request 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 Request, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 39 | class Request extends AbstractProxy |
||
| 40 | { |
||
| 41 | /** |
||
| 42 | * @const string HTTP methods |
||
| 43 | */ |
||
| 44 | const METHOD_OPTIONS = 'OPTIONS'; |
||
| 45 | const METHOD_GET = 'GET'; |
||
| 46 | const METHOD_HEAD = 'HEAD'; |
||
| 47 | const METHOD_PATCH = 'PATCH'; |
||
| 48 | const METHOD_POST = 'POST'; |
||
| 49 | const METHOD_PUT = 'PUT'; |
||
| 50 | const METHOD_DELETE = 'DELETE'; |
||
| 51 | const METHOD_TRACE = 'TRACE'; |
||
| 52 | const METHOD_CONNECT = 'CONNECT'; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @const string HTTP content types |
||
| 56 | */ |
||
| 57 | const TYPE_ANY = '*/*'; |
||
| 58 | const TYPE_HTML = 'text/html'; |
||
| 59 | const TYPE_JSON = 'application/json'; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Init instance |
||
| 63 | * |
||
| 64 | * @throws ComponentException |
||
| 65 | */ |
||
| 66 | protected static function initInstance() |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Retrieve a member of the $_SERVER super global |
||
| 73 | * |
||
| 74 | * If no $key is passed, returns the entire $_SERVER array. |
||
| 75 | * |
||
| 76 | * @param string $key |
||
| 77 | * @param string $default Default value to use if key not found |
||
| 78 | * @return string Returns null if key does not exist |
||
| 79 | */ |
||
| 80 | 4 | public static function getServer($key = null, $default = null) |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Retrieve a member of the $_COOKIE super global |
||
| 87 | * |
||
| 88 | * If no $key is passed, returns the entire $_COOKIE array. |
||
| 89 | * |
||
| 90 | * @param string $key |
||
| 91 | * @param string $default Default value to use if key not found |
||
| 92 | * @return string Returns null if key does not exist |
||
| 93 | */ |
||
| 94 | public static function getCookie($key = null, $default = null) |
||
| 98 | /** |
||
| 99 | * Retrieve a member of the $_ENV super global |
||
| 100 | * |
||
| 101 | * If no $key is passed, returns the entire $_ENV array. |
||
| 102 | * |
||
| 103 | * @param string $key |
||
| 104 | * @param string $default Default value to use if key not found |
||
| 105 | * @return string Returns null if key does not exist |
||
| 106 | */ |
||
| 107 | public static function getEnv($key = null, $default = null) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Search for a header value |
||
| 114 | * |
||
| 115 | * @param string $header |
||
| 116 | * @param mixed $default |
||
| 117 | * @return string |
||
| 118 | */ |
||
| 119 | 2 | public static function getHeader($header, $default = null) |
|
| 123 | |||
| 124 | /** |
||
| 125 | * Access values contained in the superglobals as public members |
||
| 126 | * Order of precedence: 1. GET, 2. POST, 3. COOKIE, 4. SERVER, 5. ENV |
||
| 127 | * |
||
| 128 | * @param string $key |
||
| 129 | * @param null $default |
||
| 130 | * @return mixed |
||
| 131 | * @link http://msdn.microsoft.com/en-us/library/system.web.httprequest.item.aspx |
||
| 132 | */ |
||
| 133 | 40 | public static function getParam($key, $default = null) |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Get all params from GET and POST or PUT |
||
| 153 | * |
||
| 154 | * @return array |
||
| 155 | */ |
||
| 156 | 3 | public static function getParams() |
|
| 163 | |||
| 164 | /** |
||
| 165 | * Get uploaded file |
||
| 166 | * |
||
| 167 | * @param string $name |
||
| 168 | * @return \Zend\Diactoros\UploadedFile |
||
| 169 | */ |
||
| 170 | public static function getFile($name) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Get the client's IP address |
||
| 177 | * |
||
| 178 | * @param bool $checkProxy |
||
| 179 | * @return string |
||
| 180 | */ |
||
| 181 | public static function getClientIp($checkProxy = true) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Get module |
||
| 197 | * |
||
| 198 | * @return string |
||
| 199 | */ |
||
| 200 | 38 | public static function getModule() |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Get controller |
||
| 207 | * |
||
| 208 | * @return string |
||
| 209 | */ |
||
| 210 | 38 | public static function getController() |
|
| 214 | |||
| 215 | /** |
||
| 216 | * Get method |
||
| 217 | * |
||
| 218 | * @return string |
||
| 219 | */ |
||
| 220 | public static function getMethod() |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Get Accept MIME Type |
||
| 227 | * |
||
| 228 | * @todo: refactoring this method, accept types should be stored in static? variable |
||
| 229 | * @param array $allowTypes |
||
| 230 | * @return string |
||
| 231 | */ |
||
| 232 | 2 | public static function getAccept($allowTypes = []) |
|
| 286 | |||
| 287 | /** |
||
| 288 | * Check CLI |
||
| 289 | * |
||
| 290 | * @return bool |
||
| 291 | */ |
||
| 292 | public static function isCli() |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Check HTTP |
||
| 299 | * |
||
| 300 | * @return bool |
||
| 301 | */ |
||
| 302 | 1 | public static function isHttp() |
|
| 306 | |||
| 307 | /** |
||
| 308 | * Is this a GET method request? |
||
| 309 | * |
||
| 310 | * @return bool |
||
| 311 | */ |
||
| 312 | public static function isGet() |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Is this a POST method request? |
||
| 319 | * |
||
| 320 | * @return bool |
||
| 321 | */ |
||
| 322 | public static function isPost() |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Is this a PUT method request? |
||
| 329 | * |
||
| 330 | * @return bool |
||
| 331 | */ |
||
| 332 | public static function isPut() |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Is this a DELETE method request? |
||
| 339 | * |
||
| 340 | * @return bool |
||
| 341 | */ |
||
| 342 | public static function isDelete() |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Is the request a Javascript XMLHttpRequest? |
||
| 349 | * |
||
| 350 | * Should work with Prototype/Script.aculo.us, possibly others. |
||
| 351 | * |
||
| 352 | * @return bool |
||
| 353 | */ |
||
| 354 | 2 | public static function isXmlHttpRequest() |
|
| 358 | } |
||
| 359 |
This error can happen if you refactor code and forget to move the variable initialization.
Let’s take a look at a simple example:
The above code is perfectly fine. Now imagine that we re-order the statements:
In that case,
$xwould be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.