Complex classes like CrawlerDetect 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 CrawlerDetect, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class CrawlerDetect |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * The user agent. |
||
| 23 | * |
||
| 24 | * @var null |
||
| 25 | */ |
||
| 26 | protected $userAgent = null; |
||
| 27 | |||
| 28 | protected $checkAgent = null; |
||
| 29 | |||
| 30 | protected $checkIp = null; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Headers that contain a user agent. |
||
| 34 | * |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | protected $httpHeaders = array(); |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Store regex matches. |
||
| 41 | * |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | protected $matches = array(); |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Crawlers object. |
||
| 48 | * |
||
| 49 | * @var \Jaybizzle\CrawlerDetect\Fixtures\Crawlers |
||
| 50 | */ |
||
| 51 | protected $crawlers; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Exclusions object. |
||
| 55 | * |
||
| 56 | * @var \Jaybizzle\CrawlerDetect\Fixtures\Exclusions |
||
| 57 | */ |
||
| 58 | protected $exclusions; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Headers object. |
||
| 62 | * |
||
| 63 | * @var \Jaybizzle\CrawlerDetect\Fixtures\Headers |
||
| 64 | */ |
||
| 65 | protected $uaHttpHeaders; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * The compiled regex string. |
||
| 69 | * |
||
| 70 | * @var string |
||
| 71 | */ |
||
| 72 | protected $compiledRegex; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * The compiled exclusions regex string. |
||
| 76 | * |
||
| 77 | * @var string |
||
| 78 | */ |
||
| 79 | protected $compiledExclusions; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Class constructor. |
||
| 83 | */ |
||
| 84 | public function __construct() |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Compile the regex patterns into one regex string. |
||
| 91 | * |
||
| 92 | * @param array |
||
| 93 | * |
||
| 94 | * @return string |
||
| 95 | */ |
||
| 96 | public function compileRegex($patterns) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Set HTTP headers. |
||
| 103 | * |
||
| 104 | * @param array|null $httpHeaders |
||
|
|
|||
| 105 | */ |
||
| 106 | public function setHttpHeaders() |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Return user agent headers. |
||
| 121 | * |
||
| 122 | * @return array |
||
| 123 | */ |
||
| 124 | public function getUaHttpHeaders() |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Set the user agent. |
||
| 131 | * |
||
| 132 | * @param string|null $userAgent |
||
| 133 | */ |
||
| 134 | public function setUserAgent() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Check the user agent. |
||
| 147 | * |
||
| 148 | * @return $this |
||
| 149 | */ |
||
| 150 | public function agent($userAgent = null) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Check the IP address. |
||
| 168 | * |
||
| 169 | * @return $this |
||
| 170 | */ |
||
| 171 | public function ip($userIp) |
||
| 179 | |||
| 180 | public function checkAgent() |
||
| 201 | |||
| 202 | public function checkIp() |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Check user agent string against the regex. |
||
| 218 | * |
||
| 219 | * @param string|null $userAgent |
||
| 220 | * |
||
| 221 | * @return bool |
||
| 222 | */ |
||
| 223 | public function isCrawler() |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Return the matches. |
||
| 240 | * |
||
| 241 | * @return string|null |
||
| 242 | */ |
||
| 243 | public function getMatches() |
||
| 247 | |||
| 248 | |||
| 249 | public function ipInRange($ip, $range) |
||
| 301 | |||
| 302 | // decbin32 |
||
| 303 | // In order to simplify working with IP addresses (in binary) and their |
||
| 304 | // netmasks, it is easier to ensure that the binary strings are padded |
||
| 305 | // with zeros out to 32 characters - IP addresses are 32 bit numbers |
||
| 306 | public function decbin32 ($dec) { |
||
| 309 | } |
||
| 310 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.