Complex classes like Queue 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 Queue, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
40 | class Queue |
||
41 | { |
||
42 | |||
43 | // FIXME some of the methods should be renamed to plural forms |
||
44 | // FIXME singular form methods should deal with exactly one item only |
||
45 | |||
46 | /** |
||
47 | * Returns the timestamp of the last indexing run. |
||
48 | * |
||
49 | * @param int $rootPageId The root page uid for which to get |
||
50 | * the last indexed item id |
||
51 | * @return int Timestamp of last index run. |
||
52 | */ |
||
53 | public function getLastIndexTime($rootPageId) |
||
72 | |||
73 | /** |
||
74 | * Returns the uid of the last indexed item in the queue |
||
75 | * |
||
76 | * @param int $rootPageId The root page uid for which to get |
||
77 | * the last indexed item id |
||
78 | * @return int The last indexed item's ID. |
||
79 | */ |
||
80 | public function getLastIndexedItemId($rootPageId) |
||
98 | |||
99 | /** |
||
100 | * Truncate and rebuild the tx_solr_indexqueue_item table. This is the most |
||
101 | * complete way to force reindexing, or to build the Index Queue for the |
||
102 | * first time. The Index Queue initialization is site-specific. |
||
103 | * |
||
104 | * @param Site $site The site to initialize |
||
105 | * @param string $indexingConfigurationName Name of a specific |
||
106 | * indexing configuration |
||
107 | * @return array An array of booleans, each representing whether the |
||
108 | * initialization for an indexing configuration was successful |
||
109 | */ |
||
110 | 5 | public function initialize(Site $site, $indexingConfigurationName = '') |
|
151 | |||
152 | /** |
||
153 | * Initializes the Index Queue for a specific indexing configuration. |
||
154 | * |
||
155 | * @param Site $site The site to initialize |
||
156 | * @param string $indexingConfigurationName name of a specific |
||
157 | * indexing configuration |
||
158 | * @return bool TRUE if the initialization was successful, FALSE otherwise |
||
159 | */ |
||
160 | 5 | protected function initializeIndexingConfiguration( |
|
183 | |||
184 | /** |
||
185 | * Gets the indexing configuration to use for an item. |
||
186 | * Sometimes, when there are multiple configurations for a certain item type |
||
187 | * (table) it can be hard or even impossible to find which one to use |
||
188 | * though. |
||
189 | * Currently selects the first indexing configuration where the name matches |
||
190 | * the itemType or where the configured tbale is the same as the itemType. |
||
191 | * |
||
192 | * !!! Might return incorrect results for complex configurations !!! |
||
193 | * Try to set the indexingConfiguration directly when using the updateItem() |
||
194 | * method in such situations. |
||
195 | * |
||
196 | * @param string $itemType The item's type, usually a table name. |
||
197 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
198 | * different value for non-database-record types. |
||
199 | * @param int $rootPageId The configuration's page tree's root page id. |
||
200 | * Optional, not needed for all types. |
||
201 | * @return string The indexing configuration's name to use when indexing |
||
202 | * @deprecated Use getIndexingConfigurationsByItem() now, which behaves |
||
203 | * almost the same way but returns an array of configurations, will be removed in version 7.0 |
||
204 | */ |
||
205 | protected function getIndexingConfigurationByItem( |
||
221 | |||
222 | /** |
||
223 | * Gets the indexing configurations to use for an item. |
||
224 | * Multiple configurations for a certain item type (table) might be available. |
||
225 | * |
||
226 | * @param string $itemType The item's type, usually a table name. |
||
227 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
228 | * different value for non-database-record types. |
||
229 | * @param int $rootPageId The configuration's page tree's root page id. |
||
230 | * Optional, not needed for all types. |
||
231 | * @return array<string> The indexing configurations names to use when indexing |
||
232 | */ |
||
233 | 9 | protected function getIndexingConfigurationsByItem( |
|
248 | |||
249 | /** |
||
250 | * Marks an item as needing (re)indexing. |
||
251 | * |
||
252 | * Like with Solr itself, there's no add method, just a simple update method |
||
253 | * that handles the adds, too. |
||
254 | * |
||
255 | * @param string $itemType The item's type, usually a table name. |
||
256 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
257 | * different value for non-database-record types. |
||
258 | * @param string $indexingConfiguration The item's indexing configuration to use. |
||
259 | * Optional, overwrites existing / determined configuration. |
||
260 | * @param int $forcedChangeTime The change time for the item if set, otherwise |
||
261 | * value from getItemChangedTime() is used. |
||
262 | */ |
||
263 | 43 | public function updateItem( |
|
264 | $itemType, |
||
265 | $itemUid, |
||
266 | $indexingConfiguration = null, |
||
267 | $forcedChangeTime = 0 |
||
268 | ) { |
||
269 | 43 | $itemInQueue = $this->containsItem($itemType, $itemUid); |
|
270 | 43 | if ($itemInQueue) { |
|
271 | // update if that item is in the queue already |
||
272 | $changes = [ |
||
273 | 9 | 'changed' => ($forcedChangeTime > 0) |
|
274 | 1 | ? $forcedChangeTime |
|
275 | 9 | : $this->getItemChangedTime($itemType, $itemUid) |
|
276 | ]; |
||
277 | |||
278 | 9 | if (!empty($indexingConfiguration)) { |
|
279 | 4 | $changes['indexing_configuration'] = $indexingConfiguration; |
|
280 | } |
||
281 | |||
282 | 9 | $GLOBALS['TYPO3_DB']->exec_UPDATEquery( |
|
283 | 9 | 'tx_solr_indexqueue_item', |
|
284 | 9 | 'item_type = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($itemType, |
|
285 | 9 | 'tx_solr_indexqueue_item') . |
|
286 | 9 | ' AND item_uid = ' . (int)$itemUid, |
|
287 | $changes |
||
288 | ); |
||
289 | } else { |
||
290 | // add the item since it's not in the queue yet |
||
291 | 38 | $this->addItem($itemType, $itemUid, $indexingConfiguration); |
|
292 | } |
||
293 | 43 | } |
|
294 | |||
295 | /** |
||
296 | * Adds an item to the index queue. |
||
297 | * |
||
298 | * Not meant for public use. |
||
299 | * |
||
300 | * @param string $itemType The item's type, usually a table name. |
||
301 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
302 | * different value for non-database-record types. |
||
303 | * @param string $indexingConfiguration The item's indexing configuration to use. |
||
304 | * Optional, overwrites existing / determined configuration. |
||
305 | * @return void |
||
306 | */ |
||
307 | 38 | private function addItem($itemType, $itemUid, $indexingConfiguration) |
|
379 | |||
380 | /** |
||
381 | * Determines the time for when an item should be indexed. This timestamp |
||
382 | * is then stored in the changed column in the Index Queue. |
||
383 | * |
||
384 | * The changed timestamp usually is now - time(). For records which are set |
||
385 | * to published at a later time, this timestamp is the start time. So if a |
||
386 | * future start time has been set, that will be used to delay indexing |
||
387 | * of an item. |
||
388 | * |
||
389 | * @param string $itemType The item's table name. |
||
390 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
391 | * different value for non-database-record types. |
||
392 | * @return int Timestamp of the item's changed time or future start time |
||
393 | */ |
||
394 | 41 | protected function getItemChangedTime($itemType, $itemUid) |
|
441 | |||
442 | /** |
||
443 | * Gets the most recent changed time of a page's content elements |
||
444 | * |
||
445 | * @param array $page Partial page record |
||
446 | * @return int Timestamp of the most recent content element change |
||
447 | */ |
||
448 | 31 | protected function getPageItemChangedTime(array $page) |
|
464 | |||
465 | /** |
||
466 | * Gets the most recent changed time for an item taking into account |
||
467 | * localized records. |
||
468 | * |
||
469 | * @param string $itemType The item's type, usually a table name. |
||
470 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
471 | * different value for non-database-record types. |
||
472 | * @return int Timestamp of the most recent content element change |
||
473 | */ |
||
474 | 41 | protected function getLocalizableItemChangedTime($itemType, $itemUid) |
|
493 | |||
494 | /** |
||
495 | * Checks whether the Index Queue contains a specific item. |
||
496 | * |
||
497 | * @param string $itemType The item's type, usually a table name. |
||
498 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
499 | * different value for non-database-record types. |
||
500 | * @return bool TRUE if the item is found in the queue, FALSE otherwise |
||
501 | */ |
||
502 | 45 | public function containsItem($itemType, $itemUid) |
|
514 | |||
515 | /** |
||
516 | * Checks whether the Index Queue contains a specific item that has been |
||
517 | * marked as indexed. |
||
518 | * |
||
519 | * @param string $itemType The item's type, usually a table name. |
||
520 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
521 | * different value for non-database-record types. |
||
522 | * @return bool TRUE if the item is found in the queue and marked as |
||
523 | * indexed, FALSE otherwise |
||
524 | */ |
||
525 | 3 | public function containsIndexedItem($itemType, $itemUid) |
|
538 | |||
539 | /** |
||
540 | * Removes an item from the Index Queue. |
||
541 | * |
||
542 | * @param string $itemType The type of the item to remove, usually a table name. |
||
543 | * @param int $itemUid The uid of the item to remove |
||
544 | */ |
||
545 | 26 | public function deleteItem($itemType, $itemUid) |
|
573 | |||
574 | /** |
||
575 | * Removes all items of a certain type from the Index Queue. |
||
576 | * |
||
577 | * @param string $itemType The type of items to remove, usually a table name. |
||
578 | */ |
||
579 | 1 | public function deleteItemsByType($itemType) |
|
608 | |||
609 | /** |
||
610 | * Removes all items of a certain site from the Index Queue. Accepts an |
||
611 | * optional parameter to limit the deleted items by indexing configuration. |
||
612 | * |
||
613 | * @param Site $site The site to remove items for. |
||
614 | * @param string $indexingConfigurationName Name of a specific indexing |
||
615 | * configuration |
||
616 | */ |
||
617 | 5 | public function deleteItemsBySite( |
|
667 | |||
668 | /** |
||
669 | * Removes all items from the Index Queue. |
||
670 | * |
||
671 | */ |
||
672 | public function deleteAllItems() |
||
676 | |||
677 | /** |
||
678 | * Gets a single Index Queue item by its uid. |
||
679 | * |
||
680 | * @param int $itemId Index Queue item uid |
||
681 | * @return Item The request Index Queue item or NULL |
||
682 | * if no item with $itemId was found |
||
683 | */ |
||
684 | 10 | public function getItem($itemId) |
|
705 | |||
706 | /** |
||
707 | * Gets Index Queue items by type and uid. |
||
708 | * |
||
709 | * @param string $itemType item type, usually the table name |
||
710 | * @param int $itemUid item uid |
||
711 | * @return Item[] An array of items matching $itemType and $itemUid |
||
712 | */ |
||
713 | 19 | public function getItems($itemType, $itemUid) |
|
725 | |||
726 | /** |
||
727 | * Gets number of Index Queue items for a specific site / indexing configuration |
||
728 | * optional parameter to limit the counted items by indexing configuration. |
||
729 | * |
||
730 | * @param Site $site The site to search for. |
||
731 | * @param string $indexingConfigurationName name of a specific indexing |
||
732 | * configuration |
||
733 | * @return int Number of items |
||
734 | */ |
||
735 | public function getItemsCountBySite(Site $site, $indexingConfigurationName = '') |
||
741 | |||
742 | /** |
||
743 | * Gets number of unprocessed Index Queue items for a specific site / indexing configuration |
||
744 | * optional parameter to limit the counted items by indexing configuration. |
||
745 | * |
||
746 | * @param Site $site The site to search for. |
||
747 | * @param string $indexingConfigurationName name of a specific indexing |
||
748 | * configuration |
||
749 | * @return int Number of items. |
||
750 | */ |
||
751 | public function getRemainingItemsCountBySite(Site $site, $indexingConfigurationName = '') |
||
757 | |||
758 | /** |
||
759 | 44 | * Returns the number of items for all queues. |
|
760 | * |
||
761 | 44 | * @return int |
|
762 | */ |
||
763 | 44 | public function getAllItemsCount() |
|
767 | |||
768 | 44 | /** |
|
769 | * @param string $where |
||
770 | * @return int |
||
771 | */ |
||
772 | private function getItemCount($where = '1=1') |
||
779 | |||
780 | 7 | /** |
|
781 | * Build a database constraint that limits to a certain indexConfigurationName |
||
782 | * |
||
783 | 7 | * @param string $indexingConfigurationName |
|
784 | 7 | * @return string |
|
785 | 7 | */ |
|
786 | 7 | protected function buildIndexConfigurationConstraint($indexingConfigurationName) |
|
795 | |||
796 | 7 | /** |
|
797 | * Gets $limit number of items to index for a particular $site. |
||
798 | * |
||
799 | 7 | * @param Site $site TYPO3 site |
|
800 | * @param int $limit Number of items to get from the queue |
||
801 | * @return Item[] Items to index to the given solr server |
||
802 | */ |
||
803 | public function getItemsToIndex(Site $site, $limit = 50) |
||
826 | |||
827 | 24 | /** |
|
828 | 24 | * Creates an array of ApacheSolrForTypo3\Solr\IndexQueue\Item objects from an array of |
|
829 | 24 | * index queue records. |
|
830 | * |
||
831 | 24 | * @param array $indexQueueItemRecords Array of plain index queue records |
|
832 | * @return array Array of ApacheSolrForTypo3\Solr\IndexQueue\Item objects |
||
833 | 24 | */ |
|
834 | protected function getIndexQueueItemObjectsFromRecords( |
||
886 | |||
887 | /** |
||
888 | * Marks an item as failed and causes the indexer to skip the item in the |
||
889 | * next run. |
||
890 | * |
||
891 | * @param int|Item $item Either the item's Index Queue |
||
892 | * uid or the complete item |
||
893 | * @param string $errorMessage Error message |
||
894 | */ |
||
895 | public function markItemAsFailed($item, $errorMessage = '') |
||
916 | } |
||
917 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.