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 |
||
| 24 | class Crawler |
||
| 25 | { |
||
| 26 | /** @var \GuzzleHttp\Client */ |
||
| 27 | protected $client; |
||
| 28 | |||
| 29 | /** @var \Psr\Http\Message\UriInterface */ |
||
| 30 | protected $baseUrl; |
||
| 31 | |||
| 32 | /** @var array[\Spatie\Crawler\CrawlObserver] */ |
||
| 33 | protected $crawlObservers; |
||
| 34 | |||
| 35 | /** @var \Spatie\Crawler\CrawlProfile */ |
||
| 36 | protected $crawlProfile; |
||
| 37 | |||
| 38 | /** @var int */ |
||
| 39 | protected $concurrency; |
||
| 40 | |||
| 41 | /** @var \Spatie\Crawler\CrawlQueue\CrawlQueue */ |
||
| 42 | protected $crawlQueue; |
||
| 43 | |||
| 44 | /** @var int */ |
||
| 45 | protected $crawledUrlCount = 0; |
||
| 46 | |||
| 47 | /** @var int|null */ |
||
| 48 | protected $maximumCrawlCount = null; |
||
| 49 | |||
| 50 | /** @var int */ |
||
| 51 | protected $maximumResponseSize = 1024 * 1024 * 2; |
||
| 52 | |||
| 53 | /** @var int|null */ |
||
| 54 | protected $maximumDepth = null; |
||
| 55 | |||
| 56 | /** @var \Tree\Node\Node */ |
||
| 57 | protected $depthTree; |
||
| 58 | |||
| 59 | /** @var bool */ |
||
| 60 | protected $executeJavaScript = false; |
||
| 61 | |||
| 62 | /** @var Browsershot */ |
||
| 63 | protected $browsershot = null; |
||
| 64 | |||
| 65 | /** @var bool */ |
||
| 66 | protected $noSandbox = false; |
||
| 67 | |||
| 68 | protected static $defaultClientOptions = [ |
||
| 69 | RequestOptions::COOKIES => true, |
||
| 70 | RequestOptions::CONNECT_TIMEOUT => 10, |
||
| 71 | RequestOptions::TIMEOUT => 10, |
||
| 72 | RequestOptions::ALLOW_REDIRECTS => false, |
||
| 73 | ]; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @param array $clientOptions |
||
| 77 | * |
||
| 78 | * @return static |
||
| 79 | */ |
||
| 80 | public static function create(array $clientOptions = []) |
||
| 90 | |||
| 91 | public function __construct(Client $client, int $concurrency = 10) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @param int $concurrency |
||
| 104 | * |
||
| 105 | * @return $this |
||
| 106 | */ |
||
| 107 | public function setConcurrency(int $concurrency) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Responses that are larger that then specified value will be ignored. |
||
| 116 | * |
||
| 117 | * @param int $maximumResponseSizeInBytes |
||
| 118 | * |
||
| 119 | * @return $this |
||
| 120 | */ |
||
| 121 | public function setMaximumResponseSize(int $maximumResponseSizeInBytes) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @param int $maximumCrawlCount |
||
| 130 | * |
||
| 131 | * @return $this |
||
| 132 | */ |
||
| 133 | public function setMaximumCrawlCount(int $maximumCrawlCount) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @param int $maximumDepth |
||
| 142 | * |
||
| 143 | * @return $this |
||
| 144 | */ |
||
| 145 | public function setMaximumDepth(int $maximumDepth) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @param CrawlQueue $crawlQueue |
||
| 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 noSandbox() |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @param \Spatie\Crawler\CrawlObserver|array[\Spatie\Crawler\CrawlObserver] $crawlObservers |
||
|
|
|||
| 195 | * |
||
| 196 | * @return $this |
||
| 197 | */ |
||
| 198 | public function setCrawlObserver($crawlObservers) |
||
| 206 | |||
| 207 | public function setCrawlObservers(array $crawlObservers) |
||
| 213 | |||
| 214 | public function addCrawlObserver(CrawlObserver $crawlObserver) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @param \Spatie\Crawler\CrawlProfile $crawlProfile |
||
| 223 | * |
||
| 224 | * @return $this |
||
| 225 | */ |
||
| 226 | public function setCrawlProfile(CrawlProfile $crawlProfile) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @param \Psr\Http\Message\UriInterface|string $baseUrl |
||
| 235 | */ |
||
| 236 | public function startCrawling($baseUrl) |
||
| 264 | |||
| 265 | protected function startCrawlingQueue() |
||
| 300 | |||
| 301 | public function endsWith($haystack, $needle) |
||
| 306 | |||
| 307 | protected function convertBodyToString(StreamInterface $bodyStream, $readMaximumBytes = 1024 * 1024 * 2): string |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @param ResponseInterface|null $response |
||
| 318 | * @param CrawlUrl $crawlUrl |
||
| 319 | */ |
||
| 320 | protected function handleResponse($response, CrawlUrl $crawlUrl) |
||
| 326 | |||
| 327 | protected function getCrawlRequests(): Generator |
||
| 348 | |||
| 349 | protected function addAllLinksToCrawlQueue(string $html, UriInterface $foundOnUrl) |
||
| 382 | |||
| 383 | protected function shouldCrawl(Node $node): bool |
||
| 391 | |||
| 392 | protected function extractAllLinks(string $html, UriInterface $foundOnUrl): Collection |
||
| 410 | |||
| 411 | protected function normalizeUrl(UriInterface $url): UriInterface |
||
| 415 | |||
| 416 | protected function hasCrawlableScheme(UriInterface $uri): bool |
||
| 420 | |||
| 421 | protected function addtoDepthTree(Node $node, UriInterface $url, UriInterface $parentUrl) |
||
| 443 | |||
| 444 | protected function getBodyAfterExecutingJavaScript(UriInterface $foundOnUrl): string |
||
| 454 | |||
| 455 | protected function getBrowsershot(): Browsershot |
||
| 465 | |||
| 466 | public function setBrowsershot(Browsershot $browsershot) |
||
| 472 | |||
| 473 | protected function addToCrawlQueue(CrawlUrl $crawlUrl) |
||
| 481 | |||
| 482 | protected function maximumCrawlCountReached(): bool |
||
| 490 | } |
||
| 491 |
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.