Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
| 43 | class Request implements \ArrayAccess, \Countable, IRequest { |
||
| 44 | |||
| 45 | const USER_AGENT_IE = '/MSIE/'; |
||
| 46 | const USER_AGENT_IE_8 = '/MSIE 8.0/'; |
||
| 47 | // Android Chrome user agent: https://developers.google.com/chrome/mobile/docs/user-agent |
||
| 48 | const USER_AGENT_ANDROID_MOBILE_CHROME = '#Android.*Chrome/[.0-9]*#'; |
||
| 49 | const USER_AGENT_FREEBOX = '#^Mozilla/5\.0$#'; |
||
| 50 | const REGEX_LOCALHOST = '/^(127\.0\.0\.1|localhost)$/'; |
||
| 51 | |||
| 52 | protected $inputStream; |
||
| 53 | protected $content; |
||
| 54 | protected $items = array(); |
||
| 55 | protected $allowedKeys = array( |
||
| 56 | 'get', |
||
| 57 | 'post', |
||
| 58 | 'files', |
||
| 59 | 'server', |
||
| 60 | 'env', |
||
| 61 | 'cookies', |
||
| 62 | 'urlParams', |
||
| 63 | 'parameters', |
||
| 64 | 'method', |
||
| 65 | 'requesttoken', |
||
| 66 | ); |
||
| 67 | /** @var ISecureRandom */ |
||
| 68 | protected $secureRandom; |
||
| 69 | /** @var IConfig */ |
||
| 70 | protected $config; |
||
| 71 | /** @var string */ |
||
| 72 | protected $requestId = ''; |
||
| 73 | /** @var ICrypto */ |
||
| 74 | protected $crypto; |
||
| 75 | |||
| 76 | /** @var bool */ |
||
| 77 | protected $contentDecoded = false; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @param array $vars An associative array with the following optional values: |
||
| 81 | * - array 'urlParams' the parameters which were matched from the URL |
||
| 82 | * - array 'get' the $_GET array |
||
| 83 | * - array|string 'post' the $_POST array or JSON string |
||
| 84 | * - array 'files' the $_FILES array |
||
| 85 | * - array 'server' the $_SERVER array |
||
| 86 | * - array 'env' the $_ENV array |
||
| 87 | * - array 'cookies' the $_COOKIE array |
||
| 88 | * - string 'method' the request method (GET, POST etc) |
||
| 89 | * - string|false 'requesttoken' the requesttoken or false when not available |
||
| 90 | * @param ISecureRandom $secureRandom |
||
| 91 | * @param IConfig $config |
||
| 92 | * @param string $stream |
||
| 93 | * @see http://www.php.net/manual/en/reserved.variables.php |
||
| 94 | */ |
||
| 95 | 161 | public function __construct(array $vars=array(), |
|
| 122 | /** |
||
| 123 | * @param array $parameters |
||
| 124 | */ |
||
| 125 | 4 | public function setUrlParameters(array $parameters) { |
|
| 132 | |||
| 133 | /** |
||
| 134 | * Countable method |
||
| 135 | * @return int |
||
| 136 | */ |
||
| 137 | 2 | public function count() { |
|
| 140 | |||
| 141 | /** |
||
| 142 | * ArrayAccess methods |
||
| 143 | * |
||
| 144 | * Gives access to the combined GET, POST and urlParams arrays |
||
| 145 | * |
||
| 146 | * Examples: |
||
| 147 | * |
||
| 148 | * $var = $request['myvar']; |
||
| 149 | * |
||
| 150 | * or |
||
| 151 | * |
||
| 152 | * if(!isset($request['myvar']) { |
||
| 153 | * // Do something |
||
| 154 | * } |
||
| 155 | * |
||
| 156 | * $request['myvar'] = 'something'; // This throws an exception. |
||
| 157 | * |
||
| 158 | * @param string $offset The key to lookup |
||
| 159 | * @return boolean |
||
| 160 | */ |
||
| 161 | 2 | public function offsetExists($offset) { |
|
| 164 | |||
| 165 | /** |
||
| 166 | * @see offsetExists |
||
| 167 | */ |
||
| 168 | 3 | public function offsetGet($offset) { |
|
| 173 | |||
| 174 | /** |
||
| 175 | * @see offsetExists |
||
| 176 | */ |
||
| 177 | 1 | public function offsetSet($offset, $value) { |
|
| 180 | |||
| 181 | /** |
||
| 182 | * @see offsetExists |
||
| 183 | */ |
||
| 184 | public function offsetUnset($offset) { |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Magic property accessors |
||
| 190 | * @param string $name |
||
| 191 | * @param mixed $value |
||
| 192 | */ |
||
| 193 | 1 | public function __set($name, $value) { |
|
| 196 | |||
| 197 | /** |
||
| 198 | * Access request variables by method and name. |
||
| 199 | * Examples: |
||
| 200 | * |
||
| 201 | * $request->post['myvar']; // Only look for POST variables |
||
| 202 | * $request->myvar; or $request->{'myvar'}; or $request->{$myvar} |
||
| 203 | * Looks in the combined GET, POST and urlParams array. |
||
| 204 | * |
||
| 205 | * If you access e.g. ->post but the current HTTP request method |
||
| 206 | * is GET a \LogicException will be thrown. |
||
| 207 | * |
||
| 208 | * @param string $name The key to look for. |
||
| 209 | * @throws \LogicException |
||
| 210 | * @return mixed|null |
||
| 211 | */ |
||
| 212 | 297 | public function __get($name) { |
|
| 240 | |||
| 241 | /** |
||
| 242 | * @param string $name |
||
| 243 | * @return bool |
||
| 244 | */ |
||
| 245 | 1 | public function __isset($name) { |
|
| 248 | |||
| 249 | /** |
||
| 250 | * @param string $id |
||
| 251 | */ |
||
| 252 | public function __unset($id) { |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Returns the value for a specific http header. |
||
| 258 | * |
||
| 259 | * This method returns null if the header did not exist. |
||
| 260 | * |
||
| 261 | * @param string $name |
||
| 262 | * @return string |
||
| 263 | */ |
||
| 264 | 20 | public function getHeader($name) { |
|
| 285 | |||
| 286 | /** |
||
| 287 | * Lets you access post and get parameters by the index |
||
| 288 | * In case of json requests the encoded json body is accessed |
||
| 289 | * |
||
| 290 | * @param string $key the key which you want to access in the URL Parameter |
||
| 291 | * placeholder, $_POST or $_GET array. |
||
| 292 | * The priority how they're returned is the following: |
||
| 293 | * 1. URL parameters |
||
| 294 | * 2. POST parameters |
||
| 295 | * 3. GET parameters |
||
| 296 | * @param mixed $default If the key is not found, this value will be returned |
||
| 297 | * @return mixed the content of the array |
||
| 298 | */ |
||
| 299 | 11 | public function getParam($key, $default = null) { |
|
| 304 | |||
| 305 | /** |
||
| 306 | * Returns all params that were received, be it from the request |
||
| 307 | * (as GET or POST) or throuh the URL by the route |
||
| 308 | * @return array the array with all parameters |
||
| 309 | */ |
||
| 310 | 2 | public function getParams() { |
|
| 313 | |||
| 314 | /** |
||
| 315 | * Returns the method of the request |
||
| 316 | * @return string the method of the request (POST, GET, etc) |
||
| 317 | */ |
||
| 318 | 1 | public function getMethod() { |
|
| 321 | |||
| 322 | /** |
||
| 323 | * Shortcut for accessing an uploaded file through the $_FILES array |
||
| 324 | * @param string $key the key that will be taken from the $_FILES array |
||
| 325 | * @return array the file in the $_FILES element |
||
| 326 | */ |
||
| 327 | 1 | public function getUploadedFile($key) { |
|
| 330 | |||
| 331 | /** |
||
| 332 | * Shortcut for getting env variables |
||
| 333 | * @param string $key the key that will be taken from the $_ENV array |
||
| 334 | * @return array the value in the $_ENV element |
||
| 335 | */ |
||
| 336 | 1 | public function getEnv($key) { |
|
| 339 | |||
| 340 | /** |
||
| 341 | * Shortcut for getting cookie variables |
||
| 342 | * @param string $key the key that will be taken from the $_COOKIE array |
||
| 343 | * @return array the value in the $_COOKIE element |
||
| 344 | */ |
||
| 345 | 1 | public function getCookie($key) { |
|
| 348 | |||
| 349 | /** |
||
| 350 | * Returns the request body content. |
||
| 351 | * |
||
| 352 | * If the HTTP request method is PUT and the body |
||
| 353 | * not application/x-www-form-urlencoded or application/json a stream |
||
| 354 | * resource is returned, otherwise an array. |
||
| 355 | * |
||
| 356 | * @return array|string|resource The request body content or a resource to read the body stream. |
||
| 357 | * |
||
| 358 | * @throws \LogicException |
||
| 359 | */ |
||
| 360 | 19 | protected function getContent() { |
|
| 379 | |||
| 380 | /** |
||
| 381 | * Attempt to decode the content and populate parameters |
||
| 382 | */ |
||
| 383 | 18 | protected function decodeContent() { |
|
| 416 | |||
| 417 | |||
| 418 | /** |
||
| 419 | * Checks if the CSRF check was correct |
||
| 420 | * @return bool true if CSRF check passed |
||
| 421 | * @see OC_Util::callRegister() |
||
| 422 | */ |
||
| 423 | 8 | public function passesCSRFCheck() { |
|
| 456 | |||
| 457 | /** |
||
| 458 | * Returns an ID for the request, value is not guaranteed to be unique and is mostly meant for logging |
||
| 459 | * If `mod_unique_id` is installed this value will be taken. |
||
| 460 | * @return string |
||
| 461 | */ |
||
| 462 | 103 | public function getId() { |
|
| 473 | |||
| 474 | /** |
||
| 475 | * Returns the remote address, if the connection came from a trusted proxy |
||
| 476 | * and `forwarded_for_headers` has been configured then the IP address |
||
| 477 | * specified in this header will be returned instead. |
||
| 478 | * Do always use this instead of $_SERVER['REMOTE_ADDR'] |
||
| 479 | * @return string IP address |
||
| 480 | */ |
||
| 481 | 104 | public function getRemoteAddress() { |
|
| 505 | |||
| 506 | /** |
||
| 507 | * Check overwrite condition |
||
| 508 | * @param string $type |
||
| 509 | * @return bool |
||
| 510 | */ |
||
| 511 | 8 | private function isOverwriteCondition($type = '') { |
|
| 517 | |||
| 518 | /** |
||
| 519 | * Returns the server protocol. It respects one or more reverse proxies servers |
||
| 520 | * and load balancers |
||
| 521 | * @return string Server protocol (http or https) |
||
| 522 | */ |
||
| 523 | 72 | public function getServerProtocol() { |
|
| 550 | |||
| 551 | /** |
||
| 552 | * Returns the used HTTP protocol. |
||
| 553 | * |
||
| 554 | * @return string HTTP protocol. HTTP/2, HTTP/1.1 or HTTP/1.0. |
||
| 555 | */ |
||
| 556 | 13 | View Code Duplication | public function getHttpProtocol() { |
| 571 | |||
| 572 | /** |
||
| 573 | * Returns the request uri, even if the website uses one or more |
||
| 574 | * reverse proxies |
||
| 575 | * @return string |
||
| 576 | */ |
||
| 577 | 3 | public function getRequestUri() { |
|
| 584 | |||
| 585 | /** |
||
| 586 | * Get raw PathInfo from request (not urldecoded) |
||
| 587 | * @throws \Exception |
||
| 588 | * @return string Path info |
||
| 589 | */ |
||
| 590 | 18 | public function getRawPathInfo() { |
|
| 629 | |||
| 630 | /** |
||
| 631 | * Get PathInfo from request |
||
| 632 | * @throws \Exception |
||
| 633 | * @return string|false Path info or false when not found |
||
| 634 | */ |
||
| 635 | 10 | public function getPathInfo() { |
|
| 653 | |||
| 654 | /** |
||
| 655 | * Returns the script name, even if the website uses one or more |
||
| 656 | * reverse proxies |
||
| 657 | * @return string the script name |
||
| 658 | */ |
||
| 659 | public function getScriptName() { |
||
| 670 | |||
| 671 | /** |
||
| 672 | * Checks whether the user agent matches a given regex |
||
| 673 | * @param array $agent array of agent names |
||
| 674 | * @return bool true if at least one of the given agent matches, false otherwise |
||
| 675 | */ |
||
| 676 | 18 | public function isUserAgent(array $agent) { |
|
| 687 | |||
| 688 | /** |
||
| 689 | * Returns the unverified server host from the headers without checking |
||
| 690 | * whether it is a trusted domain |
||
| 691 | * @return string Server host |
||
| 692 | */ |
||
| 693 | 85 | public function getInsecureServerHost() { |
|
| 711 | |||
| 712 | |||
| 713 | /** |
||
| 714 | * Returns the server host from the headers, or the first configured |
||
| 715 | * trusted domain if the host isn't in the trusted list |
||
| 716 | * @return string Server host |
||
| 717 | */ |
||
| 718 | 82 | public function getServerHost() { |
|
| 743 | |||
| 744 | /** |
||
| 745 | * Returns the overwritehost setting from the config if set and |
||
| 746 | * if the overwrite condition is met |
||
| 747 | * @return string|null overwritehost value or null if not defined or the defined condition |
||
| 748 | * isn't met |
||
| 749 | */ |
||
| 750 | 84 | private function getOverwriteHost() { |
|
| 756 | |||
| 757 | } |
||
| 758 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.