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 | /** @var \GuzzleHttp\Client */ |
||
| 24 | protected $client; |
||
| 25 | |||
| 26 | /** @var \Psr\Http\Message\UriInterface */ |
||
| 27 | protected $baseUrl; |
||
| 28 | |||
| 29 | /** @var \Spatie\Crawler\CrawlObserverCollection */ |
||
| 30 | protected $crawlObservers; |
||
| 31 | |||
| 32 | /** @var \Spatie\Crawler\CrawlProfile */ |
||
| 33 | protected $crawlProfile; |
||
| 34 | |||
| 35 | /** @var int */ |
||
| 36 | protected $concurrency; |
||
| 37 | |||
| 38 | /** @var \Spatie\Crawler\CrawlQueue\CrawlQueue */ |
||
| 39 | protected $crawlQueue; |
||
| 40 | |||
| 41 | /** @var int */ |
||
| 42 | protected $crawledUrlCount = 0; |
||
| 43 | |||
| 44 | /** @var int|null */ |
||
| 45 | protected $maximumCrawlCount = null; |
||
| 46 | |||
| 47 | /** @var int */ |
||
| 48 | protected $maximumResponseSize = 1024 * 1024 * 2; |
||
| 49 | |||
| 50 | /** @var int|null */ |
||
| 51 | protected $maximumDepth = null; |
||
| 52 | |||
| 53 | /** @var bool */ |
||
| 54 | protected $respectRobots = true; |
||
| 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 \Spatie\Robots\RobotsTxt */ |
||
| 66 | protected $robotsTxt = null; |
||
| 67 | |||
| 68 | /** @var string */ |
||
| 69 | protected $crawlRequestFulfilledClass; |
||
| 70 | |||
| 71 | /** @var string */ |
||
| 72 | protected $crawlRequestFailedClass; |
||
| 73 | |||
| 74 | /** @var float */ |
||
| 75 | protected $delayBetweenRequests = 0; |
||
| 76 | |||
| 77 | /** @var */ |
||
| 78 | protected static $defaultClientOptions = [ |
||
| 79 | RequestOptions::COOKIES => true, |
||
| 80 | RequestOptions::CONNECT_TIMEOUT => 10, |
||
| 81 | RequestOptions::TIMEOUT => 10, |
||
| 82 | RequestOptions::ALLOW_REDIRECTS => false, |
||
| 83 | ]; |
||
| 84 | |||
| 85 | public static function create(array $clientOptions = []): Crawler |
||
| 95 | |||
| 96 | public function __construct(Client $client, int $concurrency = 10) |
||
| 112 | |||
| 113 | public function setConcurrency(int $concurrency): Crawler |
||
| 119 | |||
| 120 | public function setMaximumResponseSize(int $maximumResponseSizeInBytes): Crawler |
||
| 126 | |||
| 127 | public function getMaximumResponseSize(): ?int |
||
| 131 | |||
| 132 | public function setMaximumCrawlCount(int $maximumCrawlCount): Crawler |
||
| 138 | |||
| 139 | public function getMaximumCrawlCount(): ?int |
||
| 143 | |||
| 144 | public function getCrawlerUrlCount(): int |
||
| 148 | |||
| 149 | public function setMaximumDepth(int $maximumDepth): Crawler |
||
| 155 | |||
| 156 | public function getMaximumDepth(): ?int |
||
| 160 | |||
| 161 | public function setDelayBetweenRequests(int $delay): Crawler |
||
| 167 | |||
| 168 | public function getDelayBetweenRequests(): float |
||
| 172 | |||
| 173 | public function ignoreRobots(): Crawler |
||
| 179 | |||
| 180 | public function respectRobots(): Crawler |
||
| 186 | |||
| 187 | public function mustRespectRobots(): bool |
||
| 191 | |||
| 192 | public function getRobotsTxt(): RobotsTxt |
||
| 196 | |||
| 197 | public function setCrawlQueue(CrawlQueue $crawlQueue): Crawler |
||
| 203 | |||
| 204 | public function getCrawlQueue(): CrawlQueue |
||
| 208 | |||
| 209 | public function executeJavaScript(): Crawler |
||
| 215 | |||
| 216 | public function doNotExecuteJavaScript(): Crawler |
||
| 222 | |||
| 223 | public function mayExecuteJavascript(): bool |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @param \Spatie\Crawler\CrawlObserver|array[\Spatie\Crawler\CrawlObserver] $crawlObservers |
||
| 230 | * |
||
| 231 | * @return $this |
||
| 232 | */ |
||
| 233 | public function setCrawlObserver($crawlObservers): Crawler |
||
| 241 | |||
| 242 | public function setCrawlObservers(array $crawlObservers): Crawler |
||
| 248 | |||
| 249 | public function addCrawlObserver(CrawlObserver $crawlObserver): Crawler |
||
| 255 | |||
| 256 | public function getCrawlObservers(): CrawlObserverCollection |
||
| 260 | |||
| 261 | public function setCrawlProfile(CrawlProfile $crawlProfile): Crawler |
||
| 267 | |||
| 268 | public function getCrawlProfile(): CrawlProfile |
||
| 272 | |||
| 273 | public function setCrawlFulfilledHandlerClass(string $crawlRequestFulfilledClass): Crawler |
||
| 285 | |||
| 286 | public function setCrawlFailedHandlerClass(string $crawlRequestFailedClass): Crawler |
||
| 298 | |||
| 299 | public function setBrowsershot(Browsershot $browsershot) |
||
| 305 | |||
| 306 | public function getBrowsershot(): Browsershot |
||
| 314 | |||
| 315 | public function getBaseUrl(): UriInterface |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @param \Psr\Http\Message\UriInterface|string $baseUrl |
||
| 322 | */ |
||
| 323 | public function startCrawling($baseUrl) |
||
| 357 | |||
| 358 | public function addToDepthTree(UriInterface $url, UriInterface $parentUrl, Node $node = null): ?Node |
||
| 386 | |||
| 387 | protected function startCrawlingQueue() |
||
| 402 | |||
| 403 | /** |
||
| 404 | * @deprecated This function will be removed in the next major version |
||
| 405 | */ |
||
| 406 | public function endsWith($haystack, $needle) |
||
| 411 | |||
| 412 | protected function createRobotsTxt(UriInterface $uri): RobotsTxt |
||
| 416 | |||
| 417 | protected function getCrawlRequests(): Generator |
||
| 438 | |||
| 439 | public function addToCrawlQueue(CrawlUrl $crawlUrl): Crawler |
||
| 455 | |||
| 456 | public function maximumCrawlCountReached(): bool |
||
| 466 | } |
||
| 467 |
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.