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 |
||
| 59 | class Request implements \ArrayAccess, \Countable, IRequest { |
||
| 60 | |||
| 61 | const USER_AGENT_IE = '/(MSIE)|(Trident)/'; |
||
| 62 | // Microsoft Edge User Agent from https://msdn.microsoft.com/en-us/library/hh869301(v=vs.85).aspx |
||
| 63 | 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.]+$/'; |
||
| 64 | // Firefox User Agent from https://developer.mozilla.org/en-US/docs/Web/HTTP/Gecko_user_agent_string_reference |
||
| 65 | const USER_AGENT_FIREFOX = '/^Mozilla\/5\.0 \([^)]+\) Gecko\/[0-9.]+ Firefox\/[0-9.]+$/'; |
||
| 66 | // Chrome User Agent from https://developer.chrome.com/multidevice/user-agent |
||
| 67 | 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.]+$/'; |
||
| 68 | // Safari User Agent from http://www.useragentstring.com/pages/Safari/ |
||
| 69 | const USER_AGENT_SAFARI = '/^Mozilla\/5\.0 \([^)]+\) AppleWebKit\/[0-9.]+ \(KHTML, like Gecko\) Version\/[0-9.]+ Safari\/[0-9.A-Z]+$/'; |
||
| 70 | // Android Chrome user agent: https://developers.google.com/chrome/mobile/docs/user-agent |
||
| 71 | const USER_AGENT_ANDROID_MOBILE_CHROME = '#Android.*Chrome/[.0-9]*#'; |
||
| 72 | const USER_AGENT_FREEBOX = '#^Mozilla/5\.0$#'; |
||
| 73 | const REGEX_LOCALHOST = '/^(127\.0\.0\.1|localhost|::1)$/'; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @deprecated use \OCP\IRequest::USER_AGENT_CLIENT_IOS instead |
||
| 77 | */ |
||
| 78 | const USER_AGENT_OWNCLOUD_IOS = '/^Mozilla\/5\.0 \(iOS\) (ownCloud|Nextcloud)\-iOS.*$/'; |
||
| 79 | /** |
||
| 80 | * @deprecated use \OCP\IRequest::USER_AGENT_CLIENT_ANDROID instead |
||
| 81 | */ |
||
| 82 | const USER_AGENT_OWNCLOUD_ANDROID = '/^Mozilla\/5\.0 \(Android\) ownCloud\-android.*$/'; |
||
| 83 | /** |
||
| 84 | * @deprecated use \OCP\IRequest::USER_AGENT_CLIENT_DESKTOP instead |
||
| 85 | */ |
||
| 86 | const USER_AGENT_OWNCLOUD_DESKTOP = '/^Mozilla\/5\.0 \([A-Za-z ]+\) (mirall|csyncoC)\/.*$/'; |
||
| 87 | |||
| 88 | protected $inputStream; |
||
| 89 | protected $content; |
||
| 90 | protected $items = array(); |
||
| 91 | protected $allowedKeys = array( |
||
| 92 | 'get', |
||
| 93 | 'post', |
||
| 94 | 'files', |
||
| 95 | 'server', |
||
| 96 | 'env', |
||
| 97 | 'cookies', |
||
| 98 | 'urlParams', |
||
| 99 | 'parameters', |
||
| 100 | 'method', |
||
| 101 | 'requesttoken', |
||
| 102 | ); |
||
| 103 | /** @var ISecureRandom */ |
||
| 104 | protected $secureRandom; |
||
| 105 | /** @var IConfig */ |
||
| 106 | protected $config; |
||
| 107 | /** @var string */ |
||
| 108 | protected $requestId = ''; |
||
| 109 | /** @var ICrypto */ |
||
| 110 | protected $crypto; |
||
| 111 | /** @var CsrfTokenManager|null */ |
||
| 112 | protected $csrfTokenManager; |
||
| 113 | |||
| 114 | /** @var bool */ |
||
| 115 | protected $contentDecoded = false; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @param array $vars An associative array with the following optional values: |
||
| 119 | * - array 'urlParams' the parameters which were matched from the URL |
||
| 120 | * - array 'get' the $_GET array |
||
| 121 | * - array|string 'post' the $_POST array or JSON string |
||
| 122 | * - array 'files' the $_FILES array |
||
| 123 | * - array 'server' the $_SERVER array |
||
| 124 | * - array 'env' the $_ENV array |
||
| 125 | * - array 'cookies' the $_COOKIE array |
||
| 126 | * - string 'method' the request method (GET, POST etc) |
||
| 127 | * - string|false 'requesttoken' the requesttoken or false when not available |
||
| 128 | * @param ISecureRandom $secureRandom |
||
| 129 | * @param IConfig $config |
||
| 130 | * @param CsrfTokenManager|null $csrfTokenManager |
||
| 131 | * @param string $stream |
||
| 132 | * @see http://www.php.net/manual/en/reserved.variables.php |
||
| 133 | */ |
||
| 134 | public function __construct(array $vars=array(), |
||
| 163 | /** |
||
| 164 | * @param array $parameters |
||
| 165 | */ |
||
| 166 | public function setUrlParameters(array $parameters) { |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Countable method |
||
| 176 | * @return int |
||
| 177 | */ |
||
| 178 | public function count() { |
||
| 181 | |||
| 182 | /** |
||
| 183 | * ArrayAccess methods |
||
| 184 | * |
||
| 185 | * Gives access to the combined GET, POST and urlParams arrays |
||
| 186 | * |
||
| 187 | * Examples: |
||
| 188 | * |
||
| 189 | * $var = $request['myvar']; |
||
| 190 | * |
||
| 191 | * or |
||
| 192 | * |
||
| 193 | * if(!isset($request['myvar']) { |
||
| 194 | * // Do something |
||
| 195 | * } |
||
| 196 | * |
||
| 197 | * $request['myvar'] = 'something'; // This throws an exception. |
||
| 198 | * |
||
| 199 | * @param string $offset The key to lookup |
||
| 200 | * @return boolean |
||
| 201 | */ |
||
| 202 | public function offsetExists($offset) { |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @see offsetExists |
||
| 208 | */ |
||
| 209 | public function offsetGet($offset) { |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @see offsetExists |
||
| 217 | */ |
||
| 218 | public function offsetSet($offset, $value) { |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @see offsetExists |
||
| 224 | */ |
||
| 225 | public function offsetUnset($offset) { |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Magic property accessors |
||
| 231 | * @param string $name |
||
| 232 | * @param mixed $value |
||
| 233 | */ |
||
| 234 | public function __set($name, $value) { |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Access request variables by method and name. |
||
| 240 | * Examples: |
||
| 241 | * |
||
| 242 | * $request->post['myvar']; // Only look for POST variables |
||
| 243 | * $request->myvar; or $request->{'myvar'}; or $request->{$myvar} |
||
| 244 | * Looks in the combined GET, POST and urlParams array. |
||
| 245 | * |
||
| 246 | * If you access e.g. ->post but the current HTTP request method |
||
| 247 | * is GET a \LogicException will be thrown. |
||
| 248 | * |
||
| 249 | * @param string $name The key to look for. |
||
| 250 | * @throws \LogicException |
||
| 251 | * @return mixed|null |
||
| 252 | */ |
||
| 253 | public function __get($name) { |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @param string $name |
||
| 284 | * @return bool |
||
| 285 | */ |
||
| 286 | public function __isset($name) { |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @param string $id |
||
| 295 | */ |
||
| 296 | public function __unset($id) { |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Returns the value for a specific http header. |
||
| 302 | * |
||
| 303 | * This method returns null if the header did not exist. |
||
| 304 | * |
||
| 305 | * @param string $name |
||
| 306 | * @return string |
||
| 307 | */ |
||
| 308 | public function getHeader($name) { |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Lets you access post and get parameters by the index |
||
| 332 | * In case of json requests the encoded json body is accessed |
||
| 333 | * |
||
| 334 | * @param string $key the key which you want to access in the URL Parameter |
||
| 335 | * placeholder, $_POST or $_GET array. |
||
| 336 | * The priority how they're returned is the following: |
||
| 337 | * 1. URL parameters |
||
| 338 | * 2. POST parameters |
||
| 339 | * 3. GET parameters |
||
| 340 | * @param mixed $default If the key is not found, this value will be returned |
||
| 341 | * @return mixed the content of the array |
||
| 342 | */ |
||
| 343 | public function getParam($key, $default = null) { |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Returns all params that were received, be it from the request |
||
| 351 | * (as GET or POST) or throuh the URL by the route |
||
| 352 | * @return array the array with all parameters |
||
| 353 | */ |
||
| 354 | public function getParams() { |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Returns the method of the request |
||
| 360 | * @return string the method of the request (POST, GET, etc) |
||
| 361 | */ |
||
| 362 | public function getMethod() { |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Shortcut for accessing an uploaded file through the $_FILES array |
||
| 368 | * @param string $key the key that will be taken from the $_FILES array |
||
| 369 | * @return array the file in the $_FILES element |
||
| 370 | */ |
||
| 371 | public function getUploadedFile($key) { |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Shortcut for getting env variables |
||
| 377 | * @param string $key the key that will be taken from the $_ENV array |
||
| 378 | * @return array the value in the $_ENV element |
||
| 379 | */ |
||
| 380 | public function getEnv($key) { |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Shortcut for getting cookie variables |
||
| 386 | * @param string $key the key that will be taken from the $_COOKIE array |
||
| 387 | * @return string the value in the $_COOKIE element |
||
| 388 | */ |
||
| 389 | public function getCookie($key) { |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Returns the request body content. |
||
| 395 | * |
||
| 396 | * If the HTTP request method is PUT and the body |
||
| 397 | * not application/x-www-form-urlencoded or application/json a stream |
||
| 398 | * resource is returned, otherwise an array. |
||
| 399 | * |
||
| 400 | * @return array|string|resource The request body content or a resource to read the body stream. |
||
| 401 | * |
||
| 402 | * @throws \LogicException |
||
| 403 | */ |
||
| 404 | protected function getContent() { |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Attempt to decode the content and populate parameters |
||
| 428 | */ |
||
| 429 | protected function decodeContent() { |
||
| 462 | |||
| 463 | |||
| 464 | /** |
||
| 465 | * Checks if the CSRF check was correct |
||
| 466 | * @return bool true if CSRF check passed |
||
| 467 | */ |
||
| 468 | public function passesCSRFCheck() { |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Whether the cookie checks are required |
||
| 494 | * |
||
| 495 | * @return bool |
||
| 496 | */ |
||
| 497 | private function cookieCheckRequired() { |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Wrapper around session_get_cookie_params |
||
| 510 | * |
||
| 511 | * @return array |
||
| 512 | */ |
||
| 513 | public function getCookieParams() { |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Appends the __Host- prefix to the cookie if applicable |
||
| 519 | * |
||
| 520 | * @param string $name |
||
| 521 | * @return string |
||
| 522 | */ |
||
| 523 | protected function getProtectedCookieName($name) { |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Checks if the strict cookie has been sent with the request if the request |
||
| 535 | * is including any cookies. |
||
| 536 | * |
||
| 537 | * @return bool |
||
| 538 | * @since 9.1.0 |
||
| 539 | */ |
||
| 540 | View Code Duplication | public function passesStrictCookieCheck() { |
|
| 552 | |||
| 553 | /** |
||
| 554 | * Checks if the lax cookie has been sent with the request if the request |
||
| 555 | * is including any cookies. |
||
| 556 | * |
||
| 557 | * @return bool |
||
| 558 | * @since 9.1.0 |
||
| 559 | */ |
||
| 560 | View Code Duplication | public function passesLaxCookieCheck() { |
|
| 571 | |||
| 572 | |||
| 573 | /** |
||
| 574 | * Returns an ID for the request, value is not guaranteed to be unique and is mostly meant for logging |
||
| 575 | * If `mod_unique_id` is installed this value will be taken. |
||
| 576 | * @return string |
||
| 577 | */ |
||
| 578 | public function getId() { |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Returns the remote address, if the connection came from a trusted proxy |
||
| 593 | * and `forwarded_for_headers` has been configured then the IP address |
||
| 594 | * specified in this header will be returned instead. |
||
| 595 | * Do always use this instead of $_SERVER['REMOTE_ADDR'] |
||
| 596 | * @return string IP address |
||
| 597 | */ |
||
| 598 | public function getRemoteAddress() { |
||
| 622 | |||
| 623 | /** |
||
| 624 | * Check overwrite condition |
||
| 625 | * @param string $type |
||
| 626 | * @return bool |
||
| 627 | */ |
||
| 628 | private function isOverwriteCondition($type = '') { |
||
| 634 | |||
| 635 | /** |
||
| 636 | * Returns the server protocol. It respects one or more reverse proxies servers |
||
| 637 | * and load balancers |
||
| 638 | * @return string Server protocol (http or https) |
||
| 639 | */ |
||
| 640 | public function getServerProtocol() { |
||
| 668 | |||
| 669 | /** |
||
| 670 | * Returns the used HTTP protocol. |
||
| 671 | * |
||
| 672 | * @return string HTTP protocol. HTTP/2, HTTP/1.1 or HTTP/1.0. |
||
| 673 | */ |
||
| 674 | View Code Duplication | public function getHttpProtocol() { |
|
| 689 | |||
| 690 | /** |
||
| 691 | * Returns the request uri, even if the website uses one or more |
||
| 692 | * reverse proxies |
||
| 693 | * @return string |
||
| 694 | */ |
||
| 695 | public function getRequestUri() { |
||
| 702 | |||
| 703 | /** |
||
| 704 | * Get raw PathInfo from request (not urldecoded) |
||
| 705 | * @throws \Exception |
||
| 706 | * @return string Path info |
||
| 707 | */ |
||
| 708 | public function getRawPathInfo() { |
||
| 747 | |||
| 748 | /** |
||
| 749 | * Get PathInfo from request |
||
| 750 | * @throws \Exception |
||
| 751 | * @return string|false Path info or false when not found |
||
| 752 | */ |
||
| 753 | public function getPathInfo() { |
||
| 767 | |||
| 768 | /** |
||
| 769 | * Returns the script name, even if the website uses one or more |
||
| 770 | * reverse proxies |
||
| 771 | * @return string the script name |
||
| 772 | */ |
||
| 773 | public function getScriptName() { |
||
| 784 | |||
| 785 | /** |
||
| 786 | * Checks whether the user agent matches a given regex |
||
| 787 | * @param array $agent array of agent names |
||
| 788 | * @return bool true if at least one of the given agent matches, false otherwise |
||
| 789 | */ |
||
| 790 | public function isUserAgent(array $agent) { |
||
| 801 | |||
| 802 | /** |
||
| 803 | * Returns the unverified server host from the headers without checking |
||
| 804 | * whether it is a trusted domain |
||
| 805 | * @return string Server host |
||
| 806 | */ |
||
| 807 | public function getInsecureServerHost() { |
||
| 825 | |||
| 826 | |||
| 827 | /** |
||
| 828 | * Returns the server host from the headers, or the first configured |
||
| 829 | * trusted domain if the host isn't in the trusted list |
||
| 830 | * @return string Server host |
||
| 831 | */ |
||
| 832 | public function getServerHost() { |
||
| 857 | |||
| 858 | /** |
||
| 859 | * Returns the overwritehost setting from the config if set and |
||
| 860 | * if the overwrite condition is met |
||
| 861 | * @return string|null overwritehost value or null if not defined or the defined condition |
||
| 862 | * isn't met |
||
| 863 | */ |
||
| 864 | private function getOverwriteHost() { |
||
| 870 | |||
| 871 | } |
||
| 872 |
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.