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 | const USER_AGENT_IE = '/(MSIE)|(Trident)/'; |
||
57 | const USER_AGENT_IE_8 = '/MSIE 8.0/'; |
||
58 | // Microsoft Edge User Agent from https://msdn.microsoft.com/en-us/library/hh869301(v=vs.85).aspx |
||
59 | 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.]+$/'; |
||
60 | // Firefox User Agent from https://developer.mozilla.org/en-US/docs/Web/HTTP/Gecko_user_agent_string_reference |
||
61 | const USER_AGENT_FIREFOX = '/^Mozilla\/5\.0 \([^)]+\) Gecko\/[0-9.]+ Firefox\/[0-9.]+$/'; |
||
62 | // Chrome User Agent from https://developer.chrome.com/multidevice/user-agent |
||
63 | const USER_AGENT_CHROME = '/^Mozilla\/5\.0 \([^)]+\) AppleWebKit\/[0-9.]+ \(KHTML, like Gecko\) Chrome\/[0-9.]+ (Mobile Safari|Safari)\/[0-9.]+$/'; |
||
64 | // Safari User Agent from http://www.useragentstring.com/pages/Safari/ |
||
65 | const USER_AGENT_SAFARI = '/^Mozilla\/5\.0 \([^)]+\) AppleWebKit\/[0-9.]+ \(KHTML, like Gecko\) Version\/[0-9.]+ Safari\/[0-9.A-Z]+$/'; |
||
66 | // Android Chrome user agent: https://developers.google.com/chrome/mobile/docs/user-agent |
||
67 | const USER_AGENT_ANDROID_MOBILE_CHROME = '#Android.*Chrome/[.0-9]*#'; |
||
68 | const USER_AGENT_FREEBOX = '#^Mozilla/5\.0$#'; |
||
69 | const USER_AGENT_OWNCLOUD_IOS = '/^Mozilla\/5\.0 \(iOS\) ownCloud\-iOS.*$/'; |
||
70 | const USER_AGENT_OWNCLOUD_ANDROID = '/^Mozilla\/5\.0 \(Android\) ownCloud\-android.*$/'; |
||
71 | const USER_AGENT_OWNCLOUD_DESKTOP = '/^Mozilla\/5\.0 \([A-Za-z ]+\) (mirall|csyncoC)\/.*$/'; |
||
72 | const REGEX_LOCALHOST = '/^(127\.0\.0\.1|localhost)$/'; |
||
73 | |||
74 | protected $inputStream; |
||
75 | protected $content; |
||
76 | protected $items = []; |
||
77 | protected $allowedKeys = [ |
||
78 | 'get', |
||
79 | 'post', |
||
80 | 'files', |
||
81 | 'server', |
||
82 | 'env', |
||
83 | 'cookies', |
||
84 | 'urlParams', |
||
85 | 'parameters', |
||
86 | 'method', |
||
87 | 'requesttoken', |
||
88 | ]; |
||
89 | /** @var ISecureRandom */ |
||
90 | protected $secureRandom; |
||
91 | /** @var IConfig */ |
||
92 | protected $config; |
||
93 | /** @var string */ |
||
94 | protected $requestId = ''; |
||
95 | /** @var ICrypto */ |
||
96 | protected $crypto; |
||
97 | /** @var CsrfTokenManager|null */ |
||
98 | protected $csrfTokenManager; |
||
99 | |||
100 | /** @var bool */ |
||
101 | protected $contentDecoded = false; |
||
102 | |||
103 | /** |
||
104 | * @param array $vars An associative array with the following optional values: |
||
105 | * - array 'urlParams' the parameters which were matched from the URL |
||
106 | * - array 'get' the $_GET array |
||
107 | * - array|string 'post' the $_POST array or JSON string |
||
108 | * - array 'files' the $_FILES array |
||
109 | * - array 'server' the $_SERVER array |
||
110 | * - array 'env' the $_ENV array |
||
111 | * - array 'cookies' the $_COOKIE array |
||
112 | * - string 'method' the request method (GET, POST etc) |
||
113 | * - string|false 'requesttoken' the requesttoken or false when not available |
||
114 | * @param ISecureRandom $secureRandom |
||
115 | * @param IConfig $config |
||
116 | * @param CsrfTokenManager|null $csrfTokenManager |
||
117 | * @param string $stream |
||
118 | * @see http://www.php.net/manual/en/reserved.variables.php |
||
119 | */ |
||
120 | public function __construct(array $vars= [], |
||
148 | /** |
||
149 | * @param array $parameters |
||
150 | */ |
||
151 | public function setUrlParameters(array $parameters) { |
||
158 | |||
159 | /** |
||
160 | * Countable method |
||
161 | * @return int |
||
162 | */ |
||
163 | public function count() { |
||
166 | |||
167 | /** |
||
168 | * ArrayAccess methods |
||
169 | * |
||
170 | * Gives access to the combined GET, POST and urlParams arrays |
||
171 | * |
||
172 | * Examples: |
||
173 | * |
||
174 | * $var = $request['myvar']; |
||
175 | * |
||
176 | * or |
||
177 | * |
||
178 | * if(!isset($request['myvar']) { |
||
179 | * // Do something |
||
180 | * } |
||
181 | * |
||
182 | * $request['myvar'] = 'something'; // This throws an exception. |
||
183 | * |
||
184 | * @param string $offset The key to lookup |
||
185 | * @return boolean |
||
186 | */ |
||
187 | public function offsetExists($offset) { |
||
190 | |||
191 | /** |
||
192 | * @see offsetExists |
||
193 | */ |
||
194 | public function offsetGet($offset) { |
||
199 | |||
200 | /** |
||
201 | * @see offsetExists |
||
202 | */ |
||
203 | public function offsetSet($offset, $value) { |
||
206 | |||
207 | /** |
||
208 | * @see offsetExists |
||
209 | */ |
||
210 | public function offsetUnset($offset) { |
||
213 | |||
214 | /** |
||
215 | * Magic property accessors |
||
216 | * @param string $name |
||
217 | * @param mixed $value |
||
218 | */ |
||
219 | public function __set($name, $value) { |
||
222 | |||
223 | /** |
||
224 | * Access request variables by method and name. |
||
225 | * Examples: |
||
226 | * |
||
227 | * $request->post['myvar']; // Only look for POST variables |
||
228 | * $request->myvar; or $request->{'myvar'}; or $request->{$myvar} |
||
229 | * Looks in the combined GET, POST and urlParams array. |
||
230 | * |
||
231 | * If you access e.g. ->post but the current HTTP request method |
||
232 | * is GET a \LogicException will be thrown. |
||
233 | * |
||
234 | * @param string $name The key to look for. |
||
235 | * @throws \LogicException |
||
236 | * @return mixed|null |
||
237 | */ |
||
238 | public function __get($name) { |
||
266 | |||
267 | /** |
||
268 | * @param string $name |
||
269 | * @return bool |
||
270 | */ |
||
271 | public function __isset($name) { |
||
277 | |||
278 | /** |
||
279 | * @param string $id |
||
280 | */ |
||
281 | public function __unset($id) { |
||
284 | |||
285 | /** |
||
286 | * Returns the value for a specific http header. |
||
287 | * |
||
288 | * This method returns null if the header did not exist. |
||
289 | * |
||
290 | * @param string $name |
||
291 | * @return string |
||
292 | */ |
||
293 | public function getHeader($name) { |
||
313 | |||
314 | /** |
||
315 | * Lets you access post and get parameters by the index |
||
316 | * In case of json requests the encoded json body is accessed |
||
317 | * |
||
318 | * @param string $key the key which you want to access in the URL Parameter |
||
319 | * placeholder, $_POST or $_GET array. |
||
320 | * The priority how they're returned is the following: |
||
321 | * 1. URL parameters |
||
322 | * 2. POST parameters |
||
323 | * 3. GET parameters |
||
324 | * @param mixed $default If the key is not found, this value will be returned |
||
325 | * @return mixed the content of the array |
||
326 | */ |
||
327 | public function getParam($key, $default = null) { |
||
332 | |||
333 | /** |
||
334 | * Returns all params that were received, be it from the request |
||
335 | * (as GET or POST) or throuh the URL by the route |
||
336 | * @return array the array with all parameters |
||
337 | */ |
||
338 | public function getParams() { |
||
341 | |||
342 | /** |
||
343 | * Returns the method of the request |
||
344 | * @return string the method of the request (POST, GET, etc) |
||
345 | */ |
||
346 | public function getMethod() { |
||
349 | |||
350 | /** |
||
351 | * Shortcut for accessing an uploaded file through the $_FILES array |
||
352 | * @param string $key the key that will be taken from the $_FILES array |
||
353 | * @return array the file in the $_FILES element |
||
354 | */ |
||
355 | public function getUploadedFile($key) { |
||
358 | |||
359 | /** |
||
360 | * Shortcut for getting env variables |
||
361 | * @param string $key the key that will be taken from the $_ENV array |
||
362 | * @return array the value in the $_ENV element |
||
363 | */ |
||
364 | public function getEnv($key) { |
||
367 | |||
368 | /** |
||
369 | * Shortcut for getting cookie variables |
||
370 | * @param string $key the key that will be taken from the $_COOKIE array |
||
371 | * @return string the value in the $_COOKIE element |
||
372 | */ |
||
373 | public function getCookie($key) { |
||
376 | |||
377 | /** |
||
378 | * Returns the request body content. |
||
379 | * |
||
380 | * If the HTTP request method is PUT and the body |
||
381 | * not application/x-www-form-urlencoded or application/json a stream |
||
382 | * resource is returned, otherwise an array. |
||
383 | * |
||
384 | * @return array|string|resource The request body content or a resource to read the body stream. |
||
385 | * |
||
386 | * @throws \LogicException |
||
387 | */ |
||
388 | protected function getContent() { |
||
407 | |||
408 | /** |
||
409 | * Attempt to decode the content and populate parameters |
||
410 | */ |
||
411 | protected function decodeContent() { |
||
443 | |||
444 | /** |
||
445 | * Checks if the CSRF check was correct |
||
446 | * @return bool true if CSRF check passed |
||
447 | */ |
||
448 | 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 an X-Request-ID header is sent by the client this value will be taken. |
||
476 | * If `mod_unique_id` is installed this value will be taken. |
||
477 | * @return string |
||
478 | */ |
||
479 | public function getId() { |
||
502 | |||
503 | /** |
||
504 | * Returns the remote address, if the connection came from a trusted proxy |
||
505 | * and `forwarded_for_headers` has been configured then the IP address |
||
506 | * specified in this header will be returned instead. |
||
507 | * Do always use this instead of $_SERVER['REMOTE_ADDR'] |
||
508 | * @return string IP address |
||
509 | */ |
||
510 | public function getRemoteAddress() { |
||
534 | |||
535 | /** |
||
536 | * Check overwrite condition |
||
537 | * @param string $type |
||
538 | * @return bool |
||
539 | */ |
||
540 | private function isOverwriteCondition($type = '') { |
||
546 | |||
547 | /** |
||
548 | * Returns the server protocol. It respects one or more reverse proxies servers |
||
549 | * and load balancers |
||
550 | * @return string Server protocol (http or https) |
||
551 | */ |
||
552 | public function getServerProtocol() { |
||
580 | |||
581 | /** |
||
582 | * Returns the used HTTP protocol. |
||
583 | * |
||
584 | * @return string HTTP protocol. HTTP/2, HTTP/1.1 or HTTP/1.0. |
||
585 | */ |
||
586 | View Code Duplication | public function getHttpProtocol() { |
|
601 | |||
602 | /** |
||
603 | * Returns the request uri, even if the website uses one or more |
||
604 | * reverse proxies |
||
605 | * @return string |
||
606 | */ |
||
607 | public function getRequestUri() { |
||
628 | |||
629 | /** |
||
630 | * Get raw PathInfo from request (not urldecoded) |
||
631 | * @throws \Exception |
||
632 | * @return string Path info |
||
633 | */ |
||
634 | public function getRawPathInfo() { |
||
675 | |||
676 | /** |
||
677 | * Get PathInfo from request |
||
678 | * @throws \Exception |
||
679 | * @return string|false Path info or false when not found |
||
680 | */ |
||
681 | public function getPathInfo() { |
||
700 | |||
701 | /** |
||
702 | * Returns the script name, even if the website uses one or more |
||
703 | * reverse proxies |
||
704 | * @return string the script name |
||
705 | */ |
||
706 | public function getScriptName() { |
||
717 | |||
718 | /** |
||
719 | * Checks whether the user agent matches a given regex |
||
720 | * @param array $agent array of agent names |
||
721 | * @return bool true if at least one of the given agent matches, false otherwise |
||
722 | */ |
||
723 | public function isUserAgent(array $agent) { |
||
734 | |||
735 | /** |
||
736 | * Returns the unverified server host from the headers without checking |
||
737 | * whether it is a trusted domain |
||
738 | * @return string Server host |
||
739 | */ |
||
740 | public function getInsecureServerHost() { |
||
758 | |||
759 | /** |
||
760 | * Returns the server host from the headers, or the first configured |
||
761 | * trusted domain if the host isn't in the trusted list |
||
762 | * @return string Server host |
||
763 | */ |
||
764 | public function getServerHost() { |
||
789 | |||
790 | /** |
||
791 | * Returns the overwritehost setting from the config if set and |
||
792 | * if the overwrite condition is met |
||
793 | * @return string|null overwritehost value or null if not defined or the defined condition |
||
794 | * isn't met |
||
795 | */ |
||
796 | private function getOverwriteHost() { |
||
802 | } |
||
803 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.