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 bool */ |
||
| 56 | protected $respectRobots = true; |
||
| 57 | |||
| 58 | /** @var \Tree\Node\Node */ |
||
| 59 | protected $depthTree; |
||
| 60 | |||
| 61 | /** @var bool */ |
||
| 62 | protected $executeJavaScript = false; |
||
| 63 | |||
| 64 | /** @var Browsershot */ |
||
| 65 | protected $browsershot = null; |
||
| 66 | |||
| 67 | /** @var \Spatie\Robots\RobotsTxt */ |
||
| 68 | protected $robotsTxt = null; |
||
| 69 | |||
| 70 | protected static $defaultClientOptions = [ |
||
| 71 | RequestOptions::COOKIES => true, |
||
| 72 | RequestOptions::CONNECT_TIMEOUT => 10, |
||
| 73 | RequestOptions::TIMEOUT => 10, |
||
| 74 | RequestOptions::ALLOW_REDIRECTS => false, |
||
| 75 | ]; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @param array $clientOptions |
||
| 79 | * |
||
| 80 | * @return static |
||
| 81 | */ |
||
| 82 | public static function create(array $clientOptions = []) |
||
| 83 | { |
||
| 84 | $clientOptions = (count($clientOptions)) |
||
| 85 | ? $clientOptions |
||
| 86 | : self::$defaultClientOptions; |
||
| 87 | |||
| 88 | $client = new Client($clientOptions); |
||
| 89 | |||
| 90 | return new static($client); |
||
| 91 | } |
||
| 92 | |||
| 93 | public function __construct(Client $client, int $concurrency = 10) |
||
| 94 | { |
||
| 95 | $this->client = $client; |
||
| 96 | |||
| 97 | $this->concurrency = $concurrency; |
||
| 98 | |||
| 99 | $this->crawlProfile = new CrawlAllUrls(); |
||
| 100 | |||
| 101 | $this->crawlQueue = new CollectionCrawlQueue(); |
||
| 102 | } |
||
| 103 | |||
| 104 | public function setConcurrency(int $concurrency): self |
||
| 110 | |||
| 111 | public function setMaximumResponseSize(int $maximumResponseSizeInBytes): self |
||
| 112 | { |
||
| 117 | |||
| 118 | public function setMaximumCrawlCount(int $maximumCrawlCount): self |
||
| 124 | |||
| 125 | public function setMaximumDepth(int $maximumDepth): self |
||
| 131 | |||
| 132 | public function ignoreRobots(): self |
||
| 138 | |||
| 139 | public function respectRobots(): self |
||
| 145 | |||
| 146 | public function setCrawlQueue(CrawlQueue $crawlQueue): self |
||
| 152 | |||
| 153 | public function executeJavaScript(): self |
||
| 159 | |||
| 160 | public function doNotExecuteJavaScript(): self |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @param \Spatie\Crawler\CrawlObserver|array[\Spatie\Crawler\CrawlObserver] $crawlObservers |
||
|
|
|||
| 169 | * |
||
| 170 | * @return $this |
||
| 171 | */ |
||
| 172 | public function setCrawlObserver($crawlObservers) |
||
| 180 | |||
| 181 | public function setCrawlObservers(array $crawlObservers): self |
||
| 187 | |||
| 188 | public function addCrawlObserver(CrawlObserver $crawlObserver): self |
||
| 194 | |||
| 195 | public function setCrawlProfile(CrawlProfile $crawlProfile): self |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @param \Psr\Http\Message\UriInterface|string $baseUrl |
||
| 204 | */ |
||
| 205 | public function startCrawling($baseUrl) |
||
| 237 | |||
| 238 | protected function startCrawlingQueue() |
||
| 257 | |||
| 258 | public function endsWith($haystack, $needle) |
||
| 263 | |||
| 264 | protected function createRobotsTxt(UriInterface $uri): RobotsTxt |
||
| 268 | |||
| 269 | protected function getCrawlRequests(): Generator |
||
| 290 | |||
| 291 | public function addAllLinksToCrawlQueue(string $html, UriInterface $foundOnUrl) |
||
| 328 | |||
| 329 | protected function shouldCrawl(Node $node): bool |
||
| 341 | |||
| 342 | /** |
||
| 343 | * @param string $html |
||
| 344 | * @param \Psr\Http\Message\UriInterface $foundOnUrl |
||
| 345 | * |
||
| 346 | * @return \Illuminate\Support\Collection|\Tightenco\Collect\Support\Collection|null |
||
| 347 | */ |
||
| 348 | protected function extractAllLinks(string $html, UriInterface $foundOnUrl) |
||
| 369 | |||
| 370 | protected function normalizeUrl(UriInterface $url): UriInterface |
||
| 374 | |||
| 375 | protected function hasCrawlableScheme(UriInterface $uri): bool |
||
| 379 | |||
| 380 | protected function addToDepthTree(Node $node, UriInterface $url, UriInterface $parentUrl) |
||
| 402 | |||
| 403 | protected function getBodyAfterExecutingJavaScript(UriInterface $foundOnUrl): string |
||
| 411 | |||
| 412 | protected function getBrowsershot(): Browsershot |
||
| 420 | |||
| 421 | public function setBrowsershot(Browsershot $browsershot) |
||
| 427 | |||
| 428 | protected function addToCrawlQueue(CrawlUrl $crawlUrl) |
||
| 436 | |||
| 437 | protected function maximumCrawlCountReached(): bool |
||
| 445 | |||
| 446 | protected function getFulfilledHandler(): CrawlRequestFulfilled |
||
| 458 | |||
| 459 | protected function getFailedHandler(): CrawlRequestFailed |
||
| 466 | } |
||
| 467 |
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.