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 \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 = null; |
||
| 72 | |||
| 73 | /** @var string */ |
||
| 74 | protected $crawlRequestFailedClass = null; |
||
| 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 | /** |
||
| 85 | * @param array $clientOptions |
||
| 86 | * |
||
| 87 | * @return static |
||
| 88 | */ |
||
| 89 | public static function create(array $clientOptions = []) |
||
| 90 | { |
||
| 91 | $clientOptions = (count($clientOptions)) |
||
| 92 | ? $clientOptions |
||
| 93 | : static::$defaultClientOptions; |
||
| 94 | |||
| 95 | $client = new Client($clientOptions); |
||
| 96 | |||
| 97 | return new static($client); |
||
| 98 | } |
||
| 99 | |||
| 100 | public function __construct(Client $client, int $concurrency = 10) |
||
| 101 | { |
||
| 102 | $this->client = $client; |
||
| 103 | |||
| 104 | $this->concurrency = $concurrency; |
||
| 105 | |||
| 106 | $this->crawlProfile = new CrawlAllUrls(); |
||
| 107 | |||
| 108 | $this->crawlQueue = new CollectionCrawlQueue(); |
||
| 109 | |||
| 110 | $this->crawlObservers = new CrawlObserverCollection(); |
||
| 111 | } |
||
| 112 | |||
| 113 | public function setConcurrency(int $concurrency): Crawler |
||
| 114 | { |
||
| 115 | $this->concurrency = $concurrency; |
||
| 116 | |||
| 117 | return $this; |
||
| 118 | } |
||
| 119 | |||
| 120 | public function setMaximumResponseSize(int $maximumResponseSizeInBytes): Crawler |
||
| 126 | |||
| 127 | public function getMaximumResponseSize(): ?int |
||
| 128 | { |
||
| 129 | return $this->maximumResponseSize; |
||
| 130 | } |
||
| 131 | |||
| 132 | public function setMaximumCrawlCount(int $maximumCrawlCount): Crawler |
||
| 138 | |||
| 139 | public function getMaximumCrawlCount(): ?int |
||
| 140 | { |
||
| 141 | return $this->maximumCrawlCount; |
||
| 142 | } |
||
| 143 | |||
| 144 | public function getCrawlerUrlCount(): int |
||
| 148 | |||
| 149 | public function setMaximumDepth(int $maximumDepth): Crawler |
||
| 155 | |||
| 156 | public function getMaximumDepth(): ?int |
||
| 160 | |||
| 161 | public function ignoreRobots(): Crawler |
||
| 167 | |||
| 168 | public function respectRobots(): Crawler |
||
| 174 | |||
| 175 | public function mustRespectRobots(): bool |
||
| 179 | |||
| 180 | public function getRobotsTxt(): RobotsTxt |
||
| 184 | |||
| 185 | public function setCrawlQueue(CrawlQueue $crawlQueue): Crawler |
||
| 191 | |||
| 192 | public function getCrawlQueue(): CrawlQueue |
||
| 196 | |||
| 197 | public function executeJavaScript(): Crawler |
||
| 203 | |||
| 204 | public function doNotExecuteJavaScript(): Crawler |
||
| 210 | |||
| 211 | public function mayExecuteJavascript(): bool |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @param \Spatie\Crawler\CrawlObserver|array[\Spatie\Crawler\CrawlObserver] $crawlObservers |
||
|
|
|||
| 218 | * |
||
| 219 | * @return $this |
||
| 220 | */ |
||
| 221 | public function setCrawlObserver($crawlObservers): Crawler |
||
| 229 | |||
| 230 | public function setCrawlObservers(array $crawlObservers): Crawler |
||
| 236 | |||
| 237 | public function addCrawlObserver(CrawlObserver $crawlObserver): Crawler |
||
| 243 | |||
| 244 | public function getCrawlObservers(): CrawlObserverCollection |
||
| 248 | |||
| 249 | public function setCrawlProfile(CrawlProfile $crawlProfile): Crawler |
||
| 255 | |||
| 256 | public function getCrawlProfile(): CrawlProfile |
||
| 260 | |||
| 261 | public function setBrowsershot(Browsershot $browsershot) |
||
| 267 | |||
| 268 | public function getBrowsershot(): Browsershot |
||
| 276 | |||
| 277 | public function getCrawlFulfilledHandler(): CrawlRequestFulfilledAbstract |
||
| 282 | |||
| 283 | public function setCrawlFulfilledHandlerClass(string $crawlRequestFulfilledClass): Crawler |
||
| 293 | |||
| 294 | public function getCrawlFailedHandler(): CrawlRequestFailedAbstract |
||
| 299 | |||
| 300 | public function setCrawlFailedHandlerClass(string $crawlRequestFailedClass): Crawler |
||
| 310 | |||
| 311 | public function getBaseUrl(): UriInterface |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @param \Psr\Http\Message\UriInterface|string $baseUrl |
||
| 318 | */ |
||
| 319 | public function startCrawling($baseUrl) |
||
| 353 | |||
| 354 | public function addToDepthTree(UriInterface $url, UriInterface $parentUrl, Node $node = null): ?Node |
||
| 378 | |||
| 379 | protected function startCrawlingQueue() |
||
| 394 | |||
| 395 | /** |
||
| 396 | * @deprecated This function will be removed in the next major version |
||
| 397 | */ |
||
| 398 | public function endsWith($haystack, $needle) |
||
| 403 | |||
| 404 | protected function createRobotsTxt(UriInterface $uri): RobotsTxt |
||
| 408 | |||
| 409 | protected function getCrawlRequests(): Generator |
||
| 430 | |||
| 431 | public function addToCrawlQueue(CrawlUrl $crawlUrl): Crawler |
||
| 447 | |||
| 448 | public function maximumCrawlCountReached(): bool |
||
| 458 | } |
||
| 459 |
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.