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 |
||
| 60 | class Request implements \ArrayAccess, \Countable, IRequest { |
||
| 61 | |||
| 62 | const USER_AGENT_IE = '/(MSIE)|(Trident)/'; |
||
| 63 | // Microsoft Edge User Agent from https://msdn.microsoft.com/en-us/library/hh869301(v=vs.85).aspx |
||
| 64 | 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.]+$/'; |
||
| 65 | // Firefox User Agent from https://developer.mozilla.org/en-US/docs/Web/HTTP/Gecko_user_agent_string_reference |
||
| 66 | const USER_AGENT_FIREFOX = '/^Mozilla\/5\.0 \([^)]+\) Gecko\/[0-9.]+ Firefox\/[0-9.]+$/'; |
||
| 67 | // Chrome User Agent from https://developer.chrome.com/multidevice/user-agent |
||
| 68 | const USER_AGENT_CHROME = '/^Mozilla\/5\.0 \([^)]+\) AppleWebKit\/[0-9.]+ \(KHTML, like Gecko\)( Ubuntu Chromium\/[0-9.]+|) Chrome\/[0-9.]+ (Mobile Safari|Safari)\/[0-9.]+$/'; |
||
| 69 | // Safari User Agent from http://www.useragentstring.com/pages/Safari/ |
||
| 70 | const USER_AGENT_SAFARI = '/^Mozilla\/5\.0 \([^)]+\) AppleWebKit\/[0-9.]+ \(KHTML, like Gecko\) Version\/[0-9.]+ Safari\/[0-9.A-Z]+$/'; |
||
| 71 | // Android Chrome user agent: https://developers.google.com/chrome/mobile/docs/user-agent |
||
| 72 | const USER_AGENT_ANDROID_MOBILE_CHROME = '#Android.*Chrome/[.0-9]*#'; |
||
| 73 | const USER_AGENT_FREEBOX = '#^Mozilla/5\.0$#'; |
||
| 74 | const REGEX_LOCALHOST = '/^(127\.0\.0\.1|localhost|::1)$/'; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @deprecated use \OCP\IRequest::USER_AGENT_CLIENT_IOS instead |
||
| 78 | */ |
||
| 79 | const USER_AGENT_OWNCLOUD_IOS = '/^Mozilla\/5\.0 \(iOS\) (ownCloud|Nextcloud)\-iOS.*$/'; |
||
| 80 | /** |
||
| 81 | * @deprecated use \OCP\IRequest::USER_AGENT_CLIENT_ANDROID instead |
||
| 82 | */ |
||
| 83 | const USER_AGENT_OWNCLOUD_ANDROID = '/^Mozilla\/5\.0 \(Android\) ownCloud\-android.*$/'; |
||
| 84 | /** |
||
| 85 | * @deprecated use \OCP\IRequest::USER_AGENT_CLIENT_DESKTOP instead |
||
| 86 | */ |
||
| 87 | const USER_AGENT_OWNCLOUD_DESKTOP = '/^Mozilla\/5\.0 \([A-Za-z ]+\) (mirall|csyncoC)\/.*$/'; |
||
| 88 | |||
| 89 | protected $inputStream; |
||
| 90 | protected $content; |
||
| 91 | protected $items = []; |
||
| 92 | protected $allowedKeys = [ |
||
| 93 | 'get', |
||
| 94 | 'post', |
||
| 95 | 'files', |
||
| 96 | 'server', |
||
| 97 | 'env', |
||
| 98 | 'cookies', |
||
| 99 | 'urlParams', |
||
| 100 | 'parameters', |
||
| 101 | 'method', |
||
| 102 | 'requesttoken', |
||
| 103 | ]; |
||
| 104 | /** @var ISecureRandom */ |
||
| 105 | protected $secureRandom; |
||
| 106 | /** @var IConfig */ |
||
| 107 | protected $config; |
||
| 108 | /** @var string */ |
||
| 109 | protected $requestId = ''; |
||
| 110 | /** @var ICrypto */ |
||
| 111 | protected $crypto; |
||
| 112 | /** @var CsrfTokenManager|null */ |
||
| 113 | protected $csrfTokenManager; |
||
| 114 | |||
| 115 | /** @var bool */ |
||
| 116 | protected $contentDecoded = false; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @param array $vars An associative array with the following optional values: |
||
| 120 | * - array 'urlParams' the parameters which were matched from the URL |
||
| 121 | * - array 'get' the $_GET array |
||
| 122 | * - array|string 'post' the $_POST array or JSON string |
||
| 123 | * - array 'files' the $_FILES array |
||
| 124 | * - array 'server' the $_SERVER array |
||
| 125 | * - array 'env' the $_ENV array |
||
| 126 | * - array 'cookies' the $_COOKIE array |
||
| 127 | * - string 'method' the request method (GET, POST etc) |
||
| 128 | * - string|false 'requesttoken' the requesttoken or false when not available |
||
| 129 | * @param ISecureRandom $secureRandom |
||
| 130 | * @param IConfig $config |
||
| 131 | * @param CsrfTokenManager|null $csrfTokenManager |
||
| 132 | * @param string $stream |
||
| 133 | * @see http://www.php.net/manual/en/reserved.variables.php |
||
| 134 | */ |
||
| 135 | public function __construct(array $vars= [], |
||
| 136 | ISecureRandom $secureRandom = null, |
||
| 137 | IConfig $config, |
||
| 138 | CsrfTokenManager $csrfTokenManager = null, |
||
| 139 | string $stream = 'php://input') { |
||
| 140 | $this->inputStream = $stream; |
||
| 141 | $this->items['params'] = []; |
||
| 142 | $this->secureRandom = $secureRandom; |
||
| 143 | $this->config = $config; |
||
| 144 | $this->csrfTokenManager = $csrfTokenManager; |
||
| 145 | |||
| 146 | if(!array_key_exists('method', $vars)) { |
||
| 147 | $vars['method'] = 'GET'; |
||
| 148 | } |
||
| 149 | |||
| 150 | foreach($this->allowedKeys as $name) { |
||
| 151 | $this->items[$name] = isset($vars[$name]) |
||
| 152 | ? $vars[$name] |
||
| 153 | : []; |
||
| 154 | } |
||
| 155 | |||
| 156 | $this->items['parameters'] = array_merge( |
||
| 157 | $this->items['get'], |
||
| 158 | $this->items['post'], |
||
| 159 | $this->items['urlParams'], |
||
| 160 | $this->items['params'] |
||
| 161 | ); |
||
| 162 | |||
| 163 | } |
||
| 164 | /** |
||
| 165 | * @param array $parameters |
||
| 166 | */ |
||
| 167 | public function setUrlParameters(array $parameters) { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Countable method |
||
| 177 | * @return int |
||
| 178 | */ |
||
| 179 | public function count(): int { |
||
| 182 | |||
| 183 | /** |
||
| 184 | * ArrayAccess methods |
||
| 185 | * |
||
| 186 | * Gives access to the combined GET, POST and urlParams arrays |
||
| 187 | * |
||
| 188 | * Examples: |
||
| 189 | * |
||
| 190 | * $var = $request['myvar']; |
||
| 191 | * |
||
| 192 | * or |
||
| 193 | * |
||
| 194 | * if(!isset($request['myvar']) { |
||
| 195 | * // Do something |
||
| 196 | * } |
||
| 197 | * |
||
| 198 | * $request['myvar'] = 'something'; // This throws an exception. |
||
| 199 | * |
||
| 200 | * @param string $offset The key to lookup |
||
| 201 | * @return boolean |
||
| 202 | */ |
||
| 203 | public function offsetExists($offset): bool { |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @see offsetExists |
||
| 209 | * @param string $offset |
||
| 210 | * @return mixed |
||
| 211 | */ |
||
| 212 | public function offsetGet($offset) { |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @see offsetExists |
||
| 220 | * @param string $offset |
||
| 221 | * @param mixed $value |
||
| 222 | */ |
||
| 223 | public function offsetSet($offset, $value) { |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @see offsetExists |
||
| 229 | * @param string $offset |
||
| 230 | */ |
||
| 231 | public function offsetUnset($offset) { |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Magic property accessors |
||
| 237 | * @param string $name |
||
| 238 | * @param mixed $value |
||
| 239 | */ |
||
| 240 | public function __set($name, $value) { |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Access request variables by method and name. |
||
| 246 | * Examples: |
||
| 247 | * |
||
| 248 | * $request->post['myvar']; // Only look for POST variables |
||
| 249 | * $request->myvar; or $request->{'myvar'}; or $request->{$myvar} |
||
| 250 | * Looks in the combined GET, POST and urlParams array. |
||
| 251 | * |
||
| 252 | * If you access e.g. ->post but the current HTTP request method |
||
| 253 | * is GET a \LogicException will be thrown. |
||
| 254 | * |
||
| 255 | * @param string $name The key to look for. |
||
| 256 | * @throws \LogicException |
||
| 257 | * @return mixed|null |
||
| 258 | */ |
||
| 259 | public function __get($name) { |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @param string $name |
||
| 290 | * @return bool |
||
| 291 | */ |
||
| 292 | public function __isset($name) { |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @param string $id |
||
| 301 | */ |
||
| 302 | public function __unset($id) { |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Returns the value for a specific http header. |
||
| 308 | * |
||
| 309 | * This method returns null if the header did not exist. |
||
| 310 | * |
||
| 311 | * @param string $name |
||
| 312 | * @return string |
||
| 313 | */ |
||
| 314 | public function getHeader(string $name): string { |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Lets you access post and get parameters by the index |
||
| 338 | * In case of json requests the encoded json body is accessed |
||
| 339 | * |
||
| 340 | * @param string $key the key which you want to access in the URL Parameter |
||
| 341 | * placeholder, $_POST or $_GET array. |
||
| 342 | * The priority how they're returned is the following: |
||
| 343 | * 1. URL parameters |
||
| 344 | * 2. POST parameters |
||
| 345 | * 3. GET parameters |
||
| 346 | * @param mixed $default If the key is not found, this value will be returned |
||
| 347 | * @return mixed the content of the array |
||
| 348 | */ |
||
| 349 | public function getParam(string $key, $default = null) { |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Returns all params that were received, be it from the request |
||
| 357 | * (as GET or POST) or throuh the URL by the route |
||
| 358 | * @return array the array with all parameters |
||
| 359 | */ |
||
| 360 | public function getParams(): array { |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Returns the method of the request |
||
| 366 | * @return string the method of the request (POST, GET, etc) |
||
| 367 | */ |
||
| 368 | public function getMethod(): string { |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Shortcut for accessing an uploaded file through the $_FILES array |
||
| 374 | * @param string $key the key that will be taken from the $_FILES array |
||
| 375 | * @return array the file in the $_FILES element |
||
| 376 | */ |
||
| 377 | public function getUploadedFile(string $key) { |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Shortcut for getting env variables |
||
| 383 | * @param string $key the key that will be taken from the $_ENV array |
||
| 384 | * @return array the value in the $_ENV element |
||
| 385 | */ |
||
| 386 | public function getEnv(string $key) { |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Shortcut for getting cookie variables |
||
| 392 | * @param string $key the key that will be taken from the $_COOKIE array |
||
| 393 | * @return string the value in the $_COOKIE element |
||
| 394 | */ |
||
| 395 | public function getCookie(string $key) { |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Returns the request body content. |
||
| 401 | * |
||
| 402 | * If the HTTP request method is PUT and the body |
||
| 403 | * not application/x-www-form-urlencoded or application/json a stream |
||
| 404 | * resource is returned, otherwise an array. |
||
| 405 | * |
||
| 406 | * @return array|string|resource The request body content or a resource to read the body stream. |
||
| 407 | * |
||
| 408 | * @throws \LogicException |
||
| 409 | */ |
||
| 410 | protected function getContent() { |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Attempt to decode the content and populate parameters |
||
| 434 | */ |
||
| 435 | protected function decodeContent() { |
||
| 468 | |||
| 469 | |||
| 470 | /** |
||
| 471 | * Checks if the CSRF check was correct |
||
| 472 | * @return bool true if CSRF check passed |
||
| 473 | */ |
||
| 474 | public function passesCSRFCheck(): bool { |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Whether the cookie checks are required |
||
| 500 | * |
||
| 501 | * @return bool |
||
| 502 | */ |
||
| 503 | private function cookieCheckRequired(): bool { |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Wrapper around session_get_cookie_params |
||
| 516 | * |
||
| 517 | * @return array |
||
| 518 | */ |
||
| 519 | public function getCookieParams(): array { |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Appends the __Host- prefix to the cookie if applicable |
||
| 525 | * |
||
| 526 | * @param string $name |
||
| 527 | * @return string |
||
| 528 | */ |
||
| 529 | protected function getProtectedCookieName(string $name): string { |
||
| 538 | |||
| 539 | /** |
||
| 540 | * Checks if the strict cookie has been sent with the request if the request |
||
| 541 | * is including any cookies. |
||
| 542 | * |
||
| 543 | * @return bool |
||
| 544 | * @since 9.1.0 |
||
| 545 | */ |
||
| 546 | View Code Duplication | public function passesStrictCookieCheck(): bool { |
|
| 558 | |||
| 559 | /** |
||
| 560 | * Checks if the lax cookie has been sent with the request if the request |
||
| 561 | * is including any cookies. |
||
| 562 | * |
||
| 563 | * @return bool |
||
| 564 | * @since 9.1.0 |
||
| 565 | */ |
||
| 566 | View Code Duplication | public function passesLaxCookieCheck(): bool { |
|
| 577 | |||
| 578 | |||
| 579 | /** |
||
| 580 | * Returns an ID for the request, value is not guaranteed to be unique and is mostly meant for logging |
||
| 581 | * If `mod_unique_id` is installed this value will be taken. |
||
| 582 | * @return string |
||
| 583 | */ |
||
| 584 | public function getId(): string { |
||
| 596 | |||
| 597 | /** |
||
| 598 | * Returns the remote address, if the connection came from a trusted proxy |
||
| 599 | * and `forwarded_for_headers` has been configured then the IP address |
||
| 600 | * specified in this header will be returned instead. |
||
| 601 | * Do always use this instead of $_SERVER['REMOTE_ADDR'] |
||
| 602 | * @return string IP address |
||
| 603 | */ |
||
| 604 | public function getRemoteAddress(): string { |
||
| 628 | |||
| 629 | /** |
||
| 630 | * Check overwrite condition |
||
| 631 | * @param string $type |
||
| 632 | * @return bool |
||
| 633 | */ |
||
| 634 | private function isOverwriteCondition(string $type = ''): bool { |
||
| 640 | |||
| 641 | /** |
||
| 642 | * Returns the server protocol. It respects one or more reverse proxies servers |
||
| 643 | * and load balancers |
||
| 644 | * @return string Server protocol (http or https) |
||
| 645 | */ |
||
| 646 | public function getServerProtocol(): string { |
||
| 674 | |||
| 675 | /** |
||
| 676 | * Returns the used HTTP protocol. |
||
| 677 | * |
||
| 678 | * @return string HTTP protocol. HTTP/2, HTTP/1.1 or HTTP/1.0. |
||
| 679 | */ |
||
| 680 | public function getHttpProtocol(): string { |
||
| 699 | |||
| 700 | /** |
||
| 701 | * Returns the request uri, even if the website uses one or more |
||
| 702 | * reverse proxies |
||
| 703 | * @return string |
||
| 704 | */ |
||
| 705 | public function getRequestUri(): string { |
||
| 712 | |||
| 713 | /** |
||
| 714 | * Get raw PathInfo from request (not urldecoded) |
||
| 715 | * @throws \Exception |
||
| 716 | * @return string Path info |
||
| 717 | */ |
||
| 718 | public function getRawPathInfo(): string { |
||
| 761 | |||
| 762 | /** |
||
| 763 | * Get PathInfo from request |
||
| 764 | * @throws \Exception |
||
| 765 | * @return string|false Path info or false when not found |
||
| 766 | */ |
||
| 767 | public function getPathInfo() { |
||
| 781 | |||
| 782 | /** |
||
| 783 | * Returns the script name, even if the website uses one or more |
||
| 784 | * reverse proxies |
||
| 785 | * @return string the script name |
||
| 786 | */ |
||
| 787 | public function getScriptName(): string { |
||
| 798 | |||
| 799 | /** |
||
| 800 | * Checks whether the user agent matches a given regex |
||
| 801 | * @param array $agent array of agent names |
||
| 802 | * @return bool true if at least one of the given agent matches, false otherwise |
||
| 803 | */ |
||
| 804 | public function isUserAgent(array $agent): bool { |
||
| 815 | |||
| 816 | /** |
||
| 817 | * Returns the unverified server host from the headers without checking |
||
| 818 | * whether it is a trusted domain |
||
| 819 | * @return string Server host |
||
| 820 | */ |
||
| 821 | public function getInsecureServerHost(): string { |
||
| 839 | |||
| 840 | |||
| 841 | /** |
||
| 842 | * Returns the server host from the headers, or the first configured |
||
| 843 | * trusted domain if the host isn't in the trusted list |
||
| 844 | * @return string Server host |
||
| 845 | */ |
||
| 846 | public function getServerHost(): string { |
||
| 871 | |||
| 872 | /** |
||
| 873 | * Returns the overwritehost setting from the config if set and |
||
| 874 | * if the overwrite condition is met |
||
| 875 | * @return string|null overwritehost value or null if not defined or the defined condition |
||
| 876 | * isn't met |
||
| 877 | */ |
||
| 878 | private function getOverwriteHost() { |
||
| 884 | |||
| 885 | } |
||
| 886 |
As per the PSR-2 coding standard, default statements should not be wrapped in curly braces.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.