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 |
||
| 49 | class Request implements \ArrayAccess, \Countable, IRequest { |
||
| 50 | |||
| 51 | const USER_AGENT_IE = '/(MSIE)|(Trident)/'; |
||
| 52 | const USER_AGENT_IE_8 = '/MSIE 8.0/'; |
||
| 53 | // Microsoft Edge User Agent from https://msdn.microsoft.com/en-us/library/hh869301(v=vs.85).aspx |
||
| 54 | 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.]+$/'; |
||
| 55 | // Firefox User Agent from https://developer.mozilla.org/en-US/docs/Web/HTTP/Gecko_user_agent_string_reference |
||
| 56 | const USER_AGENT_FIREFOX = '/^Mozilla\/5\.0 \([^)]+\) Gecko\/[0-9.]+ Firefox\/[0-9.]+$/'; |
||
| 57 | // Chrome User Agent from https://developer.chrome.com/multidevice/user-agent |
||
| 58 | const USER_AGENT_CHROME = '/^Mozilla\/5\.0 \([^)]+\) AppleWebKit\/[0-9.]+ \(KHTML, like Gecko\) Chrome\/[0-9.]+ (Mobile Safari|Safari)\/[0-9.]+$/'; |
||
| 59 | // Safari User Agent from http://www.useragentstring.com/pages/Safari/ |
||
| 60 | const USER_AGENT_SAFARI = '/^Mozilla\/5\.0 \([^)]+\) AppleWebKit\/[0-9.]+ \(KHTML, like Gecko\) Version\/[0-9.]+ Safari\/[0-9.A-Z]+$/'; |
||
| 61 | // Android Chrome user agent: https://developers.google.com/chrome/mobile/docs/user-agent |
||
| 62 | const USER_AGENT_ANDROID_MOBILE_CHROME = '#Android.*Chrome/[.0-9]*#'; |
||
| 63 | const USER_AGENT_FREEBOX = '#^Mozilla/5\.0$#'; |
||
| 64 | const USER_AGENT_OWNCLOUD_IOS = '/^Mozilla\/5\.0 \(iOS\) ownCloud\-iOS.*$/'; |
||
| 65 | const USER_AGENT_OWNCLOUD_ANDROID = '/^Mozilla\/5\.0 \(Android\) ownCloud\-android.*$/'; |
||
| 66 | const USER_AGENT_OWNCLOUD_DESKTOP = '/^Mozilla\/5\.0 \([A-Za-z ]+\) (mirall|csyncoC)\/.*$/'; |
||
| 67 | const REGEX_LOCALHOST = '/^(127\.0\.0\.1|localhost)$/'; |
||
| 68 | |||
| 69 | protected $inputStream; |
||
| 70 | protected $content; |
||
| 71 | protected $items = array(); |
||
| 72 | protected $allowedKeys = array( |
||
| 73 | 'get', |
||
| 74 | 'post', |
||
| 75 | 'files', |
||
| 76 | 'server', |
||
| 77 | 'env', |
||
| 78 | 'cookies', |
||
| 79 | 'urlParams', |
||
| 80 | 'parameters', |
||
| 81 | 'method', |
||
| 82 | 'requesttoken', |
||
| 83 | ); |
||
| 84 | /** @var ISecureRandom */ |
||
| 85 | protected $secureRandom; |
||
| 86 | /** @var IConfig */ |
||
| 87 | protected $config; |
||
| 88 | /** @var string */ |
||
| 89 | protected $requestId = ''; |
||
| 90 | /** @var ICrypto */ |
||
| 91 | protected $crypto; |
||
| 92 | /** @var CsrfTokenManager|null */ |
||
| 93 | protected $csrfTokenManager; |
||
| 94 | |||
| 95 | /** @var bool */ |
||
| 96 | protected $contentDecoded = false; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @param array $vars An associative array with the following optional values: |
||
| 100 | * - array 'urlParams' the parameters which were matched from the URL |
||
| 101 | * - array 'get' the $_GET array |
||
| 102 | * - array|string 'post' the $_POST array or JSON string |
||
| 103 | * - array 'files' the $_FILES array |
||
| 104 | * - array 'server' the $_SERVER array |
||
| 105 | * - array 'env' the $_ENV array |
||
| 106 | * - array 'cookies' the $_COOKIE array |
||
| 107 | * - string 'method' the request method (GET, POST etc) |
||
| 108 | * - string|false 'requesttoken' the requesttoken or false when not available |
||
| 109 | * @param ISecureRandom $secureRandom |
||
| 110 | * @param IConfig $config |
||
| 111 | * @param CsrfTokenManager|null $csrfTokenManager |
||
| 112 | * @param string $stream |
||
| 113 | * @see http://www.php.net/manual/en/reserved.variables.php |
||
| 114 | */ |
||
| 115 | public function __construct(array $vars=array(), |
||
| 144 | /** |
||
| 145 | * @param array $parameters |
||
| 146 | */ |
||
| 147 | public function setUrlParameters(array $parameters) { |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Countable method |
||
| 157 | * @return int |
||
| 158 | */ |
||
| 159 | public function count() { |
||
| 162 | |||
| 163 | /** |
||
| 164 | * ArrayAccess methods |
||
| 165 | * |
||
| 166 | * Gives access to the combined GET, POST and urlParams arrays |
||
| 167 | * |
||
| 168 | * Examples: |
||
| 169 | * |
||
| 170 | * $var = $request['myvar']; |
||
| 171 | * |
||
| 172 | * or |
||
| 173 | * |
||
| 174 | * if(!isset($request['myvar']) { |
||
| 175 | * // Do something |
||
| 176 | * } |
||
| 177 | * |
||
| 178 | * $request['myvar'] = 'something'; // This throws an exception. |
||
| 179 | * |
||
| 180 | * @param string $offset The key to lookup |
||
| 181 | * @return boolean |
||
| 182 | */ |
||
| 183 | public function offsetExists($offset) { |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @see offsetExists |
||
| 189 | */ |
||
| 190 | public function offsetGet($offset) { |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @see offsetExists |
||
| 198 | */ |
||
| 199 | public function offsetSet($offset, $value) { |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @see offsetExists |
||
| 205 | */ |
||
| 206 | public function offsetUnset($offset) { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Magic property accessors |
||
| 212 | * @param string $name |
||
| 213 | * @param mixed $value |
||
| 214 | */ |
||
| 215 | public function __set($name, $value) { |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Access request variables by method and name. |
||
| 221 | * Examples: |
||
| 222 | * |
||
| 223 | * $request->post['myvar']; // Only look for POST variables |
||
| 224 | * $request->myvar; or $request->{'myvar'}; or $request->{$myvar} |
||
| 225 | * Looks in the combined GET, POST and urlParams array. |
||
| 226 | * |
||
| 227 | * If you access e.g. ->post but the current HTTP request method |
||
| 228 | * is GET a \LogicException will be thrown. |
||
| 229 | * |
||
| 230 | * @param string $name The key to look for. |
||
| 231 | * @throws \LogicException |
||
| 232 | * @return mixed|null |
||
| 233 | */ |
||
| 234 | public function __get($name) { |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @param string $name |
||
| 265 | * @return bool |
||
| 266 | */ |
||
| 267 | public function __isset($name) { |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @param string $id |
||
| 276 | */ |
||
| 277 | public function __unset($id) { |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Returns the value for a specific http header. |
||
| 283 | * |
||
| 284 | * This method returns null if the header did not exist. |
||
| 285 | * |
||
| 286 | * @param string $name |
||
| 287 | * @return string |
||
| 288 | */ |
||
| 289 | public function getHeader($name) { |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Lets you access post and get parameters by the index |
||
| 313 | * In case of json requests the encoded json body is accessed |
||
| 314 | * |
||
| 315 | * @param string $key the key which you want to access in the URL Parameter |
||
| 316 | * placeholder, $_POST or $_GET array. |
||
| 317 | * The priority how they're returned is the following: |
||
| 318 | * 1. URL parameters |
||
| 319 | * 2. POST parameters |
||
| 320 | * 3. GET parameters |
||
| 321 | * @param mixed $default If the key is not found, this value will be returned |
||
| 322 | * @return mixed the content of the array |
||
| 323 | */ |
||
| 324 | public function getParam($key, $default = null) { |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Returns all params that were received, be it from the request |
||
| 332 | * (as GET or POST) or throuh the URL by the route |
||
| 333 | * @return array the array with all parameters |
||
| 334 | */ |
||
| 335 | public function getParams() { |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Returns the method of the request |
||
| 341 | * @return string the method of the request (POST, GET, etc) |
||
| 342 | */ |
||
| 343 | public function getMethod() { |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Shortcut for accessing an uploaded file through the $_FILES array |
||
| 349 | * @param string $key the key that will be taken from the $_FILES array |
||
| 350 | * @return array the file in the $_FILES element |
||
| 351 | */ |
||
| 352 | public function getUploadedFile($key) { |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Shortcut for getting env variables |
||
| 358 | * @param string $key the key that will be taken from the $_ENV array |
||
| 359 | * @return array the value in the $_ENV element |
||
| 360 | */ |
||
| 361 | public function getEnv($key) { |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Shortcut for getting cookie variables |
||
| 367 | * @param string $key the key that will be taken from the $_COOKIE array |
||
| 368 | * @return array the value in the $_COOKIE element |
||
| 369 | */ |
||
| 370 | public function getCookie($key) { |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Returns the request body content. |
||
| 376 | * |
||
| 377 | * If the HTTP request method is PUT and the body |
||
| 378 | * not application/x-www-form-urlencoded or application/json a stream |
||
| 379 | * resource is returned, otherwise an array. |
||
| 380 | * |
||
| 381 | * @return array|string|resource The request body content or a resource to read the body stream. |
||
| 382 | * |
||
| 383 | * @throws \LogicException |
||
| 384 | */ |
||
| 385 | protected function getContent() { |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Attempt to decode the content and populate parameters |
||
| 407 | */ |
||
| 408 | protected function decodeContent() { |
||
| 441 | |||
| 442 | |||
| 443 | /** |
||
| 444 | * Checks if the CSRF check was correct |
||
| 445 | * @return bool true if CSRF check passed |
||
| 446 | */ |
||
| 447 | public function passesCSRFCheck() { |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Checks if the strict cookie has been sent with the request if the request |
||
| 473 | * is including any cookies. |
||
| 474 | * |
||
| 475 | * @return bool |
||
| 476 | * @since 9.1.0 |
||
| 477 | */ |
||
| 478 | public function passesStrictCookieCheck() { |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Checks if the lax cookie has been sent with the request if the request |
||
| 492 | * is including any cookies. |
||
| 493 | * |
||
| 494 | * @return bool |
||
| 495 | * @since 9.1.0 |
||
| 496 | */ |
||
| 497 | public function passesLaxCookieCheck() { |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Returns an ID for the request, value is not guaranteed to be unique and is mostly meant for logging |
||
| 511 | * If `mod_unique_id` is installed this value will be taken. |
||
| 512 | * @return string |
||
| 513 | */ |
||
| 514 | public function getId() { |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Returns the remote address, if the connection came from a trusted proxy |
||
| 528 | * and `forwarded_for_headers` has been configured then the IP address |
||
| 529 | * specified in this header will be returned instead. |
||
| 530 | * Do always use this instead of $_SERVER['REMOTE_ADDR'] |
||
| 531 | * @return string IP address |
||
| 532 | */ |
||
| 533 | public function getRemoteAddress() { |
||
| 557 | |||
| 558 | /** |
||
| 559 | * Check overwrite condition |
||
| 560 | * @param string $type |
||
| 561 | * @return bool |
||
| 562 | */ |
||
| 563 | private function isOverwriteCondition($type = '') { |
||
| 569 | |||
| 570 | /** |
||
| 571 | * Returns the server protocol. It respects one or more reverse proxies servers |
||
| 572 | * and load balancers |
||
| 573 | * @return string Server protocol (http or https) |
||
| 574 | */ |
||
| 575 | public function getServerProtocol() { |
||
| 603 | |||
| 604 | /** |
||
| 605 | * Returns the used HTTP protocol. |
||
| 606 | * |
||
| 607 | * @return string HTTP protocol. HTTP/2, HTTP/1.1 or HTTP/1.0. |
||
| 608 | */ |
||
| 609 | View Code Duplication | public function getHttpProtocol() { |
|
| 624 | |||
| 625 | /** |
||
| 626 | * Returns the request uri, even if the website uses one or more |
||
| 627 | * reverse proxies |
||
| 628 | * @return string |
||
| 629 | */ |
||
| 630 | public function getRequestUri() { |
||
| 637 | |||
| 638 | /** |
||
| 639 | * Get raw PathInfo from request (not urldecoded) |
||
| 640 | * @throws \Exception |
||
| 641 | * @return string Path info |
||
| 642 | */ |
||
| 643 | public function getRawPathInfo() { |
||
| 682 | |||
| 683 | /** |
||
| 684 | * Get PathInfo from request |
||
| 685 | * @throws \Exception |
||
| 686 | * @return string|false Path info or false when not found |
||
| 687 | */ |
||
| 688 | public function getPathInfo() { |
||
| 706 | |||
| 707 | /** |
||
| 708 | * Returns the script name, even if the website uses one or more |
||
| 709 | * reverse proxies |
||
| 710 | * @return string the script name |
||
| 711 | */ |
||
| 712 | public function getScriptName() { |
||
| 723 | |||
| 724 | /** |
||
| 725 | * Checks whether the user agent matches a given regex |
||
| 726 | * @param array $agent array of agent names |
||
| 727 | * @return bool true if at least one of the given agent matches, false otherwise |
||
| 728 | */ |
||
| 729 | public function isUserAgent(array $agent) { |
||
| 740 | |||
| 741 | /** |
||
| 742 | * Returns the unverified server host from the headers without checking |
||
| 743 | * whether it is a trusted domain |
||
| 744 | * @return string Server host |
||
| 745 | */ |
||
| 746 | public function getInsecureServerHost() { |
||
| 764 | |||
| 765 | |||
| 766 | /** |
||
| 767 | * Returns the server host from the headers, or the first configured |
||
| 768 | * trusted domain if the host isn't in the trusted list |
||
| 769 | * @return string Server host |
||
| 770 | */ |
||
| 771 | public function getServerHost() { |
||
| 796 | |||
| 797 | /** |
||
| 798 | * Returns the overwritehost setting from the config if set and |
||
| 799 | * if the overwrite condition is met |
||
| 800 | * @return string|null overwritehost value or null if not defined or the defined condition |
||
| 801 | * isn't met |
||
| 802 | */ |
||
| 803 | private function getOverwriteHost() { |
||
| 809 | |||
| 810 | } |
||
| 811 |
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.