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 |
||
| 12 | class Request{ |
||
|
|
|||
| 13 | |||
| 14 | /** |
||
| 15 | * Set a list of trusted hosts patterns. |
||
| 16 | * |
||
| 17 | * @var array |
||
| 18 | */ |
||
| 19 | private static $trustedHostPatterns = []; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Array of parameters parsed from the URL. |
||
| 23 | * |
||
| 24 | * @var array |
||
| 25 | */ |
||
| 26 | public $params = [ |
||
| 27 | "controller" => null, "action" => null, "args" => null |
||
| 28 | ]; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Array of POST data as well as uploaded files. |
||
| 32 | * |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | public $data = []; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Array of querystring arguments |
||
| 39 | * |
||
| 40 | * @var array |
||
| 41 | */ |
||
| 42 | public $query = []; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * The URL used to make the request. |
||
| 46 | * |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | public $url = null; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Constructor |
||
| 53 | * Create a new request from PHP superglobals. |
||
| 54 | * |
||
| 55 | * @param array $config user provided config |
||
| 56 | */ |
||
| 57 | public function __construct($config = []){ |
||
| 64 | |||
| 65 | /** |
||
| 66 | * merge post and files data |
||
| 67 | * You shouldn't have two fields with the same 'name' attribute in $_POST & $_FILES |
||
| 68 | * |
||
| 69 | * @param array $post |
||
| 70 | * @param array $files |
||
| 71 | * @return array the merged array |
||
| 72 | */ |
||
| 73 | private function mergeData(array $post, array $files){ |
||
| 79 | |||
| 80 | /** |
||
| 81 | * count fields in $this->data and optionally exclude some fields |
||
| 82 | * |
||
| 83 | * @param array $exclude |
||
| 84 | * @return mixed |
||
| 85 | */ |
||
| 86 | public function countData(array $exclude = []){ |
||
| 95 | |||
| 96 | /** |
||
| 97 | * safer and better access to $this->data |
||
| 98 | * |
||
| 99 | * @param string $key |
||
| 100 | * @return mixed |
||
| 101 | */ |
||
| 102 | public function data($key){ |
||
| 105 | |||
| 106 | /** |
||
| 107 | * safer and better access to $this->query |
||
| 108 | * |
||
| 109 | * @param string $key |
||
| 110 | * @return mixed |
||
| 111 | */ |
||
| 112 | public function query($key){ |
||
| 115 | |||
| 116 | /** |
||
| 117 | * safer and better access to $this->params |
||
| 118 | * |
||
| 119 | * @param string $key |
||
| 120 | * @return mixed |
||
| 121 | */ |
||
| 122 | public function param($key){ |
||
| 125 | |||
| 126 | /** |
||
| 127 | * detect if request is Ajax |
||
| 128 | * |
||
| 129 | * @return boolean |
||
| 130 | */ |
||
| 131 | public function isAjax(){ |
||
| 137 | |||
| 138 | /** |
||
| 139 | * detect if request is POST request |
||
| 140 | * |
||
| 141 | * @return boolean |
||
| 142 | */ |
||
| 143 | public function isPost(){ |
||
| 146 | |||
| 147 | /** |
||
| 148 | * detect if request is GET request |
||
| 149 | * |
||
| 150 | * @return boolean |
||
| 151 | */ |
||
| 152 | public function isGet(){ |
||
| 155 | |||
| 156 | /** |
||
| 157 | * detect if request over secured connection(SSL) |
||
| 158 | * |
||
| 159 | * @return boolean |
||
| 160 | * |
||
| 161 | */ |
||
| 162 | public function isSSL(){ |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Add parameters to $this->params. |
||
| 168 | * |
||
| 169 | * @param array $params |
||
| 170 | * @return Request |
||
| 171 | */ |
||
| 172 | public function addParams(array $params){ |
||
| 176 | |||
| 177 | /** |
||
| 178 | * get content length |
||
| 179 | * |
||
| 180 | * @return integer |
||
| 181 | */ |
||
| 182 | public function contentLength(){ |
||
| 185 | |||
| 186 | /** |
||
| 187 | * checks if there is overflow in POST & FILES data. |
||
| 188 | * This will lead to having both $_POST & $_FILES = empty array. |
||
| 189 | * |
||
| 190 | * @return bool |
||
| 191 | */ |
||
| 192 | public function dataSizeOverflow(){ |
||
| 196 | |||
| 197 | /** |
||
| 198 | * get the current uri of the request |
||
| 199 | * |
||
| 200 | * @return string|null |
||
| 201 | */ |
||
| 202 | public function uri(){ |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Get the current host of the request |
||
| 208 | * |
||
| 209 | * @return string |
||
| 210 | * @throws UnexpectedValueException if the hostname is invalid |
||
| 211 | */ |
||
| 212 | public function host(){ |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Get the name of the server host |
||
| 246 | * |
||
| 247 | * @return string|null |
||
| 248 | */ |
||
| 249 | public function name(){ |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Get the referer of this request. |
||
| 255 | * |
||
| 256 | * @return string|null |
||
| 257 | */ |
||
| 258 | public function referer(){ |
||
| 261 | |||
| 262 | /** |
||
| 263 | * get the client IP addresses. |
||
| 264 | * |
||
| 265 | * 'REMOTE_ADDR' is the most trusted one, |
||
| 266 | * otherwise you can use HTTP_CLIENT_IP, or HTTP_X_FORWARDED_FOR. |
||
| 267 | * |
||
| 268 | * @return string|null |
||
| 269 | */ |
||
| 270 | public function clientIp(){ |
||
| 273 | |||
| 274 | /** |
||
| 275 | * get the contents of the User Agent |
||
| 276 | * |
||
| 277 | * @return string|null |
||
| 278 | */ |
||
| 279 | public function userAgent(){ |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Gets the request's protocol. |
||
| 285 | * |
||
| 286 | * @return string |
||
| 287 | */ |
||
| 288 | public function protocol(){ |
||
| 289 | return $this->isSSL() ? 'https' : 'http'; |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Gets the protocol and HTTP host. |
||
| 294 | * |
||
| 295 | * @return string The protocol and the host |
||
| 296 | */ |
||
| 297 | public function getProtocolAndHost(){ |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Get the full URL for the request with the added query string parameters. |
||
| 303 | * |
||
| 304 | * @return string |
||
| 305 | */ |
||
| 306 | public function fullUrl(){ |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Get the full URL for the request without the protocol. |
||
| 329 | * |
||
| 330 | * It could be useful to force a specific protocol. |
||
| 331 | * |
||
| 332 | * @return string |
||
| 333 | */ |
||
| 334 | public function fullUrlWithoutProtocol(){ |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Returns the base URL. |
||
| 340 | * |
||
| 341 | * Examples: |
||
| 342 | * * http://localhost/ returns an empty string |
||
| 343 | * * http://localhost/miniphp/public/user returns miniphp |
||
| 344 | * * http://localhost/miniphp/posts/view/123 returns miniphp |
||
| 345 | * |
||
| 346 | * @return string |
||
| 347 | */ |
||
| 348 | public function getBaseUrl(){ |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Get the root URL for the application. |
||
| 355 | * |
||
| 356 | * @return string |
||
| 357 | */ |
||
| 358 | public function root(){ |
||
| 361 | } |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.