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 | 1 | 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 | 1 | public function setAllowedConfigurations(array $allowedConfigurations) |
|
78 | |||
79 | /** |
||
80 | * @return array |
||
81 | */ |
||
82 | 1 | public function getAllowedConfigurations() |
|
86 | |||
87 | /** |
||
88 | * Returns the setID of the crawler |
||
89 | * |
||
90 | * @return int |
||
91 | */ |
||
92 | 1 | public function getSetId() |
|
96 | |||
97 | /** |
||
98 | * Method to get an instance of the internal crawler singleton |
||
99 | * |
||
100 | * @return CrawlerController Instance of the crawler lib |
||
101 | * |
||
102 | * @throws \Exception |
||
103 | */ |
||
104 | 2 | protected function findCrawler() |
|
117 | |||
118 | /** |
||
119 | * Adds a page to the crawlerqueue by uid |
||
120 | * |
||
121 | * @param int $uid uid |
||
122 | */ |
||
123 | public function addPageToQueue($uid) |
||
124 | { |
||
125 | $uid = intval($uid); |
||
126 | //non timed elements will be added with timestamp 0 |
||
127 | $this->addPageToQueueTimed($uid, 0); |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * This method is used to limit the processing instructions to the processing instructions |
||
132 | * that are allowed. |
||
133 | * |
||
134 | * @return array |
||
135 | */ |
||
136 | 4 | protected function filterUnallowedConfigurations($configurations) |
|
149 | |||
150 | /** |
||
151 | * Adds a page to the crawlerqueue by uid and sets a |
||
152 | * timestamp when the page should be crawled. |
||
153 | * |
||
154 | * @param int $uid pageid |
||
155 | * @param int $time timestamp |
||
156 | */ |
||
157 | 4 | public function addPageToQueueTimed($uid, $time) |
|
192 | |||
193 | /** |
||
194 | * Counts all entrys in the database which are scheduled for a given page id and a schedule timestamp. |
||
195 | * |
||
196 | * @param int $page_uid |
||
197 | * @param int $schedule_timestamp |
||
198 | * |
||
199 | * @return int |
||
200 | */ |
||
201 | 1 | protected function countEntriesInQueueForPageByScheduleTime($page_uid, $schedule_timestamp) |
|
202 | { |
||
203 | //if the same page is scheduled for the same time and has not be executed? |
||
204 | 1 | if ($schedule_timestamp == 0) { |
|
205 | //un-timed elements need an exec_time with 0 because they can occur multiple times |
||
206 | 1 | $where = 'page_id=' . $page_uid . ' AND exec_time = 0 AND scheduled=' . $schedule_timestamp; |
|
207 | } else { |
||
208 | //timed elements have got a fixed schedule time, if a record with this time |
||
209 | //exists it is maybe queued for the future, or is has been queue for the past and therefore |
||
210 | //also been processed. |
||
211 | 1 | $where = 'page_id=' . $page_uid . ' AND scheduled=' . $schedule_timestamp; |
|
212 | } |
||
213 | |||
214 | 1 | $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($GLOBALS['TYPO3_DB']->exec_SELECTquery( |
|
215 | 1 | 'count(*) as cnt', |
|
216 | 1 | 'tx_crawler_queue', |
|
217 | 1 | $where |
|
218 | )); |
||
219 | |||
220 | 1 | return intval($row['cnt']); |
|
221 | } |
||
222 | |||
223 | /** |
||
224 | * Determines if a page is queued |
||
225 | * |
||
226 | * @param $uid |
||
227 | * @param bool $unprocessed_only |
||
228 | * @param bool $timed_only |
||
229 | * @param bool $timestamp |
||
230 | * |
||
231 | * @return bool |
||
232 | */ |
||
233 | 6 | public function isPageInQueue($uid, $unprocessed_only = true, $timed_only = false, $timestamp = false) |
|
267 | |||
268 | /** |
||
269 | * Method to return the latest Crawle Timestamp for a page. |
||
270 | * |
||
271 | * @param int $uid uid id of the page |
||
272 | * @param bool $future_crawldates_only |
||
273 | * @param bool $unprocessed_only |
||
274 | * |
||
275 | * @return int |
||
276 | */ |
||
277 | 1 | public function getLatestCrawlTimestampForPage($uid, $future_crawldates_only = false, $unprocessed_only = false) |
|
300 | |||
301 | /** |
||
302 | * Returns an array with timestamps when the page has been scheduled for crawling and |
||
303 | * at what time the scheduled crawl has been executed. The array also contains items that are |
||
304 | * scheduled but have note been crawled yet. |
||
305 | * |
||
306 | * @param int $uid uid of the page |
||
307 | * @param bool $limit |
||
308 | * |
||
309 | * @return array array with the crawl-history of a page => 0 : scheduled time , 1 : executed_time, 2 : set_id |
||
310 | */ |
||
311 | 1 | public function getCrawlHistoryForPage($uid, $limit = 0) |
|
324 | |||
325 | /** |
||
326 | * Method to determine unprocessed Items in the crawler queue. |
||
327 | * |
||
328 | * @return array |
||
329 | */ |
||
330 | 1 | public function getUnprocessedItems() |
|
338 | |||
339 | /** |
||
340 | * Method to get the number of unprocessed items in the crawler |
||
341 | * |
||
342 | * @param int number of unprocessed items in the queue |
||
343 | */ |
||
344 | 4 | public function countUnprocessedItems() |
|
353 | |||
354 | /** |
||
355 | * Method to check if a page is in the queue which is timed for a |
||
356 | * date when it should be crawled |
||
357 | * |
||
358 | * @param int $uid uid of the page |
||
359 | * @param boolean $show_unprocessed only respect unprocessed pages |
||
360 | * |
||
361 | * @return boolean |
||
362 | */ |
||
363 | 1 | public function isPageInQueueTimed($uid, $show_unprocessed = true) |
|
369 | |||
370 | /** |
||
371 | * Reads the registered processingInstructions of the crawler |
||
372 | * |
||
373 | * @return array |
||
374 | */ |
||
375 | 4 | private function getCrawlerProcInstructions() |
|
383 | |||
384 | /** |
||
385 | * Removes an queue entry with a given queue id |
||
386 | * |
||
387 | * @param int $qid |
||
388 | */ |
||
389 | public function removeQueueEntrie($qid) |
||
396 | |||
397 | /** |
||
398 | * Get queue statistics |
||
399 | * |
||
400 | * @param void |
||
401 | * |
||
402 | * @return array array('assignedButUnprocessed' => <>, 'unprocessed' => <>); |
||
403 | */ |
||
404 | 1 | public function getQueueStatistics() |
|
411 | |||
412 | /** |
||
413 | * Get queue repository |
||
414 | * |
||
415 | * @return QueueRepository |
||
416 | */ |
||
417 | 2 | protected function getQueueRepository() |
|
425 | |||
426 | /** |
||
427 | * Get queue statistics by configuration |
||
428 | * |
||
429 | * @return array array of array('configuration' => <>, 'assignedButUnprocessed' => <>, 'unprocessed' => <>) |
||
430 | */ |
||
431 | public function getQueueStatisticsByConfiguration() |
||
446 | |||
447 | /** |
||
448 | * Get active processes count |
||
449 | * |
||
450 | * @param void |
||
451 | * |
||
452 | * @return int |
||
453 | */ |
||
454 | public function getActiveProcessesCount() |
||
460 | |||
461 | /** |
||
462 | * Get last processed entries |
||
463 | * |
||
464 | * @param int $limit |
||
465 | * |
||
466 | * @return array |
||
467 | */ |
||
468 | public function getLastProcessedQueueEntries($limit) |
||
472 | |||
473 | /** |
||
474 | * Get current crawling speed |
||
475 | * |
||
476 | * @param float|false page speed in pages per minute |
||
477 | * |
||
478 | * @return int |
||
479 | */ |
||
480 | public function getCurrentCrawlingSpeed() |
||
515 | |||
516 | /** |
||
517 | * Get some performance data |
||
518 | * |
||
519 | * @param integer $start |
||
520 | * @param integer $end |
||
521 | * @param integer $resolution |
||
522 | * |
||
523 | * @return array data |
||
524 | * |
||
525 | * @throws \Exception |
||
526 | */ |
||
527 | public function getPerformanceData($start, $end, $resolution) |
||
576 | } |
||
577 |
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.