Complex classes like CrawlerApi 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 CrawlerApi, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
40 | class CrawlerApi |
||
41 | { |
||
42 | /** |
||
43 | * @var CrawlerController |
||
44 | */ |
||
45 | private $crawlerController; |
||
46 | |||
47 | /** |
||
48 | * @var QueueRepository |
||
49 | */ |
||
50 | protected $queueRepository; |
||
51 | |||
52 | /** |
||
53 | * @var $allowedConfigurations array |
||
54 | */ |
||
55 | protected $allowedConfigurations = []; |
||
56 | |||
57 | /** |
||
58 | * Each crawler run has a setid, this facade method delegates |
||
59 | * the it to the crawler object |
||
60 | * |
||
61 | * @param int |
||
62 | */ |
||
63 | public function overwriteSetId($id) |
||
67 | |||
68 | /** |
||
69 | * This method is used to limit the configuration selection to |
||
70 | * a set of configurations. |
||
71 | * |
||
72 | * @param array $allowedConfigurations |
||
73 | */ |
||
74 | public function setAllowedConfigurations(array $allowedConfigurations) |
||
78 | |||
79 | /** |
||
80 | * Returns the setID of the crawler |
||
81 | * |
||
82 | * @return int |
||
83 | */ |
||
84 | public function getSetId() |
||
88 | |||
89 | /** |
||
90 | * Method to get an instance of the internal crawler singleton |
||
91 | * |
||
92 | * @return CrawlerController Instance of the crawler lib |
||
93 | * |
||
94 | * @throws \Exception |
||
95 | */ |
||
96 | protected function findCrawler() |
||
109 | |||
110 | /** |
||
111 | * Adds a page to the crawlerqueue by uid |
||
112 | * |
||
113 | * @param int $uid uid |
||
114 | */ |
||
115 | public function addPageToQueue($uid) |
||
121 | |||
122 | /** |
||
123 | * This method is used to limit the processing instructions to the processing instructions |
||
124 | * that are allowed. |
||
125 | * |
||
126 | * @return array |
||
127 | */ |
||
128 | protected function filterUnallowedConfigurations($configurations) |
||
141 | |||
142 | /** |
||
143 | * Adds a page to the crawlerqueue by uid and sets a |
||
144 | * timestamp when the page should be crawled. |
||
145 | * |
||
146 | * @param int $uid pageid |
||
147 | * @param int $time timestamp |
||
148 | */ |
||
149 | public function addPageToQueueTimed($uid, $time) |
||
184 | |||
185 | /** |
||
186 | * Counts all entrys in the database which are scheduled for a given page id and a schedule timestamp. |
||
187 | * |
||
188 | * @param int $page_uid |
||
189 | * @param int $schedule_timestamp |
||
190 | * |
||
191 | * @return int |
||
192 | */ |
||
193 | protected function countEntriesInQueueForPageByScheduletime($page_uid, $schedule_timestamp) |
||
214 | |||
215 | /** |
||
216 | * Determines if a page is queued |
||
217 | * |
||
218 | * @param $uid |
||
219 | * @param bool $unprocessed_only |
||
220 | * @param bool $timed_only |
||
221 | * @param bool $timestamp |
||
222 | * |
||
223 | * @return bool |
||
224 | */ |
||
225 | public function isPageInQueue($uid, $unprocessed_only = true, $timed_only = false, $timestamp = false) |
||
259 | |||
260 | /** |
||
261 | * Method to return the latest Crawle Timestamp for a page. |
||
262 | * |
||
263 | * @param int $uid uid id of the page |
||
264 | * @param bool $future_crawldates_only |
||
265 | * @param bool $unprocessed_only |
||
266 | * |
||
267 | * @return int |
||
268 | */ |
||
269 | public function getLatestCrawlTimestampForPage($uid, $future_crawldates_only = false, $unprocessed_only = false) |
||
292 | |||
293 | /** |
||
294 | * Returns an array with timestamps when the page has been scheduled for crawling and |
||
295 | * at what time the scheduled crawl has been executed. The array also contains items that are |
||
296 | * scheduled but have note been crawled yet. |
||
297 | * |
||
298 | * @param int $uid uid of the page |
||
299 | * @param bool $limit |
||
300 | * |
||
301 | * @return array array with the crawlhistory of a page => 0 : scheduled time , 1 : execuded_time, 2 : set_id |
||
302 | */ |
||
303 | public function getCrawlHistoryForPage($uid, $limit = false) |
||
317 | |||
318 | /** |
||
319 | * Method to determine unprocessed Items in the crawler queue. |
||
320 | * |
||
321 | * @return array |
||
322 | */ |
||
323 | public function getUnprocessedItems() |
||
331 | |||
332 | /** |
||
333 | * Method to get the number of unprocessed items in the crawler |
||
334 | * |
||
335 | * @param int number of unprocessed items in the queue |
||
336 | */ |
||
337 | public function countUnprocessedItems() |
||
346 | |||
347 | /** |
||
348 | * Method to check if a page is in the queue which is timed for a |
||
349 | * date when it should be crawled |
||
350 | * |
||
351 | * @param int $uid uid of the page |
||
352 | * @param boolean $show_unprocessed only respect unprocessed pages |
||
353 | * |
||
354 | * @return boolean |
||
355 | */ |
||
356 | public function isPageInQueueTimed($uid, $show_unprocessed = true) |
||
362 | |||
363 | /** |
||
364 | * Reads the registered processingInstructions of the crawler |
||
365 | * |
||
366 | * @return array |
||
367 | */ |
||
368 | private function getCrawlerProcInstructions() |
||
376 | |||
377 | /** |
||
378 | * Removes an queue entry with a given queue id |
||
379 | * |
||
380 | * @param int $qid |
||
381 | */ |
||
382 | public function removeQueueEntrie($qid) |
||
389 | |||
390 | /** |
||
391 | * Get queue statistics |
||
392 | * |
||
393 | * @param void |
||
394 | * |
||
395 | * @return array array('assignedButUnprocessed' => <>, 'unprocessed' => <>); |
||
396 | */ |
||
397 | public function getQueueStatistics() |
||
404 | |||
405 | /** |
||
406 | * Get queue repository |
||
407 | * |
||
408 | * @return QueueRepository |
||
409 | */ |
||
410 | protected function getQueueRepository() |
||
418 | |||
419 | /** |
||
420 | * Get queue statistics by configuration |
||
421 | * |
||
422 | * @return array array of array('configuration' => <>, 'assignedButUnprocessed' => <>, 'unprocessed' => <>) |
||
423 | */ |
||
424 | public function getQueueStatisticsByConfiguration() |
||
439 | |||
440 | /** |
||
441 | * Get active processes count |
||
442 | * |
||
443 | * @param void |
||
444 | * |
||
445 | * @return int |
||
446 | */ |
||
447 | public function getActiveProcessesCount() |
||
453 | |||
454 | /** |
||
455 | * Get last processed entries |
||
456 | * |
||
457 | * @param int limit |
||
458 | * |
||
459 | * @return array |
||
460 | */ |
||
461 | public function getLastProcessedQueueEntries($limit) |
||
465 | |||
466 | /** |
||
467 | * Get current crawling speed |
||
468 | * |
||
469 | * @param float|false page speed in pages per minute |
||
470 | * |
||
471 | * @return int |
||
472 | */ |
||
473 | public function getCurrentCrawlingSpeed() |
||
508 | |||
509 | /** |
||
510 | * Get some performance data |
||
511 | * |
||
512 | * @param integer $start |
||
513 | * @param integer $end |
||
514 | * @param integer $resolution |
||
515 | * |
||
516 | * @return array data |
||
517 | * |
||
518 | * @throws \Exception |
||
519 | */ |
||
520 | public function getPerformanceData($start, $end, $resolution) |
||
569 | } |
||
570 |
This checks looks for assignemnts to variables using the
list(...)
function, where not all assigned variables are subsequently used.Consider the following code example.
Only the variables
$a
and$c
are used. There was no need to assign$b
.Instead, the list call could have been.