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 |
||
| 55 | class Request implements \ArrayAccess, \Countable, IRequest { |
||
| 56 | |||
| 57 | const USER_AGENT_IE = '/(MSIE)|(Trident)/'; |
||
| 58 | const USER_AGENT_IE_8 = '/MSIE 8.0/'; |
||
| 59 | // Microsoft Edge User Agent from https://msdn.microsoft.com/en-us/library/hh869301(v=vs.85).aspx |
||
| 60 | const USER_AGENT_MS_EDGE = '/^Mozilla\/5\.0 \([^)]+\) AppleWebKit\/[0-9.]+ \(KHTML, like Gecko\) Chrome\/[0-9.]+ (Mobile Safari|Safari)\/[0-9.]+ Edge\/[0-9.]+$/'; |
||
| 61 | // Firefox User Agent from https://developer.mozilla.org/en-US/docs/Web/HTTP/Gecko_user_agent_string_reference |
||
| 62 | const USER_AGENT_FIREFOX = '/^Mozilla\/5\.0 \([^)]+\) Gecko\/[0-9.]+ Firefox\/[0-9.]+$/'; |
||
| 63 | // Chrome User Agent from https://developer.chrome.com/multidevice/user-agent |
||
| 64 | const USER_AGENT_CHROME = '/^Mozilla\/5\.0 \([^)]+\) AppleWebKit\/[0-9.]+ \(KHTML, like Gecko\) Chrome\/[0-9.]+ (Mobile Safari|Safari)\/[0-9.]+$/'; |
||
| 65 | // Safari User Agent from http://www.useragentstring.com/pages/Safari/ |
||
| 66 | const USER_AGENT_SAFARI = '/^Mozilla\/5\.0 \([^)]+\) AppleWebKit\/[0-9.]+ \(KHTML, like Gecko\) Version\/[0-9.]+ Safari\/[0-9.A-Z]+$/'; |
||
| 67 | // Android Chrome user agent: https://developers.google.com/chrome/mobile/docs/user-agent |
||
| 68 | const USER_AGENT_ANDROID_MOBILE_CHROME = '#Android.*Chrome/[.0-9]*#'; |
||
| 69 | const USER_AGENT_FREEBOX = '#^Mozilla/5\.0$#'; |
||
| 70 | const USER_AGENT_OWNCLOUD_IOS = '/^Mozilla\/5\.0 \(iOS\) ownCloud\-iOS.*$/'; |
||
| 71 | const USER_AGENT_OWNCLOUD_ANDROID = '/^Mozilla\/5\.0 \(Android\) ownCloud\-android.*$/'; |
||
| 72 | const USER_AGENT_OWNCLOUD_DESKTOP = '/^Mozilla\/5\.0 \([A-Za-z ]+\) (mirall|csyncoC)\/.*$/'; |
||
| 73 | const REGEX_LOCALHOST = '/^(127\.0\.0\.1|localhost)$/'; |
||
| 74 | |||
| 75 | protected $inputStream; |
||
| 76 | protected $content; |
||
| 77 | protected $items = array(); |
||
| 78 | protected $allowedKeys = array( |
||
| 79 | 'get', |
||
| 80 | 'post', |
||
| 81 | 'files', |
||
| 82 | 'server', |
||
| 83 | 'env', |
||
| 84 | 'cookies', |
||
| 85 | 'urlParams', |
||
| 86 | 'parameters', |
||
| 87 | 'method', |
||
| 88 | 'requesttoken', |
||
| 89 | ); |
||
| 90 | /** @var ISecureRandom */ |
||
| 91 | protected $secureRandom; |
||
| 92 | /** @var IConfig */ |
||
| 93 | protected $config; |
||
| 94 | /** @var string */ |
||
| 95 | protected $requestId = ''; |
||
| 96 | /** @var ICrypto */ |
||
| 97 | protected $crypto; |
||
| 98 | /** @var CsrfTokenManager|null */ |
||
| 99 | protected $csrfTokenManager; |
||
| 100 | |||
| 101 | /** @var bool */ |
||
| 102 | protected $contentDecoded = false; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @param array $vars An associative array with the following optional values: |
||
| 106 | * - array 'urlParams' the parameters which were matched from the URL |
||
| 107 | * - array 'get' the $_GET array |
||
| 108 | * - array|string 'post' the $_POST array or JSON string |
||
| 109 | * - array 'files' the $_FILES array |
||
| 110 | * - array 'server' the $_SERVER array |
||
| 111 | * - array 'env' the $_ENV array |
||
| 112 | * - array 'cookies' the $_COOKIE array |
||
| 113 | * - string 'method' the request method (GET, POST etc) |
||
| 114 | * - string|false 'requesttoken' the requesttoken or false when not available |
||
| 115 | * @param ISecureRandom $secureRandom |
||
| 116 | * @param IConfig $config |
||
| 117 | * @param CsrfTokenManager|null $csrfTokenManager |
||
| 118 | * @param string $stream |
||
| 119 | * @see http://www.php.net/manual/en/reserved.variables.php |
||
| 120 | */ |
||
| 121 | public function __construct(array $vars=array(), |
||
| 150 | /** |
||
| 151 | * @param array $parameters |
||
| 152 | */ |
||
| 153 | public function setUrlParameters(array $parameters) { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Countable method |
||
| 163 | * @return int |
||
| 164 | */ |
||
| 165 | public function count() { |
||
| 168 | |||
| 169 | /** |
||
| 170 | * ArrayAccess methods |
||
| 171 | * |
||
| 172 | * Gives access to the combined GET, POST and urlParams arrays |
||
| 173 | * |
||
| 174 | * Examples: |
||
| 175 | * |
||
| 176 | * $var = $request['myvar']; |
||
| 177 | * |
||
| 178 | * or |
||
| 179 | * |
||
| 180 | * if(!isset($request['myvar']) { |
||
| 181 | * // Do something |
||
| 182 | * } |
||
| 183 | * |
||
| 184 | * $request['myvar'] = 'something'; // This throws an exception. |
||
| 185 | * |
||
| 186 | * @param string $offset The key to lookup |
||
| 187 | * @return boolean |
||
| 188 | */ |
||
| 189 | public function offsetExists($offset) { |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @see offsetExists |
||
| 195 | */ |
||
| 196 | public function offsetGet($offset) { |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @see offsetExists |
||
| 204 | */ |
||
| 205 | public function offsetSet($offset, $value) { |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @see offsetExists |
||
| 211 | */ |
||
| 212 | public function offsetUnset($offset) { |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Magic property accessors |
||
| 218 | * @param string $name |
||
| 219 | * @param mixed $value |
||
| 220 | */ |
||
| 221 | public function __set($name, $value) { |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Access request variables by method and name. |
||
| 227 | * Examples: |
||
| 228 | * |
||
| 229 | * $request->post['myvar']; // Only look for POST variables |
||
| 230 | * $request->myvar; or $request->{'myvar'}; or $request->{$myvar} |
||
| 231 | * Looks in the combined GET, POST and urlParams array. |
||
| 232 | * |
||
| 233 | * If you access e.g. ->post but the current HTTP request method |
||
| 234 | * is GET a \LogicException will be thrown. |
||
| 235 | * |
||
| 236 | * @param string $name The key to look for. |
||
| 237 | * @throws \LogicException |
||
| 238 | * @return mixed|null |
||
| 239 | */ |
||
| 240 | public function __get($name) { |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @param string $name |
||
| 271 | * @return bool |
||
| 272 | */ |
||
| 273 | public function __isset($name) { |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @param string $id |
||
| 282 | */ |
||
| 283 | public function __unset($id) { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Returns the value for a specific http header. |
||
| 289 | * |
||
| 290 | * This method returns null if the header did not exist. |
||
| 291 | * |
||
| 292 | * @param string $name |
||
| 293 | * @return string |
||
| 294 | */ |
||
| 295 | public function getHeader($name) { |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Lets you access post and get parameters by the index |
||
| 319 | * In case of json requests the encoded json body is accessed |
||
| 320 | * |
||
| 321 | * @param string $key the key which you want to access in the URL Parameter |
||
| 322 | * placeholder, $_POST or $_GET array. |
||
| 323 | * The priority how they're returned is the following: |
||
| 324 | * 1. URL parameters |
||
| 325 | * 2. POST parameters |
||
| 326 | * 3. GET parameters |
||
| 327 | * @param mixed $default If the key is not found, this value will be returned |
||
| 328 | * @return mixed the content of the array |
||
| 329 | */ |
||
| 330 | public function getParam($key, $default = null) { |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Returns all params that were received, be it from the request |
||
| 338 | * (as GET or POST) or throuh the URL by the route |
||
| 339 | * @return array the array with all parameters |
||
| 340 | */ |
||
| 341 | public function getParams() { |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Returns the method of the request |
||
| 347 | * @return string the method of the request (POST, GET, etc) |
||
| 348 | */ |
||
| 349 | public function getMethod() { |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Shortcut for accessing an uploaded file through the $_FILES array |
||
| 355 | * @param string $key the key that will be taken from the $_FILES array |
||
| 356 | * @return array the file in the $_FILES element |
||
| 357 | */ |
||
| 358 | public function getUploadedFile($key) { |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Shortcut for getting env variables |
||
| 364 | * @param string $key the key that will be taken from the $_ENV array |
||
| 365 | * @return array the value in the $_ENV element |
||
| 366 | */ |
||
| 367 | public function getEnv($key) { |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Shortcut for getting cookie variables |
||
| 373 | * @param string $key the key that will be taken from the $_COOKIE array |
||
| 374 | * @return string the value in the $_COOKIE element |
||
| 375 | */ |
||
| 376 | public function getCookie($key) { |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Returns the request body content. |
||
| 382 | * |
||
| 383 | * If the HTTP request method is PUT and the body |
||
| 384 | * not application/x-www-form-urlencoded or application/json a stream |
||
| 385 | * resource is returned, otherwise an array. |
||
| 386 | * |
||
| 387 | * @return array|string|resource The request body content or a resource to read the body stream. |
||
| 388 | * |
||
| 389 | * @throws \LogicException |
||
| 390 | */ |
||
| 391 | protected function getContent() { |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Attempt to decode the content and populate parameters |
||
| 413 | */ |
||
| 414 | protected function decodeContent() { |
||
| 447 | |||
| 448 | |||
| 449 | /** |
||
| 450 | * Checks if the CSRF check was correct |
||
| 451 | * @return bool true if CSRF check passed |
||
| 452 | */ |
||
| 453 | public function passesCSRFCheck() { |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Returns an ID for the request, value is not guaranteed to be unique and is mostly meant for logging |
||
| 475 | * If `mod_unique_id` is installed this value will be taken. |
||
| 476 | * @return string |
||
| 477 | */ |
||
| 478 | public function getId() { |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Returns the remote address, if the connection came from a trusted proxy |
||
| 492 | * and `forwarded_for_headers` has been configured then the IP address |
||
| 493 | * specified in this header will be returned instead. |
||
| 494 | * Do always use this instead of $_SERVER['REMOTE_ADDR'] |
||
| 495 | * @return string IP address |
||
| 496 | */ |
||
| 497 | public function getRemoteAddress() { |
||
| 521 | |||
| 522 | /** |
||
| 523 | * Check overwrite condition |
||
| 524 | * @param string $type |
||
| 525 | * @return bool |
||
| 526 | */ |
||
| 527 | private function isOverwriteCondition($type = '') { |
||
| 533 | |||
| 534 | /** |
||
| 535 | * Returns the server protocol. It respects one or more reverse proxies servers |
||
| 536 | * and load balancers |
||
| 537 | * @return string Server protocol (http or https) |
||
| 538 | */ |
||
| 539 | public function getServerProtocol() { |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Returns the used HTTP protocol. |
||
| 570 | * |
||
| 571 | * @return string HTTP protocol. HTTP/2, HTTP/1.1 or HTTP/1.0. |
||
| 572 | */ |
||
| 573 | View Code Duplication | public function getHttpProtocol() { |
|
| 588 | |||
| 589 | /** |
||
| 590 | * Returns the request uri, even if the website uses one or more |
||
| 591 | * reverse proxies |
||
| 592 | * @return string |
||
| 593 | */ |
||
| 594 | public function getRequestUri() { |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Get raw PathInfo from request (not urldecoded) |
||
| 604 | * @throws \Exception |
||
| 605 | * @return string Path info |
||
| 606 | */ |
||
| 607 | public function getRawPathInfo() { |
||
| 665 | |||
| 666 | /** |
||
| 667 | * Get PathInfo from request |
||
| 668 | * @throws \Exception |
||
| 669 | * @return string|false Path info or false when not found |
||
| 670 | */ |
||
| 671 | public function getPathInfo() { |
||
| 689 | |||
| 690 | /** |
||
| 691 | * Returns the script name, even if the website uses one or more |
||
| 692 | * reverse proxies |
||
| 693 | * @return string the script name |
||
| 694 | */ |
||
| 695 | public function getScriptName() { |
||
| 706 | |||
| 707 | /** |
||
| 708 | * Checks whether the user agent matches a given regex |
||
| 709 | * @param array $agent array of agent names |
||
| 710 | * @return bool true if at least one of the given agent matches, false otherwise |
||
| 711 | */ |
||
| 712 | public function isUserAgent(array $agent) { |
||
| 723 | |||
| 724 | /** |
||
| 725 | * Returns the unverified server host from the headers without checking |
||
| 726 | * whether it is a trusted domain |
||
| 727 | * @return string Server host |
||
| 728 | */ |
||
| 729 | public function getInsecureServerHost() { |
||
| 747 | |||
| 748 | |||
| 749 | /** |
||
| 750 | * Returns the server host from the headers, or the first configured |
||
| 751 | * trusted domain if the host isn't in the trusted list |
||
| 752 | * @return string Server host |
||
| 753 | */ |
||
| 754 | public function getServerHost() { |
||
| 779 | |||
| 780 | /** |
||
| 781 | * Returns the overwritehost setting from the config if set and |
||
| 782 | * if the overwrite condition is met |
||
| 783 | * @return string|null overwritehost value or null if not defined or the defined condition |
||
| 784 | * isn't met |
||
| 785 | */ |
||
| 786 | private function getOverwriteHost() { |
||
| 792 | |||
| 793 | /** |
||
| 794 | * Fixes bug in Apache 2.4 / PHP FPM where script_name contains full |
||
| 795 | * uri not just script name e.g /index.php/app/files instead of expected /index.php |
||
| 796 | * @see https://bugs.php.net/bug.php?id=65641 |
||
| 797 | * @param string $name |
||
| 798 | * @return string |
||
| 799 | */ |
||
| 800 | private function getRawScriptName($requestUri=null) { |
||
| 821 | |||
| 822 | /** |
||
| 823 | * Removes leading extra slashes and query params |
||
| 824 | * @return string |
||
| 825 | */ |
||
| 826 | private function getCleanRequestUri() |
||
| 844 | } |
||
| 845 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.