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 |
||
21 | class Crawler |
||
22 | { |
||
23 | public const DEFAULT_USER_AGENT = '*'; |
||
24 | |||
25 | /** @var \GuzzleHttp\Client */ |
||
26 | protected $client; |
||
27 | |||
28 | /** @var \Psr\Http\Message\UriInterface */ |
||
29 | protected $baseUrl; |
||
30 | |||
31 | /** @var \Spatie\Crawler\CrawlObserverCollection */ |
||
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 | /** @var string */ |
||
71 | protected $crawlRequestFulfilledClass; |
||
72 | |||
73 | /** @var string */ |
||
74 | protected $crawlRequestFailedClass; |
||
75 | |||
76 | /** @var int */ |
||
77 | protected $delayBetweenRequests = 0; |
||
78 | |||
79 | /** @var */ |
||
80 | protected static $defaultClientOptions = [ |
||
81 | RequestOptions::COOKIES => true, |
||
82 | RequestOptions::CONNECT_TIMEOUT => 10, |
||
83 | RequestOptions::TIMEOUT => 10, |
||
84 | RequestOptions::ALLOW_REDIRECTS => false, |
||
85 | RequestOptions::HEADERS => [ |
||
86 | 'User-Agent' => self::DEFAULT_USER_AGENT, |
||
87 | ], |
||
88 | ]; |
||
89 | |||
90 | public static function create(array $clientOptions = []): Crawler |
||
100 | |||
101 | public function __construct(Client $client, int $concurrency = 10) |
||
117 | |||
118 | public function setConcurrency(int $concurrency): Crawler |
||
124 | |||
125 | public function setMaximumResponseSize(int $maximumResponseSizeInBytes): Crawler |
||
131 | |||
132 | public function getMaximumResponseSize(): ?int |
||
136 | |||
137 | public function setMaximumCrawlCount(int $maximumCrawlCount): Crawler |
||
143 | |||
144 | public function getMaximumCrawlCount(): ?int |
||
148 | |||
149 | public function getCrawlerUrlCount(): int |
||
153 | |||
154 | public function setMaximumDepth(int $maximumDepth): Crawler |
||
160 | |||
161 | public function getMaximumDepth(): ?int |
||
165 | |||
166 | /** |
||
167 | * @param int $delay The delay in milliseconds. |
||
168 | * |
||
169 | * @return Crawler |
||
170 | */ |
||
171 | public function setDelayBetweenRequests(int $delay): Crawler |
||
177 | |||
178 | /** |
||
179 | * @return int The delay in milliseconds. |
||
180 | */ |
||
181 | public function getDelayBetweenRequests(): int |
||
185 | |||
186 | public function ignoreRobots(): Crawler |
||
192 | |||
193 | public function respectRobots(): Crawler |
||
199 | |||
200 | public function mustRespectRobots(): bool |
||
204 | |||
205 | public function getRobotsTxt(): RobotsTxt |
||
209 | |||
210 | public function setCrawlQueue(CrawlQueue $crawlQueue): Crawler |
||
216 | |||
217 | public function getCrawlQueue(): CrawlQueue |
||
221 | |||
222 | public function executeJavaScript(): Crawler |
||
228 | |||
229 | public function doNotExecuteJavaScript(): Crawler |
||
235 | |||
236 | public function mayExecuteJavascript(): bool |
||
240 | |||
241 | /** |
||
242 | * @param \Spatie\Crawler\CrawlObserver|array[\Spatie\Crawler\CrawlObserver] $crawlObservers |
||
|
|||
243 | * |
||
244 | * @return $this |
||
245 | */ |
||
246 | public function setCrawlObserver($crawlObservers): Crawler |
||
254 | |||
255 | public function setCrawlObservers(array $crawlObservers): Crawler |
||
261 | |||
262 | public function addCrawlObserver(CrawlObserver $crawlObserver): Crawler |
||
268 | |||
269 | public function getCrawlObservers(): CrawlObserverCollection |
||
273 | |||
274 | public function setCrawlProfile(CrawlProfile $crawlProfile): Crawler |
||
280 | |||
281 | public function getCrawlProfile(): CrawlProfile |
||
285 | |||
286 | public function setCrawlFulfilledHandlerClass(string $crawlRequestFulfilledClass): Crawler |
||
298 | |||
299 | public function setCrawlFailedHandlerClass(string $crawlRequestFailedClass): Crawler |
||
311 | |||
312 | public function setBrowsershot(Browsershot $browsershot) |
||
318 | |||
319 | public function setUserAgent(string $userAgent): Crawler |
||
332 | |||
333 | public function getUserAgent(): string |
||
345 | |||
346 | public function getBrowsershot(): Browsershot |
||
354 | |||
355 | public function getBaseUrl(): UriInterface |
||
359 | |||
360 | /** |
||
361 | * @param \Psr\Http\Message\UriInterface|string $baseUrl |
||
362 | */ |
||
363 | public function startCrawling($baseUrl) |
||
397 | |||
398 | public function addToDepthTree(UriInterface $url, UriInterface $parentUrl, Node $node = null): ?Node |
||
426 | |||
427 | protected function startCrawlingQueue() |
||
442 | |||
443 | /** |
||
444 | * @deprecated This function will be removed in the next major version |
||
445 | */ |
||
446 | public function endsWith($haystack, $needle) |
||
451 | |||
452 | protected function createRobotsTxt(UriInterface $uri): RobotsTxt |
||
456 | |||
457 | protected function getCrawlRequests(): Generator |
||
478 | |||
479 | public function addToCrawlQueue(CrawlUrl $crawlUrl): Crawler |
||
495 | |||
496 | public function maximumCrawlCountReached(): bool |
||
506 | } |
||
507 |
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.