| Total Complexity | 51 |
| Total Lines | 381 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
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.
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 |
||
| 5 | class Request |
||
| 6 | { |
||
| 7 | private $fullUrl; |
||
| 8 | |||
| 9 | private $httpMethod; |
||
| 10 | |||
| 11 | private $data = []; |
||
| 12 | |||
| 13 | private $queryStrings; |
||
| 14 | |||
| 15 | private $params; |
||
| 16 | |||
| 17 | private $headers; |
||
| 18 | |||
| 19 | public function __construct( |
||
| 39 | } |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Method responsible for bringing |
||
| 44 | * all request headers. |
||
| 45 | * |
||
| 46 | * @return array |
||
| 47 | */ |
||
| 48 | protected function resolveHeaders() |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Method responsible for returning the |
||
| 64 | * current route in parts. |
||
| 65 | * |
||
| 66 | * @return array|string|null |
||
| 67 | */ |
||
| 68 | protected function getParsedRoute(?String $data = null) |
||
| 69 | { |
||
| 70 | $parsedRoute = parse_url($this->fullUrl ?? "/"); |
||
| 71 | |||
| 72 | if (empty($data)) { |
||
| 73 | return $parsedRoute; |
||
| 74 | } |
||
| 75 | |||
| 76 | if (isset($parsedRoute[$data])) { |
||
| 77 | return $parsedRoute[$data]; |
||
| 78 | } |
||
| 79 | |||
| 80 | return null; |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Method responsible for returning |
||
| 85 | * the current route path. |
||
| 86 | * |
||
| 87 | * @return string |
||
| 88 | */ |
||
| 89 | public function path() |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Method responsible for returning |
||
| 98 | * the current route without query strings. |
||
| 99 | * |
||
| 100 | * @return null|string |
||
| 101 | */ |
||
| 102 | public function url() |
||
| 103 | { |
||
| 104 | $path = $this->getParsedRoute(); |
||
| 105 | |||
| 106 | if (!$path) return null; |
||
| 107 | |||
| 108 | return "{$path['scheme']}://{$path['host']}{$this->path()}"; |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Method responsible for returning |
||
| 113 | * the full current route (with query strings). |
||
| 114 | * |
||
| 115 | * @return null|string |
||
| 116 | */ |
||
| 117 | public function fullUrl() |
||
| 118 | { |
||
| 119 | $query = $this->getParsedRoute("query"); |
||
| 120 | |||
| 121 | return "{$this->url()}?{$query}"; |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Method responsible for returning the |
||
| 126 | * route's dynamic parameters. |
||
| 127 | * |
||
| 128 | * @return array |
||
| 129 | */ |
||
| 130 | public function getParams() |
||
| 131 | { |
||
| 132 | return $this->params; |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Method responsible for returning one or all |
||
| 137 | * data coming from the params query ($_GET). |
||
| 138 | * |
||
| 139 | * @param null|string $name = null |
||
| 140 | * @param null|string $asDefault = null |
||
| 141 | * |
||
| 142 | * @return array|string |
||
| 143 | */ |
||
| 144 | public function query(?String $name = null, ?String $asDefault = null) |
||
| 145 | { |
||
| 146 | if (!$name) { |
||
| 147 | return $this->queryStrings; |
||
| 148 | } |
||
| 149 | |||
| 150 | if (isset($this->queryStrings[$name])) { |
||
| 151 | return $this->queryStrings[$name]; |
||
| 152 | } |
||
| 153 | |||
| 154 | return $asDefault; |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Returns a property that does not exist in the class, |
||
| 159 | * usually they are indexes of the route array. Returns null |
||
| 160 | * if this index/property does not exist. |
||
| 161 | * |
||
| 162 | * @param string $data |
||
| 163 | * |
||
| 164 | * @return string|null|array |
||
| 165 | */ |
||
| 166 | public function __get(String $data) |
||
| 167 | { |
||
| 168 | if (isset($this->data[$data])) { |
||
| 169 | return $this->data[$data]; |
||
| 170 | } |
||
| 171 | |||
| 172 | if (isset($this->queryStrings[$data])) { |
||
| 173 | return $this->queryStrings[$data]; |
||
| 174 | } |
||
| 175 | |||
| 176 | if (isset($this->params[$data])) { |
||
| 177 | return $this->params[$data]; |
||
| 178 | } |
||
| 179 | |||
| 180 | return null; |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Method responsible for defining route data |
||
| 185 | * |
||
| 186 | * @param string $route |
||
| 187 | * @param array $routeParams |
||
| 188 | * |
||
| 189 | * @return void |
||
| 190 | */ |
||
| 191 | protected function resolveRouteData(String $route, array $routeParams): void |
||
| 192 | { |
||
| 193 | $params = $this->path(); |
||
| 194 | |||
| 195 | $diff = array_diff(explode('/', $params), explode('/', $route)); |
||
| 196 | |||
| 197 | $diff = array_values($diff); |
||
| 198 | |||
| 199 | if (!empty($diff)) { |
||
| 200 | foreach ($routeParams as $index => $param) { |
||
| 201 | if (!isset($diff[$index])) return; |
||
| 202 | |||
| 203 | if ($this->httpMethod != 'GET') { |
||
| 204 | $this->params[$param] = rawurldecode($diff[$index]); |
||
| 205 | continue; |
||
| 206 | } |
||
| 207 | |||
| 208 | $this->data[$param] = rawurldecode($diff[$index]); |
||
| 209 | } |
||
| 210 | } |
||
| 211 | |||
| 212 | if (empty($this->data)) { |
||
| 213 | $this->data = []; |
||
| 214 | } |
||
| 215 | |||
| 216 | if (empty($this->params)) { |
||
| 217 | $this->params = []; |
||
| 218 | } |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Method responsible for assigning and handling |
||
| 223 | * the data coming from the web form. |
||
| 224 | * |
||
| 225 | * @return void |
||
| 226 | */ |
||
| 227 | protected function setData() |
||
| 228 | { |
||
| 229 | $enableFormSpoofing = ["PUT", "PATCH", "DELETE"]; |
||
| 230 | |||
| 231 | $post = filter_input_array(INPUT_POST, FILTER_DEFAULT); |
||
| 232 | |||
| 233 | if (!empty($post['_method']) && in_array($post['_method'], $enableFormSpoofing)) { |
||
| 234 | $this->httpMethod = $post['_method']; |
||
| 235 | $this->data = $post; |
||
| 236 | |||
| 237 | unset($this->data["_method"]); |
||
| 238 | return; |
||
| 239 | } |
||
| 240 | if ($this->httpMethod == "POST") { |
||
| 241 | $this->data = filter_input_array(INPUT_POST, FILTER_DEFAULT); |
||
| 242 | |||
| 243 | unset($this->data["_method"]); |
||
| 244 | return; |
||
| 245 | } |
||
| 246 | |||
| 247 | if (in_array($this->httpMethod, $enableFormSpoofing) && !empty($_SERVER['CONTENT_LENGTH'])) { |
||
| 248 | parse_str(file_get_contents('php://input', false, null, 0, $_SERVER['CONTENT_LENGTH']), $putPatch); |
||
| 249 | $this->data = $putPatch; |
||
| 250 | |||
| 251 | unset($this->data["_method"]); |
||
| 252 | return; |
||
| 253 | } |
||
| 254 | |||
| 255 | $this->data = []; |
||
| 256 | return; |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Return the httpMethod. |
||
| 261 | * |
||
| 262 | * @return string |
||
| 263 | */ |
||
| 264 | public function getMethod() |
||
| 265 | { |
||
| 266 | return $this->httpMethod; |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Method responsible for checking if the current http method |
||
| 271 | * is the expected. |
||
| 272 | * |
||
| 273 | * @param string $expectedMethod |
||
| 274 | * |
||
| 275 | * @return bool |
||
| 276 | */ |
||
| 277 | public function isMethod(String $expectedMethod): bool |
||
| 278 | { |
||
| 279 | return $this->getMethod() === $expectedMethod; |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Method responsible for returning all request data. |
||
| 284 | * |
||
| 285 | * @param null|string $except = null |
||
| 286 | * |
||
| 287 | * @return array|null |
||
| 288 | */ |
||
| 289 | public function all(?String $except = null) |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Method responsible for returning all header data. |
||
| 313 | * |
||
| 314 | * @return array |
||
| 315 | */ |
||
| 316 | public function getHeaders() |
||
| 317 | { |
||
| 318 | return $this->headers; |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Method responsible for returning one header data or default value. |
||
| 323 | * |
||
| 324 | * @param string $header |
||
| 325 | * @param null|string $asDefault |
||
| 326 | * |
||
| 327 | * @return null|string |
||
| 328 | */ |
||
| 329 | public function header(String $header, ?String $asDefault = null) |
||
| 330 | { |
||
| 331 | if (isset($this->headers[$header])) { |
||
| 332 | return $this->headers[$header]; |
||
| 333 | } |
||
| 334 | |||
| 335 | return $asDefault; |
||
| 336 | } |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Method responsible for checking if there is one |
||
| 340 | * header in the request. |
||
| 341 | * |
||
| 342 | * @param string $header |
||
| 343 | * |
||
| 344 | * @return bool |
||
| 345 | */ |
||
| 346 | public function hasHeader(String $header) |
||
| 349 | } |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Method responsible for returning ip of client request. |
||
| 353 | * |
||
| 354 | * @return string |
||
| 355 | */ |
||
| 356 | public function ip() |
||
| 357 | { |
||
| 358 | if (array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER) && !empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { |
||
| 359 | if (strpos($_SERVER['HTTP_X_FORWARDED_FOR'], ',') > 0) { |
||
| 360 | $addr = explode(",", $_SERVER['HTTP_X_FORWARDED_FOR']); |
||
| 361 | return trim($addr[0]); |
||
| 362 | } else { |
||
| 363 | return $_SERVER['HTTP_X_FORWARDED_FOR']; |
||
| 364 | } |
||
| 365 | } else { |
||
| 366 | return $_SERVER['REMOTE_ADDR']; |
||
| 367 | } |
||
| 368 | } |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Return the bearer token. |
||
| 372 | * |
||
| 373 | * @return null|string |
||
| 374 | */ |
||
| 375 | public function bearerToken() |
||
| 386 | } |
||
| 387 | } |
||
| 388 |