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 |
||
39 | class Queue |
||
40 | { |
||
41 | |||
42 | // FIXME some of the methods should be renamed to plural forms |
||
43 | // FIXME singular form methods should deal with exactly one item only |
||
44 | |||
45 | /** |
||
46 | * Returns the timestamp of the last indexing run. |
||
47 | * |
||
48 | * @param int $rootPageId The root page uid for which to get |
||
49 | * the last indexed item id |
||
50 | * @return int Timestamp of last index run. |
||
51 | */ |
||
52 | public function getLastIndexTime($rootPageId) |
||
71 | |||
72 | /** |
||
73 | * Returns the uid of the last indexed item in the queue |
||
74 | * |
||
75 | * @param int $rootPageId The root page uid for which to get |
||
76 | * the last indexed item id |
||
77 | * @return int The last indexed item's ID. |
||
78 | */ |
||
79 | public function getLastIndexedItemId($rootPageId) |
||
97 | |||
98 | /** |
||
99 | * Truncate and rebuild the tx_solr_indexqueue_item table. This is the most |
||
100 | * complete way to force reindexing, or to build the Index Queue for the |
||
101 | * first time. The Index Queue initialization is site-specific. |
||
102 | * |
||
103 | * @param Site $site The site to initialize |
||
104 | * @param string $indexingConfigurationName Name of a specific |
||
105 | * indexing configuration |
||
106 | * @return array An array of booleans, each representing whether the |
||
107 | * initialization for an indexing configuration was successful |
||
108 | */ |
||
109 | 4 | public function initialize(Site $site, $indexingConfigurationName = '') |
|
150 | |||
151 | /** |
||
152 | * Initializes the Index Queue for a specific indexing configuration. |
||
153 | * |
||
154 | * @param Site $site The site to initialize |
||
155 | * @param string $indexingConfigurationName name of a specific |
||
156 | * indexing configuration |
||
157 | * @return bool TRUE if the initialization was successful, FALSE otherwise |
||
158 | */ |
||
159 | 4 | protected function initializeIndexingConfiguration( |
|
182 | |||
183 | /** |
||
184 | * Gets the indexing configuration to use for an item. |
||
185 | * Sometimes, when there are multiple configurations for a certain item type |
||
186 | * (table) it can be hard or even impossible to find which one to use |
||
187 | * though. |
||
188 | * Currently selects the first indexing configuration where the name matches |
||
189 | * the itemType or where the configured tbale is the same as the itemType. |
||
190 | * |
||
191 | * !!! Might return incorrect results for complex configurations !!! |
||
192 | * Try to set the indexingConfiguration directly when using the updateItem() |
||
193 | * method in such situations. |
||
194 | * |
||
195 | * @param string $itemType The item's type, usually a table name. |
||
196 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
197 | * different value for non-database-record types. |
||
198 | * @param int $rootPageId The configuration's page tree's root page id. |
||
199 | * Optional, not needed for all types. |
||
200 | * @return string The indexing configuration's name to use when indexing |
||
201 | * @deprecated Use getIndexingConfigurationsByItem() now, which behaves |
||
202 | * almost the same way but returns an array of configurations |
||
203 | */ |
||
204 | protected function getIndexingConfigurationByItem( |
||
205 | $itemType, |
||
206 | $itemUid, |
||
207 | $rootPageId = null |
||
208 | ) { |
||
209 | $indexingConfigurationName = ''; |
||
210 | |||
211 | $configurations = $this->getIndexingConfigurationsByItem($itemType, |
||
212 | $itemUid, $rootPageId); |
||
213 | if (count($configurations) > 0) { |
||
214 | $indexingConfigurationName = $configurations[0]; |
||
215 | } |
||
216 | |||
217 | return $indexingConfigurationName; |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * Gets the indexing configurations to use for an item. |
||
222 | * Multiple configurations for a certain item type (table) might be available. |
||
223 | * |
||
224 | * @param string $itemType The item's type, usually a table name. |
||
225 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
226 | * different value for non-database-record types. |
||
227 | * @param int $rootPageId The configuration's page tree's root page id. |
||
228 | * Optional, not needed for all types. |
||
229 | * @return array<string> The indexing configurations names to use when indexing |
||
230 | */ |
||
231 | 21 | protected function getIndexingConfigurationsByItem( |
|
246 | |||
247 | /** |
||
248 | * Marks an item as needing (re)indexing. |
||
249 | * |
||
250 | * Like with Solr itself, there's no add method, just a simple update method |
||
251 | * that handles the adds, too. |
||
252 | * |
||
253 | * @param string $itemType The item's type, usually a table name. |
||
254 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
255 | * different value for non-database-record types. |
||
256 | * @param string $indexingConfiguration The item's indexing configuration to use. |
||
257 | * Optional, overwrites existing / determined configuration. |
||
258 | * @param int $forcedChangeTime The change time for the item if set, otherwise |
||
259 | * value from getItemChangedTime() is used. |
||
260 | */ |
||
261 | 28 | public function updateItem( |
|
293 | |||
294 | /** |
||
295 | * Adds an item to the index queue. |
||
296 | * |
||
297 | * Not meant for public use. |
||
298 | * |
||
299 | * @param string $itemType The item's type, usually a table name. |
||
300 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
301 | * different value for non-database-record types. |
||
302 | * @param string $indexingConfiguration The item's indexing configuration to use. |
||
303 | * Optional, overwrites existing / determined configuration. |
||
304 | * @return void |
||
305 | */ |
||
306 | 24 | private function addItem($itemType, $itemUid, $indexingConfiguration) |
|
377 | |||
378 | /** |
||
379 | * Determines the time for when an item should be indexed. This timestamp |
||
380 | * is then stored in the changed column in the Index Queue. |
||
381 | * |
||
382 | * The changed timestamp usually is now - time(). For records which are set |
||
383 | * to published at a later time, this timestamp is the start time. So if a |
||
384 | * future start time has been set, that will be used to delay indexing |
||
385 | * of an item. |
||
386 | * |
||
387 | * @param string $itemType The item's table name. |
||
388 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
389 | * different value for non-database-record types. |
||
390 | * @return int Timestamp of the item's changed time or future start time |
||
391 | */ |
||
392 | 26 | protected function getItemChangedTime($itemType, $itemUid) |
|
439 | |||
440 | /** |
||
441 | * Gets the most recent changed time of a page's content elements |
||
442 | * |
||
443 | * @param array $page Partial page record |
||
444 | * @return int Timestamp of the most recent content element change |
||
445 | */ |
||
446 | 16 | protected function getPageItemChangedTime(array $page) |
|
462 | |||
463 | /** |
||
464 | * Gets the most recent changed time for an item taking into account |
||
465 | * localized records. |
||
466 | * |
||
467 | * @param string $itemType The item's type, usually a table name. |
||
468 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
469 | * different value for non-database-record types. |
||
470 | * @return int Timestamp of the most recent content element change |
||
471 | */ |
||
472 | 26 | protected function getLocalizableItemChangedTime($itemType, $itemUid) |
|
491 | |||
492 | /** |
||
493 | * Checks whether the Index Queue contains a specific item. |
||
494 | * |
||
495 | * @param string $itemType The item's type, usually a table name. |
||
496 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
497 | * different value for non-database-record types. |
||
498 | * @return bool TRUE if the item is found in the queue, FALSE otherwise |
||
499 | */ |
||
500 | 30 | public function containsItem($itemType, $itemUid) |
|
512 | |||
513 | /** |
||
514 | * Checks whether the Index Queue contains a specific item that has been |
||
515 | * marked as indexed. |
||
516 | * |
||
517 | * @param string $itemType The item's type, usually a table name. |
||
518 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
519 | * different value for non-database-record types. |
||
520 | * @return bool TRUE if the item is found in the queue and marked as |
||
521 | * indexed, FALSE otherwise |
||
522 | */ |
||
523 | 1 | public function containsIndexedItem($itemType, $itemUid) |
|
536 | |||
537 | /** |
||
538 | * Removes an item from the Index Queue. |
||
539 | * |
||
540 | * @param string $itemType The type of the item to remove, usually a table name. |
||
541 | * @param int $itemUid The uid of the item to remove |
||
542 | */ |
||
543 | 10 | public function deleteItem($itemType, $itemUid) |
|
571 | |||
572 | /** |
||
573 | * Removes all items of a certain type from the Index Queue. |
||
574 | * |
||
575 | * @param string $itemType The type of items to remove, usually a table name. |
||
576 | */ |
||
577 | 1 | public function deleteItemsByType($itemType) |
|
606 | |||
607 | /** |
||
608 | * Removes all items of a certain site from the Index Queue. Accepts an |
||
609 | * optional parameter to limit the deleted items by indexing configuration. |
||
610 | * |
||
611 | * @param Site $site The site to remove items for. |
||
612 | * @param string $indexingConfigurationName Name of a specific indexing |
||
613 | * configuration |
||
614 | */ |
||
615 | 4 | public function deleteItemsBySite( |
|
665 | |||
666 | /** |
||
667 | * Removes all items from the Index Queue. |
||
668 | * |
||
669 | */ |
||
670 | public function deleteAllItems() |
||
674 | |||
675 | /** |
||
676 | * Gets a single Index Queue item by its uid. |
||
677 | * |
||
678 | * @param int $itemId Index Queue item uid |
||
679 | * @return Item The request Index Queue item or NULL |
||
680 | * if no item with $itemId was found |
||
681 | */ |
||
682 | 6 | public function getItem($itemId) |
|
703 | |||
704 | /** |
||
705 | * Gets Index Queue items by type and uid. |
||
706 | * |
||
707 | * @param string $itemType item type, usually the table name |
||
708 | * @param int $itemUid item uid |
||
709 | * @return array An array of items matching $itemType and $itemUid |
||
710 | */ |
||
711 | 11 | public function getItems($itemType, $itemUid) |
|
723 | |||
724 | /** |
||
725 | * Gets number of Index Queue items for a specific site / indexing configuration |
||
726 | * optional parameter to limit the deleted items by indexing configuration. |
||
727 | * |
||
728 | * @param Site $site The site to search for. |
||
729 | * @param string $indexingConfigurationName name of a specific indexing |
||
730 | * configuration |
||
731 | * @return mixed Number of items (integer) or FALSE if something went |
||
732 | * wrong (boolean) |
||
733 | */ |
||
734 | public function getItemsCountBySite( |
||
751 | |||
752 | /** |
||
753 | * Returns the number of items for all queues. |
||
754 | * |
||
755 | * @return int |
||
756 | */ |
||
757 | 26 | public function getAllItemsCount() |
|
768 | |||
769 | /** |
||
770 | * Gets $limit number of items to index for a particular $site. |
||
771 | * |
||
772 | * @param Site $site TYPO3 site |
||
773 | * @param int $limit Number of items to get from the queue |
||
774 | * @return Item[] Items to index to the given solr server |
||
775 | */ |
||
776 | 5 | public function getItemsToIndex(Site $site, $limit = 50) |
|
799 | |||
800 | /** |
||
801 | * Creates an array of ApacheSolrForTypo3\Solr\IndexQueue\Item objects from an array of |
||
802 | * index queue records. |
||
803 | * |
||
804 | * @param array $indexQueueItemRecords Array of plain index queue records |
||
805 | * @return array Array of ApacheSolrForTypo3\Solr\IndexQueue\Item objects |
||
806 | */ |
||
807 | 16 | protected function getIndexQueueItemObjectsFromRecords( |
|
808 | array $indexQueueItemRecords |
||
809 | ) { |
||
810 | 16 | $indexQueueItems = array(); |
|
811 | 16 | $tableUids = array(); |
|
812 | 16 | $tableRecords = array(); |
|
813 | |||
814 | // grouping records by table |
||
815 | 16 | foreach ($indexQueueItemRecords as $indexQueueItemRecord) { |
|
816 | 16 | $tableUids[$indexQueueItemRecord['item_type']][] = $indexQueueItemRecord['item_uid']; |
|
817 | 16 | } |
|
818 | |||
819 | // fetching records by table, saves us a lot of single queries |
||
820 | 16 | foreach ($tableUids as $table => $uids) { |
|
821 | 16 | $uidList = implode(',', $uids); |
|
822 | 16 | $records = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows( |
|
823 | 16 | '*', |
|
824 | 16 | $table, |
|
825 | 16 | 'uid IN(' . $uidList . ')', |
|
826 | 16 | '', '', '', // group, order, limit |
|
827 | 'uid' |
||
828 | 16 | ); |
|
829 | 16 | $tableRecords[$table] = $records; |
|
830 | |||
831 | 16 | if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['postProcessFetchRecordsForIndexQueueItem'])) { |
|
832 | $params = ['table' => $table, 'uids' => $uids, 'tableRecords' => &$tableRecords]; |
||
833 | foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['postProcessFetchRecordsForIndexQueueItem'] as $reference) { |
||
834 | GeneralUtility::callUserFunction($reference, $params, $this); |
||
835 | } |
||
836 | unset($params); |
||
837 | } |
||
838 | 16 | } |
|
839 | |||
840 | // creating index queue item objects and assigning / mapping |
||
841 | // records to index queue items |
||
842 | 16 | foreach ($indexQueueItemRecords as $indexQueueItemRecord) { |
|
843 | 16 | if (isset($tableRecords[$indexQueueItemRecord['item_type']][$indexQueueItemRecord['item_uid']])) { |
|
844 | 15 | $indexQueueItems[] = GeneralUtility::makeInstance( |
|
845 | 15 | 'ApacheSolrForTypo3\\Solr\\IndexQueue\\Item', |
|
846 | 15 | $indexQueueItemRecord, |
|
847 | 15 | $tableRecords[$indexQueueItemRecord['item_type']][$indexQueueItemRecord['item_uid']] |
|
848 | 15 | ); |
|
849 | 15 | } else { |
|
850 | 1 | GeneralUtility::devLog('Record missing for Index Queue item. Item removed.', |
|
851 | 1 | 'solr', 3, array($indexQueueItemRecord)); |
|
852 | 1 | $this->deleteItem($indexQueueItemRecord['item_type'], |
|
853 | 1 | $indexQueueItemRecord['item_uid']); |
|
854 | } |
||
855 | 16 | } |
|
856 | |||
857 | 16 | return $indexQueueItems; |
|
858 | } |
||
859 | |||
860 | /** |
||
861 | * Marks an item as failed and causes the indexer to skip the item in the |
||
862 | * next run. |
||
863 | * |
||
864 | * @param int|Item $item Either the item's Index Queue |
||
865 | * uid or the complete item |
||
866 | * @param string $errorMessage Error message |
||
867 | */ |
||
868 | public function markItemAsFailed($item, $errorMessage = '') |
||
889 | } |
||
890 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.