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 HttpRequest 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 HttpRequest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 41 | class HttpRequest |
||
|
|
|||
| 42 | { |
||
| 43 | /** |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | protected $params; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var HttpRequest The reference to *Singleton* instance of this class |
||
| 50 | */ |
||
| 51 | private static $instance; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * The built in detectors used with `is()` can be modified with `addDetector()`. |
||
| 55 | * There are several ways to specify a detector, see HttpRequest::addDetector() for the |
||
| 56 | * various formats and ways to define detectors. |
||
| 57 | * |
||
| 58 | * @var array |
||
| 59 | */ |
||
| 60 | protected $detectors = array( |
||
| 61 | 'get' => array('env' => 'REQUEST_METHOD', 'value' => 'GET'), |
||
| 62 | 'post' => array('env' => 'REQUEST_METHOD', 'value' => 'POST'), |
||
| 63 | 'put' => array('env' => 'REQUEST_METHOD', 'value' => 'PUT'), |
||
| 64 | 'delete' => array('env' => 'REQUEST_METHOD', 'value' => 'DELETE'), |
||
| 65 | 'head' => array('env' => 'REQUEST_METHOD', 'value' => 'HEAD'), |
||
| 66 | 'options' => array('env' => 'REQUEST_METHOD', 'value' => 'OPTIONS'), |
||
| 67 | 'safemethod'=> array('env' => 'REQUEST_METHOD', 'options' => array('GET', 'HEAD')), |
||
| 68 | 'ssl' => array('env' => 'HTTPS', 'value' => 1), |
||
| 69 | 'ajax' => array('env' => 'HTTP_X_REQUESTED_WITH', 'value' => 'XMLHttpRequest'), |
||
| 70 | 'flash' => array('env' => 'HTTP_USER_AGENT', 'pattern' => '/^(Shockwave|Adobe) Flash/'), |
||
| 71 | 'mobile' => array( |
||
| 72 | 'env' => 'HTTP_USER_AGENT', |
||
| 73 | 'options' => array( |
||
| 74 | 'Android', |
||
| 75 | 'AvantGo', |
||
| 76 | 'BlackBerry', |
||
| 77 | 'DoCoMo', |
||
| 78 | 'Fennec', |
||
| 79 | 'iPod', |
||
| 80 | 'iPhone', |
||
| 81 | 'iPad', |
||
| 82 | 'J2ME', |
||
| 83 | 'MIDP', |
||
| 84 | 'NetFront', |
||
| 85 | 'Nokia', |
||
| 86 | 'Opera Mini', |
||
| 87 | 'Opera Mobi', |
||
| 88 | 'PalmOS', |
||
| 89 | 'PalmSource', |
||
| 90 | 'portalmmm', |
||
| 91 | 'Plucker', |
||
| 92 | 'ReqwirelessWeb', |
||
| 93 | 'SonyEricsson', |
||
| 94 | 'Symbian', |
||
| 95 | 'UP\\.Browser', |
||
| 96 | 'webOS', |
||
| 97 | 'Windows CE', |
||
| 98 | 'Windows Phone OS', |
||
| 99 | 'Xiino' |
||
| 100 | ) |
||
| 101 | ), |
||
| 102 | 'robot' => array( |
||
| 103 | 'env' => 'HTTP_USER_AGENT', |
||
| 104 | 'options' => array( |
||
| 105 | /* The most common ones. */ |
||
| 106 | 'Googlebot', |
||
| 107 | 'msnbot', |
||
| 108 | 'Slurp', |
||
| 109 | 'Yahoo', |
||
| 110 | /* The rest alphabetically. */ |
||
| 111 | 'Arachnoidea', |
||
| 112 | 'ArchitextSpider', |
||
| 113 | 'Ask Jeeves', |
||
| 114 | 'B-l-i-t-z-Bot', |
||
| 115 | 'Baiduspider', |
||
| 116 | 'BecomeBot', |
||
| 117 | 'cfetch', |
||
| 118 | 'ConveraCrawler', |
||
| 119 | 'ExtractorPro', |
||
| 120 | 'FAST-WebCrawler', |
||
| 121 | 'FDSE robot', |
||
| 122 | 'fido', |
||
| 123 | 'geckobot', |
||
| 124 | 'Gigabot', |
||
| 125 | 'Girafabot', |
||
| 126 | 'grub-client', |
||
| 127 | 'Gulliver', |
||
| 128 | 'HTTrack', |
||
| 129 | 'ia_archiver', |
||
| 130 | 'InfoSeek', |
||
| 131 | 'kinjabot', |
||
| 132 | 'KIT-Fireball', |
||
| 133 | 'larbin', |
||
| 134 | 'LEIA', |
||
| 135 | 'lmspider', |
||
| 136 | 'Lycos_Spider', |
||
| 137 | 'Mediapartners-Google', |
||
| 138 | 'MuscatFerret', |
||
| 139 | 'NaverBot', |
||
| 140 | 'OmniExplorer_Bot', |
||
| 141 | 'polybot', |
||
| 142 | 'Pompos', |
||
| 143 | 'Scooter', |
||
| 144 | 'Teoma', |
||
| 145 | 'TheSuBot', |
||
| 146 | 'TurnitinBot', |
||
| 147 | 'Ultraseek', |
||
| 148 | 'ViolaBot', |
||
| 149 | 'webbandit', |
||
| 150 | 'www\\.almaden\\.ibm\\.com\\/cs\\/crawler', |
||
| 151 | 'ZyBorg', |
||
| 152 | ) |
||
| 153 | ), |
||
| 154 | ); |
||
| 155 | |||
| 156 | /** |
||
| 157 | * __construct |
||
| 158 | */ |
||
| 159 | private function __construct() |
||
| 174 | |||
| 175 | /** |
||
| 176 | * get singleton instance, establish the request data on first access |
||
| 177 | * |
||
| 178 | * @return HttpRequest |
||
| 179 | */ |
||
| 180 | 10 | public static function getInstance() |
|
| 181 | { |
||
| 182 | 10 | if (null === static::$instance) { |
|
| 183 | static::$instance = new static(); |
||
| 184 | } |
||
| 185 | 10 | return static::$instance; |
|
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * get a http header for the current request |
||
| 190 | * |
||
| 191 | * @param null|string $name header name |
||
| 192 | * |
||
| 193 | * @return null|string |
||
| 194 | */ |
||
| 195 | public function getHeader($name = null) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * get the scheme of current request |
||
| 219 | * |
||
| 220 | * @return string |
||
| 221 | */ |
||
| 222 | 1 | public function getScheme() |
|
| 226 | |||
| 227 | /** |
||
| 228 | * get the host from the current request |
||
| 229 | * |
||
| 230 | * @return string |
||
| 231 | */ |
||
| 232 | 1 | public function getHost() |
|
| 236 | |||
| 237 | /** |
||
| 238 | * get the URI of the current request |
||
| 239 | * |
||
| 240 | * @return null|string |
||
| 241 | */ |
||
| 242 | public static function getUri() |
||
| 254 | |||
| 255 | /** |
||
| 256 | * get the referer of the current request |
||
| 257 | * |
||
| 258 | * @return string |
||
| 259 | */ |
||
| 260 | 1 | public function getReferer() |
|
| 264 | |||
| 265 | /** |
||
| 266 | * get the current script name associated with the request |
||
| 267 | * |
||
| 268 | * @return string |
||
| 269 | */ |
||
| 270 | 1 | public function getScriptName() |
|
| 276 | |||
| 277 | /** |
||
| 278 | * Get the domain name and include $tldLength segments of the tld. |
||
| 279 | * |
||
| 280 | * @return string Domain name without subdomains |
||
| 281 | */ |
||
| 282 | 1 | public function getDomain() |
|
| 288 | |||
| 289 | /** |
||
| 290 | * Get the subdomains for a host. |
||
| 291 | * |
||
| 292 | * @return string subdomain portion of host name |
||
| 293 | */ |
||
| 294 | 1 | public function getSubdomains() |
|
| 307 | |||
| 308 | /** |
||
| 309 | * Get the Client IP address, optionally attempting to peek behind any proxies |
||
| 310 | * to get a real routable address. |
||
| 311 | * |
||
| 312 | * @param boolean $considerProxy true to enable proxy tests |
||
| 313 | * |
||
| 314 | * @return string |
||
| 315 | */ |
||
| 316 | public function getClientIp($considerProxy = false) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Return current url |
||
| 352 | * |
||
| 353 | * @return string |
||
| 354 | */ |
||
| 355 | public function getUrl() |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Gets an environment variable from available sources, and provides emulation |
||
| 367 | * for unsupported or inconsistent environment variables (i.e. DOCUMENT_ROOT on |
||
| 368 | * IIS, or SCRIPT_NAME in CGI mode). Also exposes some additional custom |
||
| 369 | * environment information. |
||
| 370 | * Note : code modifications for XOOPS |
||
| 371 | * |
||
| 372 | * @param string $name Environment variable name. |
||
| 373 | * @param mixed $default default value |
||
| 374 | * |
||
| 375 | * @return string|boolean Environment variable setting. |
||
| 376 | * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#env |
||
| 377 | * |
||
| 378 | * @todo this methods and Xoops::getEnv() need to be unified |
||
| 379 | */ |
||
| 380 | 13 | public function getEnv($name, $default = null) |
|
| 445 | |||
| 446 | /** |
||
| 447 | * get files associated with the current request |
||
| 448 | * |
||
| 449 | * @param string $name name of file |
||
| 450 | * |
||
| 451 | * @return array |
||
| 452 | */ |
||
| 453 | public static function getFiles($name) |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Check whether or not a Request is a certain type. Uses the built in detection rules |
||
| 481 | * as well as additional rules defined with HttpRequest::addDetector(). Any detector can be called |
||
| 482 | * as `is($type)` or `is$Type()`. |
||
| 483 | * |
||
| 484 | * @param string $type The type of request you want to check. |
||
| 485 | * |
||
| 486 | * @return boolean Whether or not the request is the type you are checking. |
||
| 487 | */ |
||
| 488 | public function is($type) |
||
| 504 | |||
| 505 | /** |
||
| 506 | * detectByEnv - perform detection on detectors with an 'env' component |
||
| 507 | * |
||
| 508 | * @param array $detect a detectors array entry to test against |
||
| 509 | * |
||
| 510 | * @return boolean true if detect is matched, false if not |
||
| 511 | */ |
||
| 512 | protected function detectByEnv($detect) |
||
| 524 | |||
| 525 | /** |
||
| 526 | * detectByParam - perform detection on detectors with an 'param' component. |
||
| 527 | * To match an entry with the name in the 'param' key of the $detect rule must |
||
| 528 | * exist in the $params property and be equal to the 'value' entry specified |
||
| 529 | * in the $detect array. |
||
| 530 | * |
||
| 531 | * @param array $detect a detectors array entry to test against. Param entries are |
||
| 532 | * of the form array('param' => name, 'value' => value) |
||
| 533 | * |
||
| 534 | * @return boolean true if detect is matched, false if not |
||
| 535 | */ |
||
| 536 | protected function detectByParam($detect) |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Add a new detector to the list of detectors that a request can use. |
||
| 545 | * There are several different formats and types of detectors that can be set. |
||
| 546 | * ### Environment value comparison |
||
| 547 | * An environment value comparison, compares a value fetched from `env()` to a known value |
||
| 548 | * the environment value is equality checked against the provided value. |
||
| 549 | * e.g `addDetector('post', array('env' => 'REQUEST_METHOD', 'value' => 'POST'))` |
||
| 550 | * ### Pattern value comparison |
||
| 551 | * Pattern value comparison allows you to compare a value fetched from `env()` to a regular expression. |
||
| 552 | * e.g `addDetector('iphone', array('env' => 'HTTP_USER_AGENT', 'pattern' => '/iPhone/i'));` |
||
| 553 | * ### Option based comparison |
||
| 554 | * Option based comparisons use a list of options to create a regular expression. Subsequent calls |
||
| 555 | * to add an already defined options detector will merge the options. |
||
| 556 | * e.g `addDetector('mobile', array('env' => 'HTTP_USER_AGENT', 'options' => array('Fennec')));` |
||
| 557 | * ### Callback detectors |
||
| 558 | * Callback detectors allow you to provide a 'callback' type to handle the check. The callback will |
||
| 559 | * receive the request object as its only parameter. |
||
| 560 | * e.g `addDetector('custom', array('callback' => array('SomeClass', 'someMethod')));` |
||
| 561 | * ### Request parameter detectors |
||
| 562 | * Allows for custom detectors on the request parameters. |
||
| 563 | * e.g `addDetector('post', array('param' => 'requested', 'value' => 1)` |
||
| 564 | * |
||
| 565 | * @param string $name The name of the detector. |
||
| 566 | * @param array $options The options for the detector definition. See above. |
||
| 567 | * |
||
| 568 | * @return void |
||
| 569 | */ |
||
| 570 | public function addDetector($name, $options) |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Determine if a client accepts a given media type |
||
| 581 | * |
||
| 582 | * @param string $mediaType The content type to check for. |
||
| 583 | * |
||
| 584 | * @return boolean true if client accepts the media type, otherwise false |
||
| 585 | */ |
||
| 586 | 1 | public function clientAcceptsType($mediaType) |
|
| 601 | |||
| 602 | /** |
||
| 603 | * getAcceptMediaTypes returns the http-accept header as an |
||
| 604 | * array of media types arranged by specified preference |
||
| 605 | * |
||
| 606 | * @return array associative array of preference (numeric weight >0 <=1.0 ) |
||
| 607 | * keyed by media types, and sorted by preference |
||
| 608 | */ |
||
| 609 | 1 | View Code Duplication | public function getAcceptMediaTypes() |
| 630 | |||
| 631 | /** |
||
| 632 | * getAcceptedLanguages returns the http-accept-language header as an |
||
| 633 | * array of language codes arranged by specified preference |
||
| 634 | * |
||
| 635 | * @return array associative array of preference (numeric weight >0 <=1.0 ) |
||
| 636 | * keyed by language code, and sorted by preference |
||
| 637 | */ |
||
| 638 | 1 | View Code Duplication | public function getAcceptedLanguages() |
| 659 | } |
||
| 660 |