Complex classes like Crawler 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 Crawler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class Crawler implements LoggerAwareInterface |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var Client |
||
| 17 | */ |
||
| 18 | private $client; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var int |
||
| 22 | */ |
||
| 23 | private $limit = 0; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var bool |
||
| 27 | */ |
||
| 28 | private $stopOnError = false; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var UrlMatcherInterface[] |
||
| 32 | */ |
||
| 33 | private $whitelistUrlMatchers = []; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var UrlMatcherInterface[] |
||
| 37 | */ |
||
| 38 | private $blacklistUrlMatchers = []; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var UrlNormalizerInterface[] |
||
| 42 | */ |
||
| 43 | private $urlNormalizers = []; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var Url |
||
| 47 | */ |
||
| 48 | private $baseUrl; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | private $urlsCrawled = []; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var array |
||
| 57 | */ |
||
| 58 | private $urlsQueued = []; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var array |
||
| 62 | */ |
||
| 63 | private $urlsRejected = []; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var array |
||
| 67 | */ |
||
| 68 | private $urlsReturned = []; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var LoggerInterface |
||
| 72 | */ |
||
| 73 | private $logger = null; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @param Client $client |
||
| 77 | * @param array $options |
||
| 78 | */ |
||
| 79 | 3 | public function __construct(Client $client = null, array $options = []) |
|
| 90 | |||
| 91 | /** |
||
| 92 | * @param Client $client |
||
| 93 | */ |
||
| 94 | 3 | public function setClient(Client $client) |
|
| 98 | |||
| 99 | /** |
||
| 100 | * @return Client |
||
| 101 | */ |
||
| 102 | 2 | public function getClient() |
|
| 106 | |||
| 107 | /** |
||
| 108 | * @param array $options |
||
| 109 | */ |
||
| 110 | 3 | public function setOptions(array $options) |
|
| 128 | |||
| 129 | /** |
||
| 130 | * @return int |
||
| 131 | */ |
||
| 132 | 3 | public function getLimit() |
|
| 136 | |||
| 137 | /** |
||
| 138 | * @param int $limit |
||
| 139 | * @return $this |
||
| 140 | */ |
||
| 141 | 2 | public function setLimit($limit) |
|
| 147 | |||
| 148 | /** |
||
| 149 | * @return boolean |
||
| 150 | */ |
||
| 151 | 3 | public function getStopOnError() |
|
| 155 | |||
| 156 | /** |
||
| 157 | * @param boolean $stopOnError |
||
| 158 | * @return Crawler |
||
| 159 | */ |
||
| 160 | 2 | public function setStopOnError($stopOnError) |
|
| 166 | |||
| 167 | /** |
||
| 168 | * @return array |
||
| 169 | */ |
||
| 170 | 1 | public function getUrlsCrawled() |
|
| 174 | |||
| 175 | /** |
||
| 176 | * @return array |
||
| 177 | */ |
||
| 178 | 1 | public function getUrlsQueued() |
|
| 182 | |||
| 183 | /** |
||
| 184 | * @return array |
||
| 185 | */ |
||
| 186 | 1 | public function getUrlsRejected() |
|
| 190 | |||
| 191 | /** |
||
| 192 | * @return array |
||
| 193 | */ |
||
| 194 | 1 | public function getUrlsReturned() |
|
| 198 | |||
| 199 | /** |
||
| 200 | * @param $urlMatchers |
||
| 201 | * @return $this |
||
| 202 | */ |
||
| 203 | 1 | public function setWhitelistUrlMatchers(array $urlMatchers) |
|
| 212 | |||
| 213 | /** |
||
| 214 | * @return Url\Matcher\UrlMatcherInterface[] |
||
| 215 | */ |
||
| 216 | 2 | public function getWhitelistUrlMatchers() |
|
| 220 | |||
| 221 | /** |
||
| 222 | * @param UrlMatcherInterface $urlMatcher |
||
| 223 | * @return $this |
||
| 224 | */ |
||
| 225 | 1 | public function addWhitelistUrlMatcher(UrlMatcherInterface $urlMatcher) |
|
| 231 | |||
| 232 | /** |
||
| 233 | * @return $this |
||
| 234 | */ |
||
| 235 | 1 | public function clearWhitelistUrlMatchers() |
|
| 241 | |||
| 242 | /** |
||
| 243 | * @param array $urlMatchers |
||
| 244 | * @return $this |
||
| 245 | */ |
||
| 246 | 1 | public function setBlacklistUrlMatchers(array $urlMatchers) |
|
| 255 | |||
| 256 | /** |
||
| 257 | * @return Url\Matcher\UrlMatcherInterface[] |
||
| 258 | */ |
||
| 259 | 2 | public function getBlacklistUrlMatchers() |
|
| 263 | |||
| 264 | /** |
||
| 265 | * @param UrlMatcherInterface $urlMatcher |
||
| 266 | * @return $this |
||
| 267 | */ |
||
| 268 | 1 | public function addBlacklistUrlMatcher(UrlMatcherInterface $urlMatcher) |
|
| 274 | |||
| 275 | /** |
||
| 276 | * @return $this |
||
| 277 | */ |
||
| 278 | 1 | public function clearBlacklistUrlMatchers() |
|
| 284 | |||
| 285 | /** |
||
| 286 | * @param array $normalizers |
||
| 287 | * @return $this |
||
| 288 | */ |
||
| 289 | public function setUrlNormalizers(array $normalizers) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @param UrlNormalizerInterface $normalizer |
||
| 302 | * @return $this |
||
| 303 | */ |
||
| 304 | public function addUrlNormalizer(UrlNormalizerInterface $normalizer) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @return $this |
||
| 313 | */ |
||
| 314 | public function clearUrlNormalizers() |
||
| 320 | |||
| 321 | /** |
||
| 322 | * @return LoggerInterface |
||
| 323 | */ |
||
| 324 | 3 | public function getLogger() |
|
| 332 | |||
| 333 | /** |
||
| 334 | * @param LoggerInterface $logger |
||
| 335 | * @return $this |
||
| 336 | */ |
||
| 337 | 2 | public function setLogger(LoggerInterface $logger) |
|
| 343 | |||
| 344 | /** |
||
| 345 | * @param Url $url |
||
| 346 | */ |
||
| 347 | protected function addUrlToQueue(Url $url) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * @param $url |
||
| 354 | * @return Url |
||
| 355 | */ |
||
| 356 | protected function createHttpUrlString($url) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * @param Url $url |
||
| 363 | */ |
||
| 364 | protected function reset(Url $url) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @param string $url |
||
| 375 | * @return \Generator|void |
||
| 376 | */ |
||
| 377 | public function crawl($url) |
||
| 416 | |||
| 417 | /** |
||
| 418 | * @param DomCrawler $crawler |
||
| 419 | */ |
||
| 420 | protected function updateQueue(DomCrawler $crawler) |
||
| 440 | |||
| 441 | /** |
||
| 442 | * @param Url $url |
||
| 443 | * @return Url |
||
| 444 | */ |
||
| 445 | protected function normalizeUrl(Url $url) |
||
| 453 | |||
| 454 | /** |
||
| 455 | * @param Url $url |
||
| 456 | * @return bool |
||
| 457 | */ |
||
| 458 | protected function shouldReturnUrl(Url $url) |
||
| 481 | |||
| 482 | /** |
||
| 483 | * @param Url $url |
||
| 484 | * @return bool |
||
| 485 | */ |
||
| 486 | protected function shouldCrawlUrl(Url $url) |
||
| 506 | |||
| 507 | /** |
||
| 508 | * @param Url $url |
||
| 509 | * @return bool |
||
| 510 | */ |
||
| 511 | protected function isUrlPartOfBaseUrl(Url $url) |
||
| 521 | |||
| 522 | /** |
||
| 523 | * @return bool |
||
| 524 | */ |
||
| 525 | private function isLimitReached() |
||
| 529 | |||
| 530 | /** |
||
| 531 | * @param DomCrawler $crawler |
||
| 532 | * @return array |
||
| 533 | */ |
||
| 534 | protected function extractUrlsFromCrawler(DomCrawler $crawler) |
||
| 542 | |||
| 543 | /** |
||
| 544 | * @param $url |
||
| 545 | * @return DomCrawler |
||
| 546 | */ |
||
| 547 | protected function requestPage($url) |
||
| 555 | } |
||
| 556 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: