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 |
||
44 | class Queue |
||
45 | { |
||
46 | /** |
||
47 | * @var RootPageResolver |
||
48 | */ |
||
49 | protected $rootPageResolver; |
||
50 | |||
51 | /** |
||
52 | * @var ConfigurationAwareRecordService |
||
53 | */ |
||
54 | protected $recordService; |
||
55 | |||
56 | /** |
||
57 | * @var \ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager |
||
58 | */ |
||
59 | protected $logger = null; |
||
60 | |||
61 | /** |
||
62 | * Queue constructor. |
||
63 | * @param RootPageResolver|null $rootPageResolver |
||
64 | * @param ConfigurationAwareRecordService|null $recordService |
||
65 | */ |
||
66 | 72 | public function __construct(RootPageResolver $rootPageResolver = null, ConfigurationAwareRecordService $recordService = null) |
|
72 | |||
73 | // FIXME some of the methods should be renamed to plural forms |
||
74 | // FIXME singular form methods should deal with exactly one item only |
||
75 | |||
76 | /** |
||
77 | * Returns the timestamp of the last indexing run. |
||
78 | * |
||
79 | * @param int $rootPageId The root page uid for which to get |
||
80 | * the last indexed item id |
||
81 | * @return int Timestamp of last index run. |
||
82 | */ |
||
83 | public function getLastIndexTime($rootPageId) |
||
102 | |||
103 | /** |
||
104 | * Returns the uid of the last indexed item in the queue |
||
105 | * |
||
106 | * @param int $rootPageId The root page uid for which to get |
||
107 | * the last indexed item id |
||
108 | * @return int The last indexed item's ID. |
||
109 | */ |
||
110 | public function getLastIndexedItemId($rootPageId) |
||
128 | |||
129 | /** |
||
130 | * Truncate and rebuild the tx_solr_indexqueue_item table. This is the most |
||
131 | * complete way to force reindexing, or to build the Index Queue for the |
||
132 | * first time. The Index Queue initialization is site-specific. |
||
133 | * |
||
134 | * @param Site $site The site to initialize |
||
135 | * @param string $indexingConfigurationName Name of a specific |
||
136 | * indexing configuration |
||
137 | * @return array An array of booleans, each representing whether the |
||
138 | * initialization for an indexing configuration was successful |
||
139 | */ |
||
140 | 6 | public function initialize(Site $site, $indexingConfigurationName = '') |
|
181 | |||
182 | /** |
||
183 | * Initializes the Index Queue for a specific indexing configuration. |
||
184 | * |
||
185 | * @param Site $site The site to initialize |
||
186 | * @param string $indexingConfigurationName name of a specific |
||
187 | * indexing configuration |
||
188 | * @return bool TRUE if the initialization was successful, FALSE otherwise |
||
189 | */ |
||
190 | 6 | protected function initializeIndexingConfiguration( |
|
213 | |||
214 | /** |
||
215 | * Gets the indexing configuration to use for an item. |
||
216 | * Sometimes, when there are multiple configurations for a certain item type |
||
217 | * (table) it can be hard or even impossible to find which one to use |
||
218 | * though. |
||
219 | * Currently selects the first indexing configuration where the name matches |
||
220 | * the itemType or where the configured tbale is the same as the itemType. |
||
221 | * |
||
222 | * !!! Might return incorrect results for complex configurations !!! |
||
223 | * Try to set the indexingConfiguration directly when using the updateItem() |
||
224 | * method in such situations. |
||
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 string The indexing configuration's name to use when indexing |
||
232 | * @deprecated since 6.1 will be removed in 7.0 |
||
233 | * Use getIndexingConfigurationsByItem() now, which behaves |
||
234 | * almost the same way but returns an array of configurations |
||
235 | */ |
||
236 | protected function getIndexingConfigurationByItem( |
||
251 | |||
252 | /** |
||
253 | * Gets the indexing configurations to use for an item. |
||
254 | * Multiple configurations for a certain item type (table) might be available. |
||
255 | * |
||
256 | * @param string $itemType The item's type, usually a table name. |
||
257 | * @param int $rootPageId The configuration's page tree's root page id. |
||
258 | * Optional, not needed for all types. |
||
259 | * @return array<string> The indexing configurations names to use when indexing |
||
260 | * @deprecated since 6.1 will be removed in 7.0 |
||
261 | */ |
||
262 | protected function getIndexingConfigurationsByItem( |
||
278 | |||
279 | /** |
||
280 | * Marks an item as needing (re)indexing. |
||
281 | * |
||
282 | * Like with Solr itself, there's no add method, just a simple update method |
||
283 | * that handles the adds, too. |
||
284 | * |
||
285 | * The method creates or updates the index queue items for all related rootPageIds. |
||
286 | * |
||
287 | * @param string $itemType The item's type, usually a table name. |
||
288 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
289 | * different value for non-database-record types. |
||
290 | * @param int $forcedChangeTime The change time for the item if set, otherwise |
||
291 | * value from getItemChangedTime() is used. |
||
292 | */ |
||
293 | 49 | public function updateItem($itemType, $itemUid, $forcedChangeTime = 0) |
|
314 | |||
315 | /** |
||
316 | * Finds indexing errors for the current site |
||
317 | * |
||
318 | * @param Site $site |
||
319 | * @return array Error items for the current site's Index Queue |
||
320 | */ |
||
321 | public function getErrorsBySite(Site $site) |
||
329 | |||
330 | /** |
||
331 | * Resets all the errors for all index queue items. |
||
332 | * |
||
333 | * @return mixed |
||
334 | */ |
||
335 | public function resetAllErrors() |
||
343 | |||
344 | /** |
||
345 | * Updates an existing queue entry by $itemType $itemUid and $rootPageId. |
||
346 | * |
||
347 | * @param string $itemType The item's type, usually a table name. |
||
348 | * @param int $itemUid The item's uid, usually an integer uid, could be a |
||
349 | * different value for non-database-record types. |
||
350 | * @param string $indexingConfiguration The name of the related indexConfiguration |
||
351 | * @param int $rootPageId The uid of the rootPage |
||
352 | * @param int $forcedChangeTime The forced change time that should be used for updating |
||
353 | */ |
||
354 | 10 | protected function updateExistingItem($itemType, $itemUid, $indexingConfiguration, $rootPageId, $forcedChangeTime) |
|
371 | |||
372 | /** |
||
373 | * Adds an item to the index queue. |
||
374 | * |
||
375 | * Not meant for public use. |
||
376 | * |
||
377 | * @param string $itemType The item's type, usually a table name. |
||
378 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
379 | * different value for non-database-record types. |
||
380 | * @param string $indexingConfiguration The item's indexing configuration to use. |
||
381 | * Optional, overwrites existing / determined configuration. |
||
382 | * @return void |
||
383 | */ |
||
384 | 42 | private function addNewItem($itemType, $itemUid, $indexingConfiguration, $rootPageId) |
|
409 | |||
410 | /** |
||
411 | * Get record to be added in addNewItem |
||
412 | * |
||
413 | * @param string $itemType The item's type, usually a table name. |
||
414 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
415 | * different value for non-database-record types. |
||
416 | * @param string $additionalRecordFields for sql-query |
||
417 | * |
||
418 | * @return array|NULL |
||
419 | */ |
||
420 | 42 | protected function getRecordCached($itemType, $itemUid, $additionalRecordFields) |
|
433 | |||
434 | /** |
||
435 | * Determines the time for when an item should be indexed. This timestamp |
||
436 | * is then stored in the changed column in the Index Queue. |
||
437 | * |
||
438 | * The changed timestamp usually is now - time(). For records which are set |
||
439 | * to published at a later time, this timestamp is the start time. So if a |
||
440 | * future start time has been set, that will be used to delay indexing |
||
441 | * of an item. |
||
442 | * |
||
443 | * @param string $itemType The item's table name. |
||
444 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
445 | * different value for non-database-record types. |
||
446 | * @return int Timestamp of the item's changed time or future start time |
||
447 | */ |
||
448 | 47 | protected function getItemChangedTime($itemType, $itemUid) |
|
493 | |||
494 | /** |
||
495 | * Gets the most recent changed time of a page's content elements |
||
496 | * |
||
497 | * @param array $page Partial page record |
||
498 | * @return int Timestamp of the most recent content element change |
||
499 | */ |
||
500 | 34 | protected function getPageItemChangedTime(array $page) |
|
516 | |||
517 | /** |
||
518 | * Gets the most recent changed time for an item taking into account |
||
519 | * localized records. |
||
520 | * |
||
521 | * @param string $itemType The item's type, usually a table name. |
||
522 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
523 | * different value for non-database-record types. |
||
524 | * @return int Timestamp of the most recent content element change |
||
525 | */ |
||
526 | 47 | protected function getLocalizableItemChangedTime($itemType, $itemUid) |
|
545 | |||
546 | /** |
||
547 | * Checks whether the Index Queue contains a specific item. |
||
548 | * |
||
549 | * @param string $itemType The item's type, usually a table name. |
||
550 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
551 | * different value for non-database-record types. |
||
552 | * @return bool TRUE if the item is found in the queue, FALSE otherwise |
||
553 | */ |
||
554 | 3 | public function containsItem($itemType, $itemUid) |
|
566 | |||
567 | /** |
||
568 | * Checks whether the Index Queue contains a specific item. |
||
569 | * |
||
570 | * @param string $itemType The item's type, usually a table name. |
||
571 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
572 | * different value for non-database-record types. |
||
573 | * @param integer $rootPageId |
||
574 | * @return bool TRUE if the item is found in the queue, FALSE otherwise |
||
575 | */ |
||
576 | 48 | public function containsItemWithRootPageId($itemType, $itemUid, $rootPageId) |
|
588 | |||
589 | /** |
||
590 | * Checks whether the Index Queue contains a specific item that has been |
||
591 | * marked as indexed. |
||
592 | * |
||
593 | * @param string $itemType The item's type, usually a table name. |
||
594 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
595 | * different value for non-database-record types. |
||
596 | * @return bool TRUE if the item is found in the queue and marked as |
||
597 | * indexed, FALSE otherwise |
||
598 | */ |
||
599 | 3 | public function containsIndexedItem($itemType, $itemUid) |
|
612 | |||
613 | /** |
||
614 | * Removes an item from the Index Queue. |
||
615 | * |
||
616 | * @param string $itemType The type of the item to remove, usually a table name. |
||
617 | * @param int $itemUid The uid of the item to remove |
||
618 | */ |
||
619 | 28 | public function deleteItem($itemType, $itemUid) |
|
647 | |||
648 | /** |
||
649 | * Removes all items of a certain type from the Index Queue. |
||
650 | * |
||
651 | * @param string $itemType The type of items to remove, usually a table name. |
||
652 | */ |
||
653 | 1 | public function deleteItemsByType($itemType) |
|
682 | |||
683 | /** |
||
684 | * Removes all items of a certain site from the Index Queue. Accepts an |
||
685 | * optional parameter to limit the deleted items by indexing configuration. |
||
686 | * |
||
687 | * @param Site $site The site to remove items for. |
||
688 | * @param string $indexingConfigurationName Name of a specific indexing |
||
689 | * configuration |
||
690 | */ |
||
691 | 6 | public function deleteItemsBySite( |
|
741 | |||
742 | /** |
||
743 | * Removes all items from the Index Queue. |
||
744 | * |
||
745 | */ |
||
746 | 1 | public function deleteAllItems() |
|
750 | |||
751 | /** |
||
752 | * Gets a single Index Queue item by its uid. |
||
753 | * |
||
754 | * @param int $itemId Index Queue item uid |
||
755 | * @return Item The request Index Queue item or NULL |
||
756 | * if no item with $itemId was found |
||
757 | */ |
||
758 | 11 | public function getItem($itemId) |
|
778 | |||
779 | /** |
||
780 | * Gets Index Queue items by type and uid. |
||
781 | * |
||
782 | * @param string $itemType item type, usually the table name |
||
783 | * @param int $itemUid item uid |
||
784 | * @return Item[] An array of items matching $itemType and $itemUid |
||
785 | */ |
||
786 | 21 | public function getItems($itemType, $itemUid) |
|
798 | |||
799 | /** |
||
800 | * Gets number of Index Queue items for a specific site / indexing configuration |
||
801 | * optional parameter to limit the counted items by indexing configuration. |
||
802 | * |
||
803 | * @param Site $site The site to search for. |
||
804 | * @param string $indexingConfigurationName name of a specific indexing |
||
805 | * configuration |
||
806 | * @deprecated since 6.1 will be removed in 7.0 use getStatisticsBySite()->getTotalCount() please |
||
807 | * @return int Number of items |
||
808 | */ |
||
809 | public function getItemsCountBySite(Site $site, $indexingConfigurationName = '') |
||
810 | { |
||
811 | GeneralUtility::logDeprecatedFunction(); |
||
812 | return (int) $this->getStatisticsBySite($site, $indexingConfigurationName)->getTotalCount(); |
||
813 | } |
||
814 | |||
815 | /** |
||
816 | * Gets number of unprocessed Index Queue items for a specific site / indexing configuration |
||
817 | * optional parameter to limit the counted items by indexing configuration. |
||
818 | * |
||
819 | * @param Site $site The site to search for. |
||
820 | * @param string $indexingConfigurationName name of a specific indexing |
||
821 | * configuration |
||
822 | * @deprecated since 6.1 will be removed in 7.0 use getStatisticsBySite()->getPendingCount() please |
||
823 | * @return int Number of items. |
||
824 | */ |
||
825 | public function getRemainingItemsCountBySite(Site $site, $indexingConfigurationName = '') |
||
826 | { |
||
827 | GeneralUtility::logDeprecatedFunction(); |
||
828 | return (int) $this->getStatisticsBySite($site, $indexingConfigurationName)->getPendingCount(); |
||
829 | } |
||
830 | |||
831 | /** |
||
832 | * Returns the number of items for all queues. |
||
833 | * |
||
834 | * @return int |
||
835 | */ |
||
836 | 51 | public function getAllItemsCount() |
|
840 | |||
841 | /** |
||
842 | * @param string $where |
||
843 | * @return int |
||
844 | */ |
||
845 | 51 | private function getItemCount($where = '1=1') |
|
852 | |||
853 | /** |
||
854 | * Extracts the number of pending, indexed and erroneous items from the |
||
855 | * Index Queue. |
||
856 | * |
||
857 | * @param Site $site |
||
858 | * @param string $indexingConfigurationName |
||
859 | * |
||
860 | * @return QueueStatistic |
||
861 | */ |
||
862 | 5 | public function getStatisticsBySite(Site $site, $indexingConfigurationName = '') |
|
890 | |||
891 | /** |
||
892 | * Build a database constraint that limits to a certain indexConfigurationName |
||
893 | * |
||
894 | * @param string $indexingConfigurationName |
||
895 | * @return string |
||
896 | */ |
||
897 | 5 | protected function buildIndexConfigurationConstraint($indexingConfigurationName) |
|
906 | |||
907 | /** |
||
908 | * Gets $limit number of items to index for a particular $site. |
||
909 | * |
||
910 | * @param Site $site TYPO3 site |
||
911 | * @param int $limit Number of items to get from the queue |
||
912 | * @return Item[] Items to index to the given solr server |
||
913 | */ |
||
914 | 7 | public function getItemsToIndex(Site $site, $limit = 50) |
|
937 | |||
938 | /** |
||
939 | * Creates an array of ApacheSolrForTypo3\Solr\IndexQueue\Item objects from an array of |
||
940 | * index queue records. |
||
941 | * |
||
942 | * @param array $indexQueueItemRecords Array of plain index queue records |
||
943 | * @return array Array of ApacheSolrForTypo3\Solr\IndexQueue\Item objects |
||
944 | */ |
||
945 | 26 | protected function getIndexQueueItemObjectsFromRecords( |
|
1002 | |||
1003 | /** |
||
1004 | * Marks an item as failed and causes the indexer to skip the item in the |
||
1005 | * next run. |
||
1006 | * |
||
1007 | * @param int|Item $item Either the item's Index Queue uid or the complete item |
||
1008 | * @param string $errorMessage Error message |
||
1009 | */ |
||
1010 | public function markItemAsFailed($item, $errorMessage = '') |
||
1031 | |||
1032 | /** |
||
1033 | * Sets the timestamp of when an item last has been indexed. |
||
1034 | * |
||
1035 | * @param Item $item |
||
1036 | */ |
||
1037 | 5 | public function updateIndexTimeByItem(Item $item) |
|
1045 | } |
||
1046 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.