Complex classes like Client 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 Client, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 6 | class Client implements RobotsTxtInterface |
||
| 7 | { |
||
| 8 | use UrlToolbox; |
||
| 9 | |||
| 10 | protected $rules = []; |
||
| 11 | |||
| 12 | protected $url = ''; |
||
| 13 | protected $statusCode = 200; |
||
| 14 | |||
| 15 | protected $userAgent = self::USER_AGENT; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Constructor |
||
| 19 | * |
||
| 20 | * @param string $content - file content |
||
| 21 | * @param string|null $encoding - character encoding |
||
| 22 | * @param integer|null $byteLimit - maximum of bytes to parse |
||
| 23 | * @param integer|null $maxRuleLength - max length of each rule |
||
| 24 | */ |
||
| 25 | public function __construct($content, $encoding = null, $byteLimit = self::BYTE_LIMIT, $maxRuleLength = self::MAX_LENGTH_RULE) |
||
| 33 | |||
| 34 | public function setOrigin($url, $statusCode) |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Set UserAgent |
||
| 45 | * |
||
| 46 | * @param string|null $userAgent |
||
| 47 | * @return void |
||
| 48 | */ |
||
| 49 | public function setUserAgent($userAgent) |
||
| 61 | |||
| 62 | /** |
||
| 63 | * |
||
| 64 | * |
||
| 65 | * @param string $url - url to check |
||
| 66 | * @return bool |
||
| 67 | */ |
||
| 68 | public function isAllowed($url) |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Check rules |
||
| 75 | * |
||
| 76 | * @param string $type - rule to check |
||
| 77 | * @param string $path - path to check |
||
| 78 | * @return bool |
||
| 79 | */ |
||
| 80 | protected function checkRules($type, $path) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Check rule switch |
||
| 100 | * |
||
| 101 | * @param string $type - directive or part of an url |
||
| 102 | * @param string $path |
||
| 103 | * @param array $array |
||
| 104 | * @return bool |
||
| 105 | */ |
||
| 106 | private function checkRuleSwitch($type, $path, $array) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Check Clean-Param rule |
||
| 119 | * |
||
| 120 | * @param string $path |
||
| 121 | * @param array $array |
||
| 122 | * @return bool |
||
| 123 | */ |
||
| 124 | protected function checkCleanParamRule($path, $array) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Check basic rule |
||
| 144 | * |
||
| 145 | * @param string $path |
||
| 146 | * @param array $array |
||
| 147 | * @return bool |
||
| 148 | */ |
||
| 149 | protected function checkRulePaths($path, $array) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Check Host rule |
||
| 167 | * |
||
| 168 | * @param array $array |
||
| 169 | * @return bool |
||
| 170 | */ |
||
| 171 | protected function checkHostRule($array) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Get path |
||
| 196 | * |
||
| 197 | * @param string $url |
||
| 198 | * @return string |
||
| 199 | * @throws Exceptions\ClientException |
||
| 200 | */ |
||
| 201 | protected function getPath($url) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * |
||
| 216 | * |
||
| 217 | * @param string $url - url to check |
||
| 218 | * @return bool |
||
| 219 | */ |
||
| 220 | public function isDisallowed($url) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Get sitemaps |
||
| 227 | * |
||
| 228 | * @return array |
||
| 229 | */ |
||
| 230 | public function getSitemaps() |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Get host |
||
| 240 | * |
||
| 241 | * @return string|null |
||
| 242 | */ |
||
| 243 | public function getHost() |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Get Clean-param |
||
| 253 | * |
||
| 254 | * @return array |
||
| 255 | */ |
||
| 256 | public function getCleanParam() |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Get CacheDelay |
||
| 266 | * |
||
| 267 | * @param bool $fallback return Crawl-delay if not found |
||
| 268 | * @return int|float|null |
||
| 269 | */ |
||
| 270 | public function getCacheDelay($fallback = true) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Get CrawlDelay |
||
| 280 | * |
||
| 281 | * @return int|float |
||
| 282 | */ |
||
| 283 | public function getCrawlDelay() |
||
| 290 | } |
||
| 291 |