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 |
||
15 | class Crawler implements LoggerAwareInterface |
||
16 | { |
||
17 | /** |
||
18 | * @var Client |
||
19 | */ |
||
20 | private $client; |
||
21 | |||
22 | /** |
||
23 | * @var int |
||
24 | */ |
||
25 | private $limit = 0; |
||
26 | |||
27 | /** |
||
28 | * @var bool |
||
29 | */ |
||
30 | private $stopOnError = false; |
||
31 | |||
32 | /** |
||
33 | * @var bool |
||
34 | */ |
||
35 | private $exceptionOnError = false; |
||
36 | |||
37 | /** |
||
38 | * @var UrlMatcherInterface[] |
||
39 | */ |
||
40 | private $whitelistUrlMatchers = []; |
||
41 | |||
42 | /** |
||
43 | * @var UrlMatcherInterface[] |
||
44 | */ |
||
45 | private $blacklistUrlMatchers = []; |
||
46 | |||
47 | /** |
||
48 | * @var UrlNormalizerInterface[] |
||
49 | */ |
||
50 | private $urlNormalizers = []; |
||
51 | |||
52 | /** |
||
53 | * @var Url |
||
54 | */ |
||
55 | private $baseUrl; |
||
56 | |||
57 | /** |
||
58 | * @var array |
||
59 | */ |
||
60 | private $urlsCrawled = []; |
||
61 | |||
62 | /** |
||
63 | * @var array |
||
64 | */ |
||
65 | private $urlsQueued = []; |
||
66 | |||
67 | /** |
||
68 | * @var array |
||
69 | */ |
||
70 | private $urlsRejected = []; |
||
71 | |||
72 | /** |
||
73 | * @var array |
||
74 | */ |
||
75 | private $urlsReturned = []; |
||
76 | |||
77 | /** |
||
78 | * @var LoggerInterface |
||
79 | */ |
||
80 | private $logger = null; |
||
81 | |||
82 | /** |
||
83 | * @param Client $client |
||
84 | */ |
||
85 | 15 | public function __construct(Client $client = null) |
|
95 | |||
96 | /** |
||
97 | * @param Client $client |
||
98 | */ |
||
99 | 15 | public function setClient(Client $client) |
|
103 | |||
104 | /** |
||
105 | * @return Client |
||
106 | */ |
||
107 | 2 | public function getClient() |
|
111 | |||
112 | /** |
||
113 | * @return int |
||
114 | */ |
||
115 | 2 | public function getLimit() |
|
119 | |||
120 | /** |
||
121 | * @param int $limit |
||
122 | * @return $this |
||
123 | */ |
||
124 | 2 | public function setLimit($limit) |
|
130 | |||
131 | /** |
||
132 | * @return boolean |
||
133 | */ |
||
134 | 5 | public function getStopOnError() |
|
138 | |||
139 | /** |
||
140 | * @param boolean $stopOnError |
||
141 | * @return $this |
||
142 | */ |
||
143 | 2 | public function setStopOnError($stopOnError) |
|
149 | |||
150 | /** |
||
151 | * @return boolean |
||
152 | */ |
||
153 | 2 | public function getExceptionOnError() |
|
157 | |||
158 | /** |
||
159 | * @param boolean $exceptionOnError |
||
160 | * @return $this |
||
161 | */ |
||
162 | 1 | public function setExceptionOnError($exceptionOnError) |
|
168 | |||
169 | /** |
||
170 | * @return array |
||
171 | */ |
||
172 | 9 | public function getUrlsCrawled() |
|
176 | |||
177 | /** |
||
178 | * @return array |
||
179 | */ |
||
180 | 2 | public function getUrlsQueued() |
|
184 | |||
185 | /** |
||
186 | * @return array |
||
187 | */ |
||
188 | 2 | public function getUrlsRejected() |
|
192 | |||
193 | /** |
||
194 | * @return array |
||
195 | */ |
||
196 | 4 | public function getUrlsReturned() |
|
200 | |||
201 | /** |
||
202 | * @param $urlMatchers |
||
203 | * @return $this |
||
204 | */ |
||
205 | 1 | public function setWhitelistUrlMatchers(array $urlMatchers) |
|
214 | |||
215 | /** |
||
216 | * @return Url\Matcher\UrlMatcherInterface[] |
||
217 | */ |
||
218 | 2 | public function getWhitelistUrlMatchers() |
|
222 | |||
223 | /** |
||
224 | * @param UrlMatcherInterface $urlMatcher |
||
225 | * @return $this |
||
226 | */ |
||
227 | 2 | public function addWhitelistUrlMatcher(UrlMatcherInterface $urlMatcher) |
|
233 | |||
234 | /** |
||
235 | * @return $this |
||
236 | */ |
||
237 | 1 | public function clearWhitelistUrlMatchers() |
|
243 | |||
244 | /** |
||
245 | * @param array $urlMatchers |
||
246 | * @return $this |
||
247 | */ |
||
248 | 1 | public function setBlacklistUrlMatchers(array $urlMatchers) |
|
257 | |||
258 | /** |
||
259 | * @return Url\Matcher\UrlMatcherInterface[] |
||
260 | */ |
||
261 | 2 | public function getBlacklistUrlMatchers() |
|
265 | |||
266 | /** |
||
267 | * @param UrlMatcherInterface $urlMatcher |
||
268 | * @return $this |
||
269 | */ |
||
270 | 2 | public function addBlacklistUrlMatcher(UrlMatcherInterface $urlMatcher) |
|
276 | |||
277 | /** |
||
278 | * @return $this |
||
279 | */ |
||
280 | 1 | public function clearBlacklistUrlMatchers() |
|
286 | |||
287 | /** |
||
288 | * @param array $normalizers |
||
289 | * @return $this |
||
290 | */ |
||
291 | 1 | public function setUrlNormalizers(array $normalizers) |
|
301 | |||
302 | /** |
||
303 | * @return UrlNormalizerInterface[] |
||
304 | */ |
||
305 | 1 | public function getUrlNormalizers() |
|
309 | |||
310 | /** |
||
311 | * @param UrlNormalizerInterface $normalizer |
||
312 | * @return $this |
||
313 | */ |
||
314 | 2 | public function addUrlNormalizer(UrlNormalizerInterface $normalizer) |
|
320 | |||
321 | /** |
||
322 | * @return $this |
||
323 | */ |
||
324 | 1 | public function clearUrlNormalizers() |
|
330 | |||
331 | /** |
||
332 | * @return LoggerInterface |
||
333 | */ |
||
334 | 13 | public function getLogger() |
|
342 | |||
343 | /** |
||
344 | * @param LoggerInterface $logger |
||
345 | * @return $this |
||
346 | */ |
||
347 | 1 | public function setLogger(LoggerInterface $logger) |
|
353 | |||
354 | /** |
||
355 | * @param Url $url |
||
356 | */ |
||
357 | 11 | protected function addUrlToQueue(Url $url) |
|
361 | |||
362 | /** |
||
363 | * @param $url |
||
364 | * @return Url |
||
365 | * @throws \Exception |
||
366 | */ |
||
367 | 10 | protected function createHttpUrlString($url) |
|
380 | |||
381 | /** |
||
382 | * @param Url $url |
||
383 | */ |
||
384 | 11 | protected function reset(Url $url) |
|
392 | |||
393 | /** |
||
394 | * @param string $url |
||
395 | * @return \Generator|Page[] |
||
396 | * @throws RequestException |
||
397 | */ |
||
398 | 10 | public function crawl($url) |
|
440 | |||
441 | /** |
||
442 | * @param Url $url |
||
443 | * @return Url |
||
444 | */ |
||
445 | 10 | protected function updateUrl(Url $url) |
|
454 | |||
455 | /** |
||
456 | * @param DomCrawler $crawler |
||
457 | */ |
||
458 | 9 | protected function updateQueue(DomCrawler $crawler) |
|
473 | |||
474 | /** |
||
475 | * @param Url $url |
||
476 | * @return Url |
||
477 | */ |
||
478 | 7 | protected function normalizeUrl(Url $url) |
|
486 | |||
487 | /** |
||
488 | * @param Url $url |
||
489 | * @return bool |
||
490 | */ |
||
491 | 9 | protected function shouldReturnUrl(Url $url) |
|
509 | |||
510 | /** |
||
511 | * @param Url $url |
||
512 | * @return bool |
||
513 | */ |
||
514 | 1 | protected function isUrlWhitelisted(Url $url) |
|
524 | |||
525 | /** |
||
526 | * @param Url $url |
||
527 | * @return bool |
||
528 | */ |
||
529 | 9 | protected function isUrlBlacklisted(Url $url) |
|
539 | |||
540 | /** |
||
541 | * @param Url $url |
||
542 | * @return bool |
||
543 | */ |
||
544 | 8 | protected function shouldCrawlUrl(Url $url) |
|
561 | |||
562 | /** |
||
563 | * @param Url $url |
||
564 | * @return bool |
||
565 | */ |
||
566 | 8 | protected function isUrlRejected(Url $url) |
|
570 | |||
571 | /** |
||
572 | * @param Url $url |
||
573 | * @return bool |
||
574 | */ |
||
575 | 8 | protected function isUrlCrawled(Url $url) |
|
579 | |||
580 | /** |
||
581 | * @param Url $url |
||
582 | * @return bool |
||
583 | */ |
||
584 | 8 | protected function isUrlQueued(Url $url) |
|
588 | |||
589 | /** |
||
590 | * @param Url $url |
||
591 | * @return bool |
||
592 | */ |
||
593 | 8 | protected function isUrlPartOfBaseUrl(Url $url) |
|
603 | |||
604 | /** |
||
605 | * @return bool |
||
606 | */ |
||
607 | 9 | protected function isLimitReached() |
|
611 | |||
612 | /** |
||
613 | * @param DomCrawler $crawler |
||
614 | * @return array |
||
615 | */ |
||
616 | 9 | protected function extractUrlsFromCrawler(DomCrawler $crawler) |
|
624 | |||
625 | /** |
||
626 | * @param string $url |
||
627 | * @return DomCrawler |
||
628 | */ |
||
629 | 10 | protected function requestPage($url) |
|
637 | } |
||
638 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: