Complex classes like Spider 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 Spider, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class Spider |
||
| 26 | { |
||
| 27 | /** @var RequestHandlerInterface */ |
||
| 28 | private $requestHandler; |
||
| 29 | |||
| 30 | /** @var PersistenceHandlerInterface */ |
||
| 31 | private $persistenceHandler; |
||
| 32 | |||
| 33 | /** @var QueueManagerInterface */ |
||
| 34 | private $queueManager; |
||
| 35 | |||
| 36 | /** @var EventDispatcherInterface */ |
||
| 37 | private $dispatcher; |
||
| 38 | |||
| 39 | /** @var DiscovererSet */ |
||
| 40 | private $discovererSet; |
||
| 41 | |||
| 42 | /** @var PostFetchFilterInterface[] */ |
||
| 43 | private $postFetchFilters = array(); |
||
| 44 | |||
| 45 | /** @var FilterableUri The URI of the site to spider */ |
||
| 46 | private $seed = array(); |
||
| 47 | |||
| 48 | /** @var string the unique id of this spider instance */ |
||
| 49 | private $spiderId; |
||
| 50 | |||
| 51 | /** @var the maximum number of downloaded resources. 0 means no limit */ |
||
| 52 | public $downloadLimit = 0; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @param string $seed the URI to start crawling |
||
| 56 | * @param string $spiderId |
||
|
|
|||
| 57 | */ |
||
| 58 | public function __construct($seed, $spiderId = null) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Starts crawling the URI provided on instantiation |
||
| 81 | * |
||
| 82 | * @return array |
||
| 83 | */ |
||
| 84 | public function crawl() |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @param PostFetchFilterInterface $filter |
||
| 94 | */ |
||
| 95 | public function addPostFetchFilter(PostFetchFilterInterface $filter) |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @param RequestHandlerInterface $requestHandler |
||
| 102 | */ |
||
| 103 | public function setRequestHandler(RequestHandlerInterface $requestHandler) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @return RequestHandlerInterface |
||
| 110 | */ |
||
| 111 | public function getRequestHandler() |
||
| 119 | |||
| 120 | /** |
||
| 121 | * param DiscovererSet $discovererSet |
||
| 122 | */ |
||
| 123 | public function setDiscovererSet(DiscovererSet $discovererSet) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @return DiscovererSet |
||
| 130 | */ |
||
| 131 | public function getDiscovererSet() |
||
| 139 | |||
| 140 | /** |
||
| 141 | * param QueueManagerInterface $queueManager |
||
| 142 | */ |
||
| 143 | public function setQueueManager(QueueManagerInterface $queueManager) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @return QueueManagerInterface |
||
| 150 | */ |
||
| 151 | public function getQueueManager() |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @param PersistenceHandlerInterface $persistenceHandler |
||
| 162 | */ |
||
| 163 | public function setPersistenceHandler(PersistenceHandlerInterface $persistenceHandler) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @return PersistenceHandlerInterface |
||
| 170 | */ |
||
| 171 | public function getPersistenceHandler() |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @param EventDispatcherInterface $eventDispatcher |
||
| 182 | * @return $this |
||
| 183 | */ |
||
| 184 | public function setDispatcher(EventDispatcherInterface $eventDispatcher) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @return EventDispatcherInterface |
||
| 193 | */ |
||
| 194 | public function getDispatcher() |
||
| 201 | |||
| 202 | public function handleSignal($signal) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param Resource $resource |
||
| 215 | * @return bool |
||
| 216 | */ |
||
| 217 | private function matchesPostfetchFilter(Resource $resource) |
||
| 230 | |||
| 231 | private function isDownLoadLimitExceeded() |
||
| 238 | /** |
||
| 239 | * Function that crawls each provided URI |
||
| 240 | * It applies all processors and listeners set on the Spider |
||
| 241 | * |
||
| 242 | * This is a either depth first algorithm as explained here: |
||
| 243 | * https://en.wikipedia.org/wiki/Depth-first_search#Example |
||
| 244 | * Note that because we don't do it recursive, but iteratively, |
||
| 245 | * results will be in a different order from the example, because |
||
| 246 | * we always take the right-most child first, whereas a recursive |
||
| 247 | * variant would always take the left-most child first |
||
| 248 | * |
||
| 249 | * or |
||
| 250 | * |
||
| 251 | * a breadth first algorithm |
||
| 252 | * |
||
| 253 | * @return void |
||
| 254 | */ |
||
| 255 | private function doCrawl() |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @param UriInterface $uri |
||
| 290 | * @return Resource |
||
| 291 | */ |
||
| 292 | protected function fetchResource(UriInterface $uri) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * A shortcut for EventDispatcher::dispatch() |
||
| 320 | * |
||
| 321 | * @param string $eventName |
||
| 322 | * @param Event $event |
||
| 323 | */ |
||
| 324 | private function dispatch($eventName, Event $event = null) |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @param string $uri |
||
| 331 | */ |
||
| 332 | private function setSeed($uri) |
||
| 337 | } |
||
| 338 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.