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 |
||
| 23 | class Crawler |
||
| 24 | { |
||
| 25 | /** @var \GuzzleHttp\Client */ |
||
| 26 | protected $client; |
||
| 27 | |||
| 28 | /** @var \Psr\Http\Message\UriInterface */ |
||
| 29 | protected $baseUrl; |
||
| 30 | |||
| 31 | /** @var array[\Spatie\Crawler\CrawlObserver] */ |
||
| 32 | protected $crawlObservers; |
||
| 33 | |||
| 34 | /** @var \Spatie\Crawler\CrawlProfile */ |
||
| 35 | protected $crawlProfile; |
||
| 36 | |||
| 37 | /** @var int */ |
||
| 38 | protected $concurrency; |
||
| 39 | |||
| 40 | /** @var \Spatie\Crawler\CrawlQueue\CrawlQueue */ |
||
| 41 | protected $crawlQueue; |
||
| 42 | |||
| 43 | /** @var int */ |
||
| 44 | protected $crawledUrlCount = 0; |
||
| 45 | |||
| 46 | /** @var int|null */ |
||
| 47 | protected $maximumCrawlCount = null; |
||
| 48 | |||
| 49 | /** @var int */ |
||
| 50 | protected $maximumResponseSize = 1024 * 1024 * 2; |
||
| 51 | |||
| 52 | /** @var int|null */ |
||
| 53 | protected $maximumDepth = null; |
||
| 54 | |||
| 55 | /** @var \Tree\Node\Node */ |
||
| 56 | protected $depthTree; |
||
| 57 | |||
| 58 | /** @var bool */ |
||
| 59 | protected $executeJavaScript = false; |
||
| 60 | |||
| 61 | /** @var bool */ |
||
| 62 | protected $honorMetaRobotsFollowPolicy = true; |
||
| 63 | |||
| 64 | /** @var Browsershot */ |
||
| 65 | protected $browsershot = null; |
||
| 66 | |||
| 67 | protected static $defaultClientOptions = [ |
||
| 68 | RequestOptions::COOKIES => true, |
||
| 69 | RequestOptions::CONNECT_TIMEOUT => 10, |
||
| 70 | RequestOptions::TIMEOUT => 10, |
||
| 71 | RequestOptions::ALLOW_REDIRECTS => false, |
||
| 72 | ]; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @param array $clientOptions |
||
| 76 | * |
||
| 77 | * @return static |
||
| 78 | */ |
||
| 79 | public static function create(array $clientOptions = []) |
||
| 80 | { |
||
| 81 | $clientOptions = (count($clientOptions)) |
||
| 82 | ? $clientOptions |
||
| 83 | : self::$defaultClientOptions; |
||
| 84 | |||
| 85 | $client = new Client($clientOptions); |
||
| 86 | |||
| 87 | return new static($client); |
||
| 88 | } |
||
| 89 | |||
| 90 | public function __construct(Client $client, int $concurrency = 10) |
||
| 91 | { |
||
| 92 | $this->client = $client; |
||
| 93 | |||
| 94 | $this->concurrency = $concurrency; |
||
| 95 | |||
| 96 | $this->crawlProfile = new CrawlAllUrls(); |
||
| 97 | |||
| 98 | $this->crawlQueue = new CollectionCrawlQueue(); |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @param int $concurrency |
||
| 103 | * |
||
| 104 | * @return $this |
||
| 105 | */ |
||
| 106 | public function setConcurrency(int $concurrency) |
||
| 107 | { |
||
| 108 | $this->concurrency = $concurrency; |
||
| 109 | |||
| 110 | return $this; |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Responses that are larger that then specified value will be ignored. |
||
| 115 | * |
||
| 116 | * @param int $maximumResponseSizeInBytes |
||
| 117 | * |
||
| 118 | * @return $this |
||
| 119 | */ |
||
| 120 | public function setMaximumResponseSize(int $maximumResponseSizeInBytes) |
||
| 121 | { |
||
| 122 | $this->maximumResponseSize = $maximumResponseSizeInBytes; |
||
| 123 | |||
| 124 | return $this; |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @param int $maximumCrawlCount |
||
| 129 | * |
||
| 130 | * @return $this |
||
| 131 | */ |
||
| 132 | public function setMaximumCrawlCount(int $maximumCrawlCount) |
||
| 133 | { |
||
| 134 | $this->maximumCrawlCount = $maximumCrawlCount; |
||
| 135 | |||
| 136 | return $this; |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @param int $maximumDepth |
||
| 141 | * |
||
| 142 | * @return $this |
||
| 143 | */ |
||
| 144 | public function setMaximumDepth(int $maximumDepth) |
||
| 145 | { |
||
| 146 | $this->maximumDepth = $maximumDepth; |
||
| 147 | |||
| 148 | return $this; |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @param CrawlQueue $crawlQueue |
||
| 153 | * |
||
| 154 | * @return $this |
||
| 155 | */ |
||
| 156 | public function setCrawlQueue(CrawlQueue $crawlQueue) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @return $this |
||
| 165 | */ |
||
| 166 | public function executeJavaScript() |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @return $this |
||
| 175 | */ |
||
| 176 | public function doNotExecuteJavaScript() |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @return $this |
||
| 185 | */ |
||
| 186 | public function honorMetaRobotsFollowPolicy() |
||
| 187 | { |
||
| 188 | $this->honorMetaRobotsFollowPolicy = true; |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @return $this |
||
| 195 | */ |
||
| 196 | public function doNotHonorMetaRobotsFollowPolicy() |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @param \Spatie\Crawler\CrawlObserver|array[\Spatie\Crawler\CrawlObserver] $crawlObservers |
||
|
|
|||
| 205 | * |
||
| 206 | * @return $this |
||
| 207 | */ |
||
| 208 | public function setCrawlObserver($crawlObservers) |
||
| 216 | |||
| 217 | public function setCrawlObservers(array $crawlObservers) |
||
| 223 | |||
| 224 | public function addCrawlObserver(CrawlObserver $crawlObserver) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @param \Spatie\Crawler\CrawlProfile $crawlProfile |
||
| 233 | * |
||
| 234 | * @return $this |
||
| 235 | */ |
||
| 236 | public function setCrawlProfile(CrawlProfile $crawlProfile) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @param \Psr\Http\Message\UriInterface|string $baseUrl |
||
| 245 | */ |
||
| 246 | public function startCrawling($baseUrl) |
||
| 274 | |||
| 275 | protected function startCrawlingQueue() |
||
| 311 | |||
| 312 | public function endsWith($haystack, $needle) |
||
| 317 | |||
| 318 | protected function convertBodyToString(StreamInterface $bodyStream, $readMaximumBytes = 1024 * 1024 * 2): string |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @param ResponseInterface|null $response |
||
| 329 | * @param CrawlUrl $crawlUrl |
||
| 330 | */ |
||
| 331 | protected function handleCrawled(ResponseInterface $response, CrawlUrl $crawlUrl) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * @param RequestException $exception |
||
| 344 | * @param CrawlUrl $crawlUrl |
||
| 345 | */ |
||
| 346 | protected function handleCrawlFailed(RequestException $exception, CrawlUrl $crawlUrl) |
||
| 356 | |||
| 357 | protected function getCrawlRequests(): Generator |
||
| 378 | |||
| 379 | protected function addAllLinksToCrawlQueue(string $html, UriInterface $foundOnUrl) |
||
| 416 | |||
| 417 | protected function shouldCrawl(Node $node): bool |
||
| 425 | |||
| 426 | /** |
||
| 427 | * @param string $html |
||
| 428 | * @param \Psr\Http\Message\UriInterface $foundOnUrl |
||
| 429 | * |
||
| 430 | * @return \Illuminate\Support\Collection|\Tightenco\Collect\Support\Collection|null |
||
| 431 | */ |
||
| 432 | protected function extractAllLinks(string $html, UriInterface $foundOnUrl) |
||
| 464 | |||
| 465 | protected function normalizeUrl(UriInterface $url): UriInterface |
||
| 469 | |||
| 470 | protected function hasCrawlableScheme(UriInterface $uri): bool |
||
| 474 | |||
| 475 | protected function addtoDepthTree(Node $node, UriInterface $url, UriInterface $parentUrl) |
||
| 497 | |||
| 498 | protected function getBodyAfterExecutingJavaScript(UriInterface $foundOnUrl): string |
||
| 506 | |||
| 507 | protected function getBrowsershot(): Browsershot |
||
| 517 | |||
| 518 | public function setBrowsershot(Browsershot $browsershot) |
||
| 524 | |||
| 525 | protected function addToCrawlQueue(CrawlUrl $crawlUrl) |
||
| 533 | |||
| 534 | protected function maximumCrawlCountReached(): bool |
||
| 542 | } |
||
| 543 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.