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 |
||
| 20 | class Crawler |
||
| 21 | { |
||
| 22 | /** @var \GuzzleHttp\Client */ |
||
| 23 | protected $client; |
||
| 24 | |||
| 25 | /** @var \Psr\Http\Message\UriInterface */ |
||
| 26 | protected $baseUrl; |
||
| 27 | |||
| 28 | /** @var \Spatie\Crawler\CrawlObserverCollection */ |
||
| 29 | protected $crawlObservers; |
||
| 30 | |||
| 31 | /** @var \Spatie\Crawler\CrawlProfile */ |
||
| 32 | protected $crawlProfile; |
||
| 33 | |||
| 34 | /** @var int */ |
||
| 35 | protected $concurrency; |
||
| 36 | |||
| 37 | /** @var \Spatie\Crawler\CrawlQueue\CrawlQueue */ |
||
| 38 | protected $crawlQueue; |
||
| 39 | |||
| 40 | /** @var int */ |
||
| 41 | protected $crawledUrlCount = 0; |
||
| 42 | |||
| 43 | /** @var int|null */ |
||
| 44 | protected $maximumCrawlCount = null; |
||
| 45 | |||
| 46 | /** @var int */ |
||
| 47 | protected $maximumResponseSize = 1024 * 1024 * 2; |
||
| 48 | |||
| 49 | /** @var int|null */ |
||
| 50 | protected $maximumDepth = null; |
||
| 51 | |||
| 52 | /** @var bool */ |
||
| 53 | protected $respectRobots = true; |
||
| 54 | |||
| 55 | /** @var array */ |
||
| 56 | protected $depthArray; |
||
| 57 | |||
| 58 | /** @var bool */ |
||
| 59 | protected $executeJavaScript = false; |
||
| 60 | |||
| 61 | /** @var Browsershot */ |
||
| 62 | protected $browsershot = null; |
||
| 63 | |||
| 64 | /** @var \Spatie\Robots\RobotsTxt */ |
||
| 65 | protected $robotsTxt = null; |
||
| 66 | |||
| 67 | /** @var string */ |
||
| 68 | protected $crawlRequestFulfilledClass; |
||
| 69 | |||
| 70 | /** @var string */ |
||
| 71 | protected $crawlRequestFailedClass; |
||
| 72 | |||
| 73 | /** @var float */ |
||
| 74 | protected $delayBetweenRequests = 0; |
||
| 75 | |||
| 76 | /** @var */ |
||
| 77 | protected static $defaultClientOptions = [ |
||
| 78 | RequestOptions::COOKIES => true, |
||
| 79 | RequestOptions::CONNECT_TIMEOUT => 10, |
||
| 80 | RequestOptions::TIMEOUT => 10, |
||
| 81 | RequestOptions::ALLOW_REDIRECTS => false, |
||
| 82 | ]; |
||
| 83 | |||
| 84 | public static function create(array $clientOptions = []): Crawler |
||
| 94 | |||
| 95 | public function __construct(Client $client, int $concurrency = 10) |
||
| 111 | |||
| 112 | public function setConcurrency(int $concurrency): Crawler |
||
| 118 | |||
| 119 | public function setMaximumResponseSize(int $maximumResponseSizeInBytes): Crawler |
||
| 125 | |||
| 126 | public function getMaximumResponseSize(): ?int |
||
| 130 | |||
| 131 | public function setMaximumCrawlCount(int $maximumCrawlCount): Crawler |
||
| 137 | |||
| 138 | public function getMaximumCrawlCount(): ?int |
||
| 142 | |||
| 143 | public function getCrawlerUrlCount(): int |
||
| 147 | |||
| 148 | public function setMaximumDepth(int $maximumDepth): Crawler |
||
| 154 | |||
| 155 | public function getMaximumDepth(): ?int |
||
| 159 | |||
| 160 | public function setDelayBetweenRequests(int $delay): Crawler |
||
| 166 | |||
| 167 | public function getDelayBetweenRequests(): float |
||
| 171 | |||
| 172 | public function ignoreRobots(): Crawler |
||
| 178 | |||
| 179 | public function respectRobots(): Crawler |
||
| 185 | |||
| 186 | public function mustRespectRobots(): bool |
||
| 190 | |||
| 191 | public function getRobotsTxt(): RobotsTxt |
||
| 195 | |||
| 196 | public function setCrawlQueue(CrawlQueue $crawlQueue): Crawler |
||
| 202 | |||
| 203 | public function getCrawlQueue(): CrawlQueue |
||
| 207 | |||
| 208 | public function executeJavaScript(): Crawler |
||
| 214 | |||
| 215 | public function doNotExecuteJavaScript(): Crawler |
||
| 221 | |||
| 222 | public function mayExecuteJavascript(): bool |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @param \Spatie\Crawler\CrawlObserver|array[\Spatie\Crawler\CrawlObserver] $crawlObservers |
||
| 229 | * |
||
| 230 | * @return $this |
||
| 231 | */ |
||
| 232 | public function setCrawlObserver($crawlObservers): Crawler |
||
| 240 | |||
| 241 | public function setCrawlObservers(array $crawlObservers): Crawler |
||
| 247 | |||
| 248 | public function addCrawlObserver(CrawlObserver $crawlObserver): Crawler |
||
| 254 | |||
| 255 | public function getCrawlObservers(): CrawlObserverCollection |
||
| 259 | |||
| 260 | public function setCrawlProfile(CrawlProfile $crawlProfile): Crawler |
||
| 266 | |||
| 267 | public function getCrawlProfile(): CrawlProfile |
||
| 271 | |||
| 272 | public function setCrawlFulfilledHandlerClass(string $crawlRequestFulfilledClass): Crawler |
||
| 284 | |||
| 285 | public function setCrawlFailedHandlerClass(string $crawlRequestFailedClass): Crawler |
||
| 297 | |||
| 298 | public function setBrowsershot(Browsershot $browsershot) |
||
| 304 | |||
| 305 | public function getBrowsershot(): Browsershot |
||
| 313 | |||
| 314 | public function getBaseUrl(): UriInterface |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @param \Psr\Http\Message\UriInterface|string $baseUrl |
||
| 321 | */ |
||
| 322 | public function startCrawling($baseUrl) |
||
| 356 | |||
| 357 | public function addToDepthArray(UriInterface $url, UriInterface $parentUrl): int |
||
| 373 | |||
| 374 | protected function startCrawlingQueue() |
||
| 389 | |||
| 390 | /** |
||
| 391 | * @deprecated This function will be removed in the next major version |
||
| 392 | */ |
||
| 393 | public function endsWith($haystack, $needle) |
||
| 398 | |||
| 399 | protected function createRobotsTxt(UriInterface $uri): RobotsTxt |
||
| 403 | |||
| 404 | protected function getCrawlRequests(): Generator |
||
| 425 | |||
| 426 | public function addToCrawlQueue(CrawlUrl $crawlUrl): Crawler |
||
| 442 | |||
| 443 | public function maximumCrawlCountReached(): bool |
||
| 453 | } |
||
| 454 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.