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 |
||
| 37 | class Request extends AbstractProxy |
||
| 38 | { |
||
| 39 | /** |
||
| 40 | * @const string HTTP methods |
||
| 41 | */ |
||
| 42 | const METHOD_OPTIONS = 'OPTIONS'; |
||
| 43 | const METHOD_GET = 'GET'; |
||
| 44 | const METHOD_HEAD = 'HEAD'; |
||
| 45 | const METHOD_PATCH = 'PATCH'; |
||
| 46 | const METHOD_POST = 'POST'; |
||
| 47 | const METHOD_PUT = 'PUT'; |
||
| 48 | const METHOD_DELETE = 'DELETE'; |
||
| 49 | const METHOD_TRACE = 'TRACE'; |
||
| 50 | const METHOD_CONNECT = 'CONNECT'; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @const string HTTP content types |
||
| 54 | */ |
||
| 55 | const TYPE_ANY = '*/*'; |
||
| 56 | const TYPE_HTML = 'text/html'; |
||
| 57 | const TYPE_JSON = 'application/json'; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Init instance |
||
| 61 | * |
||
| 62 | * @throws ComponentException |
||
| 63 | */ |
||
| 64 | protected static function initInstance() |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Retrieve a member of the $_SERVER super global |
||
| 71 | * |
||
| 72 | * If no $key is passed, returns the entire $_SERVER array. |
||
| 73 | * |
||
| 74 | * @param string $key |
||
| 75 | * @param string $default Default value to use if key not found |
||
| 76 | * @return string Returns null if key does not exist |
||
| 77 | */ |
||
| 78 | 4 | public static function getServer($key = null, $default = null) |
|
| 82 | |||
| 83 | /** |
||
| 84 | * Retrieve a member of the $_COOKIE super global |
||
| 85 | * |
||
| 86 | * If no $key is passed, returns the entire $_COOKIE array. |
||
| 87 | * |
||
| 88 | * @param string $key |
||
| 89 | * @param string $default Default value to use if key not found |
||
| 90 | * @return string Returns null if key does not exist |
||
| 91 | */ |
||
| 92 | public static function getCookie($key = null, $default = null) |
||
| 96 | /** |
||
| 97 | * Retrieve a member of the $_ENV super global |
||
| 98 | * |
||
| 99 | * If no $key is passed, returns the entire $_ENV array. |
||
| 100 | * |
||
| 101 | * @param string $key |
||
| 102 | * @param string $default Default value to use if key not found |
||
| 103 | * @return string Returns null if key does not exist |
||
| 104 | */ |
||
| 105 | public static function getEnv($key = null, $default = null) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Search for a header value |
||
| 112 | * |
||
| 113 | * @param string $header |
||
| 114 | * @param mixed $default |
||
| 115 | * @return string |
||
| 116 | */ |
||
| 117 | 2 | public static function getHeader($header, $default = null) |
|
| 121 | |||
| 122 | /** |
||
| 123 | * Access values contained in the superglobals as public members |
||
| 124 | * Order of precedence: 1. GET, 2. POST, 3. COOKIE, 4. SERVER, 5. ENV |
||
| 125 | * |
||
| 126 | * @param string $key |
||
| 127 | * @param null $default |
||
| 128 | * @return mixed |
||
| 129 | * @link http://msdn.microsoft.com/en-us/library/system.web.httprequest.item.aspx |
||
| 130 | */ |
||
| 131 | 40 | public static function getParam($key, $default = null) |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Get all params from GET and POST or PUT |
||
| 151 | * |
||
| 152 | * @return array |
||
| 153 | */ |
||
| 154 | 3 | public static function getParams() |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Get uploaded file |
||
| 164 | * |
||
| 165 | * @param string $name |
||
| 166 | * @return \Zend\Diactoros\UploadedFile |
||
| 167 | */ |
||
| 168 | public static function getFile($name) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Get the client's IP address |
||
| 175 | * |
||
| 176 | * @param bool $checkProxy |
||
| 177 | * @return string |
||
| 178 | */ |
||
| 179 | public static function getClientIp($checkProxy = true) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Get module |
||
| 195 | * |
||
| 196 | * @return string |
||
| 197 | */ |
||
| 198 | 38 | public static function getModule() |
|
| 202 | |||
| 203 | /** |
||
| 204 | * Get controller |
||
| 205 | * |
||
| 206 | * @return string |
||
| 207 | */ |
||
| 208 | 38 | public static function getController() |
|
| 212 | |||
| 213 | /** |
||
| 214 | * Get method |
||
| 215 | * |
||
| 216 | * @return string |
||
| 217 | */ |
||
| 218 | public static function getMethod() |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Get Accept MIME Type |
||
| 225 | * |
||
| 226 | * @todo: refactoring this method, accept types should be stored in static? variable |
||
| 227 | * @param array $allowTypes |
||
| 228 | * @return string |
||
| 229 | */ |
||
| 230 | 2 | public static function getAccept($allowTypes = []) |
|
| 285 | |||
| 286 | /** |
||
| 287 | * Check CLI |
||
| 288 | * |
||
| 289 | * @return bool |
||
| 290 | */ |
||
| 291 | public static function isCli() |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Check HTTP |
||
| 298 | * |
||
| 299 | * @return bool |
||
| 300 | */ |
||
| 301 | 1 | public static function isHttp() |
|
| 305 | |||
| 306 | /** |
||
| 307 | * Is this a GET method request? |
||
| 308 | * |
||
| 309 | * @return bool |
||
| 310 | */ |
||
| 311 | public static function isGet() |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Is this a POST method request? |
||
| 318 | * |
||
| 319 | * @return bool |
||
| 320 | */ |
||
| 321 | public static function isPost() |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Is this a PUT method request? |
||
| 328 | * |
||
| 329 | * @return bool |
||
| 330 | */ |
||
| 331 | public static function isPut() |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Is this a DELETE method request? |
||
| 338 | * |
||
| 339 | * @return bool |
||
| 340 | */ |
||
| 341 | public static function isDelete() |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Is the request a Javascript XMLHttpRequest? |
||
| 348 | * |
||
| 349 | * Should work with Prototype/Script.aculo.us, possibly others. |
||
| 350 | * |
||
| 351 | * @return bool |
||
| 352 | */ |
||
| 353 | 2 | public static function isXmlHttpRequest() |
|
| 357 | } |
||
| 358 |
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.