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 REGEX_LOCALHOST = '/^(127\.0\.0\.1|localhost)$/'; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @deprecated use \OCP\IRequest::USER_AGENT_CLIENT_IOS instead |
||
| 74 | */ |
||
| 75 | const USER_AGENT_OWNCLOUD_IOS = '/^Mozilla\/5\.0 \(iOS\) ownCloud\-iOS.*$/'; |
||
| 76 | /** |
||
| 77 | * @deprecated use \OCP\IRequest::USER_AGENT_CLIENT_ANDROID instead |
||
| 78 | */ |
||
| 79 | const USER_AGENT_OWNCLOUD_ANDROID = '/^Mozilla\/5\.0 \(Android\) ownCloud\-android.*$/'; |
||
| 80 | /** |
||
| 81 | * @deprecated use \OCP\IRequest::USER_AGENT_CLIENT_DESKTOP instead |
||
| 82 | */ |
||
| 83 | const USER_AGENT_OWNCLOUD_DESKTOP = '/^Mozilla\/5\.0 \([A-Za-z ]+\) (mirall|csyncoC)\/.*$/'; |
||
| 84 | |||
| 85 | protected $inputStream; |
||
| 86 | protected $content; |
||
| 87 | protected $items = array(); |
||
| 88 | protected $allowedKeys = array( |
||
| 89 | 'get', |
||
| 90 | 'post', |
||
| 91 | 'files', |
||
| 92 | 'server', |
||
| 93 | 'env', |
||
| 94 | 'cookies', |
||
| 95 | 'urlParams', |
||
| 96 | 'parameters', |
||
| 97 | 'method', |
||
| 98 | 'requesttoken', |
||
| 99 | ); |
||
| 100 | /** @var ISecureRandom */ |
||
| 101 | protected $secureRandom; |
||
| 102 | /** @var IConfig */ |
||
| 103 | protected $config; |
||
| 104 | /** @var string */ |
||
| 105 | protected $requestId = ''; |
||
| 106 | /** @var ICrypto */ |
||
| 107 | protected $crypto; |
||
| 108 | /** @var CsrfTokenManager|null */ |
||
| 109 | protected $csrfTokenManager; |
||
| 110 | |||
| 111 | /** @var bool */ |
||
| 112 | protected $contentDecoded = false; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @param array $vars An associative array with the following optional values: |
||
| 116 | * - array 'urlParams' the parameters which were matched from the URL |
||
| 117 | * - array 'get' the $_GET array |
||
| 118 | * - array|string 'post' the $_POST array or JSON string |
||
| 119 | * - array 'files' the $_FILES array |
||
| 120 | * - array 'server' the $_SERVER array |
||
| 121 | * - array 'env' the $_ENV array |
||
| 122 | * - array 'cookies' the $_COOKIE array |
||
| 123 | * - string 'method' the request method (GET, POST etc) |
||
| 124 | * - string|false 'requesttoken' the requesttoken or false when not available |
||
| 125 | * @param ISecureRandom $secureRandom |
||
| 126 | * @param IConfig $config |
||
| 127 | * @param CsrfTokenManager|null $csrfTokenManager |
||
| 128 | * @param string $stream |
||
| 129 | * @see http://www.php.net/manual/en/reserved.variables.php |
||
| 130 | */ |
||
| 131 | public function __construct(array $vars=array(), |
||
| 160 | /** |
||
| 161 | * @param array $parameters |
||
| 162 | */ |
||
| 163 | public function setUrlParameters(array $parameters) { |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Countable method |
||
| 173 | * @return int |
||
| 174 | */ |
||
| 175 | public function count() { |
||
| 178 | |||
| 179 | /** |
||
| 180 | * ArrayAccess methods |
||
| 181 | * |
||
| 182 | * Gives access to the combined GET, POST and urlParams arrays |
||
| 183 | * |
||
| 184 | * Examples: |
||
| 185 | * |
||
| 186 | * $var = $request['myvar']; |
||
| 187 | * |
||
| 188 | * or |
||
| 189 | * |
||
| 190 | * if(!isset($request['myvar']) { |
||
| 191 | * // Do something |
||
| 192 | * } |
||
| 193 | * |
||
| 194 | * $request['myvar'] = 'something'; // This throws an exception. |
||
| 195 | * |
||
| 196 | * @param string $offset The key to lookup |
||
| 197 | * @return boolean |
||
| 198 | */ |
||
| 199 | public function offsetExists($offset) { |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @see offsetExists |
||
| 205 | */ |
||
| 206 | public function offsetGet($offset) { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @see offsetExists |
||
| 214 | */ |
||
| 215 | public function offsetSet($offset, $value) { |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @see offsetExists |
||
| 221 | */ |
||
| 222 | public function offsetUnset($offset) { |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Magic property accessors |
||
| 228 | * @param string $name |
||
| 229 | * @param mixed $value |
||
| 230 | */ |
||
| 231 | public function __set($name, $value) { |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Access request variables by method and name. |
||
| 237 | * Examples: |
||
| 238 | * |
||
| 239 | * $request->post['myvar']; // Only look for POST variables |
||
| 240 | * $request->myvar; or $request->{'myvar'}; or $request->{$myvar} |
||
| 241 | * Looks in the combined GET, POST and urlParams array. |
||
| 242 | * |
||
| 243 | * If you access e.g. ->post but the current HTTP request method |
||
| 244 | * is GET a \LogicException will be thrown. |
||
| 245 | * |
||
| 246 | * @param string $name The key to look for. |
||
| 247 | * @throws \LogicException |
||
| 248 | * @return mixed|null |
||
| 249 | */ |
||
| 250 | public function __get($name) { |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @param string $name |
||
| 281 | * @return bool |
||
| 282 | */ |
||
| 283 | public function __isset($name) { |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param string $id |
||
| 292 | */ |
||
| 293 | public function __unset($id) { |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Returns the value for a specific http header. |
||
| 299 | * |
||
| 300 | * This method returns null if the header did not exist. |
||
| 301 | * |
||
| 302 | * @param string $name |
||
| 303 | * @return string |
||
| 304 | */ |
||
| 305 | public function getHeader($name) { |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Lets you access post and get parameters by the index |
||
| 329 | * In case of json requests the encoded json body is accessed |
||
| 330 | * |
||
| 331 | * @param string $key the key which you want to access in the URL Parameter |
||
| 332 | * placeholder, $_POST or $_GET array. |
||
| 333 | * The priority how they're returned is the following: |
||
| 334 | * 1. URL parameters |
||
| 335 | * 2. POST parameters |
||
| 336 | * 3. GET parameters |
||
| 337 | * @param mixed $default If the key is not found, this value will be returned |
||
| 338 | * @return mixed the content of the array |
||
| 339 | */ |
||
| 340 | public function getParam($key, $default = null) { |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Returns all params that were received, be it from the request |
||
| 348 | * (as GET or POST) or throuh the URL by the route |
||
| 349 | * @return array the array with all parameters |
||
| 350 | */ |
||
| 351 | public function getParams() { |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Returns the method of the request |
||
| 357 | * @return string the method of the request (POST, GET, etc) |
||
| 358 | */ |
||
| 359 | public function getMethod() { |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Shortcut for accessing an uploaded file through the $_FILES array |
||
| 365 | * @param string $key the key that will be taken from the $_FILES array |
||
| 366 | * @return array the file in the $_FILES element |
||
| 367 | */ |
||
| 368 | public function getUploadedFile($key) { |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Shortcut for getting env variables |
||
| 374 | * @param string $key the key that will be taken from the $_ENV array |
||
| 375 | * @return array the value in the $_ENV element |
||
| 376 | */ |
||
| 377 | public function getEnv($key) { |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Shortcut for getting cookie variables |
||
| 383 | * @param string $key the key that will be taken from the $_COOKIE array |
||
| 384 | * @return string the value in the $_COOKIE element |
||
| 385 | */ |
||
| 386 | public function getCookie($key) { |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Returns the request body content. |
||
| 392 | * |
||
| 393 | * If the HTTP request method is PUT and the body |
||
| 394 | * not application/x-www-form-urlencoded or application/json a stream |
||
| 395 | * resource is returned, otherwise an array. |
||
| 396 | * |
||
| 397 | * @return array|string|resource The request body content or a resource to read the body stream. |
||
| 398 | * |
||
| 399 | * @throws \LogicException |
||
| 400 | */ |
||
| 401 | protected function getContent() { |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Attempt to decode the content and populate parameters |
||
| 423 | */ |
||
| 424 | protected function decodeContent() { |
||
| 457 | |||
| 458 | |||
| 459 | /** |
||
| 460 | * Checks if the CSRF check was correct |
||
| 461 | * @return bool true if CSRF check passed |
||
| 462 | */ |
||
| 463 | public function passesCSRFCheck() { |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Checks if the strict cookie has been sent with the request if the request |
||
| 489 | * is including any cookies. |
||
| 490 | * |
||
| 491 | * @return bool |
||
| 492 | * @since 9.1.0 |
||
| 493 | */ |
||
| 494 | public function passesStrictCookieCheck() { |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Checks if the lax cookie has been sent with the request if the request |
||
| 507 | * is including any cookies. |
||
| 508 | * |
||
| 509 | * @return bool |
||
| 510 | * @since 9.1.0 |
||
| 511 | */ |
||
| 512 | public function passesLaxCookieCheck() { |
||
| 521 | |||
| 522 | |||
| 523 | /** |
||
| 524 | * Returns an ID for the request, value is not guaranteed to be unique and is mostly meant for logging |
||
| 525 | * If `mod_unique_id` is installed this value will be taken. |
||
| 526 | * @return string |
||
| 527 | */ |
||
| 528 | public function getId() { |
||
| 539 | |||
| 540 | /** |
||
| 541 | * Returns the remote address, if the connection came from a trusted proxy |
||
| 542 | * and `forwarded_for_headers` has been configured then the IP address |
||
| 543 | * specified in this header will be returned instead. |
||
| 544 | * Do always use this instead of $_SERVER['REMOTE_ADDR'] |
||
| 545 | * @return string IP address |
||
| 546 | */ |
||
| 547 | public function getRemoteAddress() { |
||
| 571 | |||
| 572 | /** |
||
| 573 | * Check overwrite condition |
||
| 574 | * @param string $type |
||
| 575 | * @return bool |
||
| 576 | */ |
||
| 577 | private function isOverwriteCondition($type = '') { |
||
| 583 | |||
| 584 | /** |
||
| 585 | * Returns the server protocol. It respects one or more reverse proxies servers |
||
| 586 | * and load balancers |
||
| 587 | * @return string Server protocol (http or https) |
||
| 588 | */ |
||
| 589 | public function getServerProtocol() { |
||
| 617 | |||
| 618 | /** |
||
| 619 | * Returns the used HTTP protocol. |
||
| 620 | * |
||
| 621 | * @return string HTTP protocol. HTTP/2, HTTP/1.1 or HTTP/1.0. |
||
| 622 | */ |
||
| 623 | View Code Duplication | public function getHttpProtocol() { |
|
| 638 | |||
| 639 | /** |
||
| 640 | * Returns the request uri, even if the website uses one or more |
||
| 641 | * reverse proxies |
||
| 642 | * @return string |
||
| 643 | */ |
||
| 644 | public function getRequestUri() { |
||
| 651 | |||
| 652 | /** |
||
| 653 | * Get raw PathInfo from request (not urldecoded) |
||
| 654 | * @throws \Exception |
||
| 655 | * @return string Path info |
||
| 656 | */ |
||
| 657 | public function getRawPathInfo() { |
||
| 696 | |||
| 697 | /** |
||
| 698 | * Get PathInfo from request |
||
| 699 | * @throws \Exception |
||
| 700 | * @return string|false Path info or false when not found |
||
| 701 | */ |
||
| 702 | public function getPathInfo() { |
||
| 720 | |||
| 721 | /** |
||
| 722 | * Returns the script name, even if the website uses one or more |
||
| 723 | * reverse proxies |
||
| 724 | * @return string the script name |
||
| 725 | */ |
||
| 726 | public function getScriptName() { |
||
| 737 | |||
| 738 | /** |
||
| 739 | * Checks whether the user agent matches a given regex |
||
| 740 | * @param array $agent array of agent names |
||
| 741 | * @return bool true if at least one of the given agent matches, false otherwise |
||
| 742 | */ |
||
| 743 | public function isUserAgent(array $agent) { |
||
| 754 | |||
| 755 | /** |
||
| 756 | * Returns the unverified server host from the headers without checking |
||
| 757 | * whether it is a trusted domain |
||
| 758 | * @return string Server host |
||
| 759 | */ |
||
| 760 | public function getInsecureServerHost() { |
||
| 778 | |||
| 779 | |||
| 780 | /** |
||
| 781 | * Returns the server host from the headers, or the first configured |
||
| 782 | * trusted domain if the host isn't in the trusted list |
||
| 783 | * @return string Server host |
||
| 784 | */ |
||
| 785 | public function getServerHost() { |
||
| 810 | |||
| 811 | /** |
||
| 812 | * Returns the overwritehost setting from the config if set and |
||
| 813 | * if the overwrite condition is met |
||
| 814 | * @return string|null overwritehost value or null if not defined or the defined condition |
||
| 815 | * isn't met |
||
| 816 | */ |
||
| 817 | private function getOverwriteHost() { |
||
| 823 | |||
| 824 | } |
||
| 825 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.