Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
| 41 | class Queue |
||
| 42 | { |
||
| 43 | |||
| 44 | // FIXME some of the methods should be renamed to plural forms |
||
| 45 | // FIXME singular form methods should deal with exactly one item only |
||
| 46 | |||
| 47 | |||
| 48 | /** |
||
| 49 | * Returns the timestamp of the last indexing run. |
||
| 50 | * |
||
| 51 | * @param integer $rootPageId The root page uid for which to get |
||
| 52 | * the last indexed item id |
||
| 53 | * @return integer Timestamp of last index run. |
||
| 54 | */ |
||
| 55 | public function getLastIndexTime($rootPageId) |
||
|
1 ignored issue
–
show
|
|||
| 56 | { |
||
| 57 | $lastIndexTime = 0; |
||
| 58 | |||
| 59 | $lastIndexedRow = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows( |
||
| 60 | 'indexed', |
||
| 61 | 'tx_solr_indexqueue_item', |
||
| 62 | 'root = ' . (int)$rootPageId, |
||
| 63 | '', |
||
| 64 | 'indexed DESC', |
||
| 65 | 1 |
||
| 66 | ); |
||
| 67 | |||
| 68 | if ($lastIndexedRow[0]['indexed']) { |
||
| 69 | $lastIndexTime = $lastIndexedRow[0]['indexed']; |
||
| 70 | } |
||
| 71 | |||
| 72 | return $lastIndexTime; |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Returns the uid of the last indexed item in the queue |
||
| 77 | * |
||
| 78 | * @param integer $rootPageId The root page uid for which to get |
||
| 79 | * the last indexed item id |
||
| 80 | * @return integer The last indexed item's ID. |
||
| 81 | */ |
||
| 82 | public function getLastIndexedItemId($rootPageId) |
||
|
1 ignored issue
–
show
|
|||
| 83 | { |
||
| 84 | $lastIndexedItemId = 0; |
||
| 85 | |||
| 86 | $lastIndexedItemRow = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows( |
||
| 87 | 'uid', |
||
| 88 | 'tx_solr_indexqueue_item', |
||
| 89 | 'root = ' . (int)$rootPageId, |
||
| 90 | '', |
||
| 91 | 'indexed DESC', |
||
| 92 | 1 |
||
| 93 | ); |
||
| 94 | if ($lastIndexedItemRow[0]['uid']) { |
||
| 95 | $lastIndexedItemId = $lastIndexedItemRow[0]['uid']; |
||
| 96 | } |
||
| 97 | |||
| 98 | return $lastIndexedItemId; |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Truncate and rebuild the tx_solr_indexqueue_item table. This is the most |
||
| 103 | * complete way to force reindexing, or to build the Index Queue for the |
||
| 104 | * first time. The Index Queue initialization is site-specific. |
||
| 105 | * |
||
| 106 | * @param Site $site The site to initialize |
||
| 107 | * @param string $indexingConfigurationName Name of a specific |
||
| 108 | * indexing configuration |
||
| 109 | * @return array An array of booleans, each representing whether the |
||
| 110 | * initialization for an indexing configuration was successful |
||
| 111 | */ |
||
| 112 | public function initialize(Site $site, $indexingConfigurationName = '') |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Initializes the Index Queue for a specific indexing configuration. |
||
| 156 | * |
||
| 157 | * @param Site $site The site to initialize |
||
| 158 | * @param string $indexingConfigurationName name of a specific |
||
| 159 | * indexing configuration |
||
| 160 | * @return boolean TRUE if the initialization was successful, FALSE otherwise |
||
| 161 | */ |
||
| 162 | protected function initializeIndexingConfiguration( |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Gets the indexing configuration to use for an item. |
||
| 188 | * Sometimes, when there are multiple configurations for a certain item type |
||
| 189 | * (table) it can be hard or even impossible to find which one to use |
||
| 190 | * though. |
||
| 191 | * Currently selects the first indexing configuration where the name matches |
||
| 192 | * the itemType or where the configured tbale is the same as the itemType. |
||
| 193 | * |
||
| 194 | * !!! Might return incorrect results for complex configurations !!! |
||
| 195 | * Try to set the indexingConfiguration directly when using the updateItem() |
||
| 196 | * method in such situations. |
||
| 197 | * |
||
| 198 | * @param string $itemType The item's type, usually a table name. |
||
| 199 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
| 200 | * different value for non-database-record types. |
||
| 201 | * @param integer $rootPageId The configuration's page tree's root page id. |
||
| 202 | * Optional, not needed for all types. |
||
| 203 | * @return string The indexing configuration's name to use when indexing |
||
| 204 | * @deprecated Use getIndexingConfigurationsByItem() now, which behaves |
||
| 205 | * almost the same way but returns an array of configurations |
||
| 206 | */ |
||
| 207 | protected function getIndexingConfigurationByItem( |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Gets the indexing configurations to use for an item. |
||
| 225 | * Multiple configurations for a certain item type (table) might be available. |
||
| 226 | * |
||
| 227 | * @param string $itemType The item's type, usually a table name. |
||
| 228 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
| 229 | * different value for non-database-record types. |
||
| 230 | * @param integer $rootPageId The configuration's page tree's root page id. |
||
| 231 | * Optional, not needed for all types. |
||
| 232 | * @return array<string> The indexing configurations names to use when indexing |
||
| 233 | */ |
||
| 234 | protected function getIndexingConfigurationsByItem( |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Marks an item as needing (re)indexing. |
||
| 252 | * |
||
| 253 | * Like with Solr itself, there's no add method, just a simple update method |
||
| 254 | * that handles the adds, too. |
||
| 255 | * |
||
| 256 | * @param string $itemType The item's type, usually a table name. |
||
| 257 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
| 258 | * different value for non-database-record types. |
||
| 259 | * @param string $indexingConfiguration The item's indexing configuration to use. |
||
| 260 | * Optional, overwrites existing / determined configuration. |
||
| 261 | * @param int $forcedChangeTime The change time for the item if set, otherwise |
||
| 262 | * value from getItemChangedTime() is used. |
||
| 263 | */ |
||
| 264 | public function updateItem( |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Adds an item to the index queue. |
||
| 299 | * |
||
| 300 | * Not meant for public use. |
||
| 301 | * |
||
| 302 | * @param string $itemType The item's type, usually a table name. |
||
| 303 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
| 304 | * different value for non-database-record types. |
||
| 305 | * @param string $indexingConfiguration The item's indexing configuration to use. |
||
| 306 | * Optional, overwrites existing / determined configuration. |
||
| 307 | * @return void |
||
| 308 | */ |
||
| 309 | private function addItem($itemType, $itemUid, $indexingConfiguration) |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Determines the time for when an item should be indexed. This timestamp |
||
| 383 | * is then stored in the changed column in the Index Queue. |
||
| 384 | * |
||
| 385 | * The changed timestamp usually is now - time(). For records which are set |
||
| 386 | * to published at a later time, this timestamp is the start time. So if a |
||
| 387 | * future start time has been set, that will be used to delay indexing |
||
| 388 | * of an item. |
||
| 389 | * |
||
| 390 | * @param string $itemType The item's table name. |
||
| 391 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
| 392 | * different value for non-database-record types. |
||
| 393 | * @return integer Timestamp of the item's changed time or future start time |
||
| 394 | */ |
||
| 395 | protected function getItemChangedTime($itemType, $itemUid) |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Gets the most recent changed time of a page's content elements |
||
| 445 | * |
||
| 446 | * @param array $page Partial page record |
||
| 447 | * @return integer Timestamp of the most recent content element change |
||
| 448 | */ |
||
| 449 | protected function getPageItemChangedTime(array $page) |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Gets the most recent changed time for an item taking into account |
||
| 468 | * localized records. |
||
| 469 | * |
||
| 470 | * @param string $itemType The item's type, usually a table name. |
||
| 471 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
| 472 | * different value for non-database-record types. |
||
| 473 | * @return integer Timestamp of the most recent content element change |
||
| 474 | */ |
||
| 475 | protected function getLocalizableItemChangedTime($itemType, $itemUid) |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Checks whether the Index Queue contains a specific item. |
||
| 497 | * |
||
| 498 | * @param string $itemType The item's type, usually a table name. |
||
| 499 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
| 500 | * different value for non-database-record types. |
||
| 501 | * @return boolean TRUE if the item is found in the queue, FALSE otherwise |
||
| 502 | */ |
||
| 503 | View Code Duplication | public function containsItem($itemType, $itemUid) |
|
| 515 | |||
| 516 | /** |
||
| 517 | * Checks whether the Index Queue contains a specific item that has been |
||
| 518 | * marked as indexed. |
||
| 519 | * |
||
| 520 | * @param string $itemType The item's type, usually a table name. |
||
| 521 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
| 522 | * different value for non-database-record types. |
||
| 523 | * @return boolean TRUE if the item is found in the queue and marked as |
||
| 524 | * indexed, FALSE otherwise |
||
| 525 | */ |
||
| 526 | View Code Duplication | public function containsIndexedItem($itemType, $itemUid) |
|
| 539 | |||
| 540 | /** |
||
| 541 | * Removes an item from the Index Queue. |
||
| 542 | * |
||
| 543 | * @param string $itemType The type of the item to remove, usually a table name. |
||
| 544 | * @param integer $itemUid The uid of the item to remove |
||
| 545 | */ |
||
| 546 | View Code Duplication | public function deleteItem($itemType, $itemUid) |
|
| 574 | |||
| 575 | /** |
||
| 576 | * Removes all items of a certain type from the Index Queue. |
||
| 577 | * |
||
| 578 | * @param string $itemType The type of items to remove, usually a table name. |
||
| 579 | */ |
||
| 580 | View Code Duplication | public function deleteItemsByType($itemType) |
|
| 609 | |||
| 610 | /** |
||
| 611 | * Removes all items of a certain site from the Index Queue. Accepts an |
||
| 612 | * optional parameter to limit the deleted items by indexing configuration. |
||
| 613 | * |
||
| 614 | * @param Site $site The site to remove items for. |
||
| 615 | * @param string $indexingConfigurationName Name of a specific indexing |
||
| 616 | * configuration |
||
| 617 | */ |
||
| 618 | public function deleteItemsBySite( |
||
| 668 | |||
| 669 | /** |
||
| 670 | * Removes all items from the Index Queue. |
||
| 671 | * |
||
| 672 | */ |
||
| 673 | public function deleteAllItems() |
||
| 677 | |||
| 678 | /** |
||
| 679 | * Gets a single Index Queue item by its uid. |
||
| 680 | * |
||
| 681 | * @param integer $itemId Index Queue item uid |
||
| 682 | * @return Item The request Index Queue item or NULL |
||
| 683 | * if no item with $itemId was found |
||
| 684 | */ |
||
| 685 | public function getItem($itemId) |
||
| 706 | |||
| 707 | /** |
||
| 708 | * Gets Index Queue items by type and uid. |
||
| 709 | * |
||
| 710 | * @param string $itemType item type, ususally the table name |
||
| 711 | * @param integer $itemUid item uid |
||
| 712 | * @return array An array of items matching $itemType and $itemUid |
||
| 713 | */ |
||
| 714 | public function getItems($itemType, $itemUid) |
||
| 726 | |||
| 727 | /** |
||
| 728 | * Gets number of Index Queue items for a specific site / indexing configuration |
||
| 729 | * optional parameter to limit the deleted items by indexing configuration. |
||
| 730 | * |
||
| 731 | * @param Site $site The site to search for. |
||
| 732 | * @param string $indexingConfigurationName name of a specific indexing |
||
| 733 | * configuration |
||
| 734 | * @return mixed Number of items (integer) or FALSE if something went |
||
| 735 | * wrong (boolean) |
||
| 736 | */ |
||
| 737 | public function getItemsCountBySite( |
||
| 754 | |||
| 755 | /** |
||
| 756 | * Returns the number of items for all queues. |
||
| 757 | * |
||
| 758 | * @return integer |
||
| 759 | */ |
||
| 760 | public function getAllItemsCount() |
||
| 771 | |||
| 772 | /** |
||
| 773 | * Gets $limit number of items to index for a particular $site. |
||
| 774 | * |
||
| 775 | * @param Site $site TYPO3 site |
||
| 776 | * @param integer $limit Number of items to get from the queue |
||
| 777 | * @return Item[] Items to index to the given solr server |
||
| 778 | */ |
||
| 779 | public function getItemsToIndex(Site $site, $limit = 50) |
||
| 802 | |||
| 803 | /** |
||
| 804 | * Creates an array of ApacheSolrForTypo3\Solr\IndexQueue\Item objects from an array of |
||
| 805 | * index queue records. |
||
| 806 | * |
||
| 807 | * @param array $indexQueueItemRecords Array of plain index queue records |
||
| 808 | * @return array Array of ApacheSolrForTypo3\Solr\IndexQueue\Item objects |
||
| 809 | */ |
||
| 810 | protected function getIndexQueueItemObjectsFromRecords( |
||
|
1 ignored issue
–
show
|
|||
| 811 | array $indexQueueItemRecords |
||
| 812 | ) { |
||
| 813 | $indexQueueItems = array(); |
||
| 814 | $tableUids = array(); |
||
| 815 | $tableRecords = array(); |
||
| 816 | |||
| 817 | // grouping records by table |
||
| 818 | foreach ($indexQueueItemRecords as $indexQueueItemRecord) { |
||
| 819 | $tableUids[$indexQueueItemRecord['item_type']][] = $indexQueueItemRecord['item_uid']; |
||
| 820 | } |
||
| 821 | |||
| 822 | // fetching records by table, saves us a lot of single queries |
||
| 823 | foreach ($tableUids as $table => $uids) { |
||
| 824 | $uidList = implode(',', $uids); |
||
| 825 | $records = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows( |
||
| 826 | '*', |
||
| 827 | $table, |
||
| 828 | 'uid IN(' . $uidList . ')', |
||
| 829 | '', '', '', // group, order, limit |
||
| 830 | 'uid' |
||
| 831 | ); |
||
| 832 | $tableRecords[$table] = $records; |
||
| 833 | |||
| 834 | if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['postProcessFetchRecordsForIndexQueueItem'])) { |
||
| 835 | $params = ['table' => $table, 'uids' => $uids, 'tableRecords' => &$tableRecords]; |
||
| 836 | foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['postProcessFetchRecordsForIndexQueueItem'] as $reference) { |
||
| 837 | GeneralUtility::callUserFunction($reference, $params, $this); |
||
| 838 | } |
||
| 839 | unset($params); |
||
| 840 | } |
||
| 841 | } |
||
| 842 | |||
| 843 | // creating index queue item objects and assigning / mapping |
||
| 844 | // records to index queue items |
||
| 845 | foreach ($indexQueueItemRecords as $indexQueueItemRecord) { |
||
| 846 | if (isset($tableRecords[$indexQueueItemRecord['item_type']][$indexQueueItemRecord['item_uid']])) { |
||
| 847 | $indexQueueItems[] = GeneralUtility::makeInstance( |
||
| 848 | 'ApacheSolrForTypo3\\Solr\\IndexQueue\\Item', |
||
| 849 | $indexQueueItemRecord, |
||
| 850 | $tableRecords[$indexQueueItemRecord['item_type']][$indexQueueItemRecord['item_uid']] |
||
| 851 | ); |
||
| 852 | } else { |
||
| 853 | GeneralUtility::devLog('Record missing for Index Queue item. Item removed.', |
||
| 854 | 'solr', 3, array($indexQueueItemRecord)); |
||
| 855 | $this->deleteItem($indexQueueItemRecord['item_type'], |
||
| 856 | $indexQueueItemRecord['item_uid']); |
||
| 857 | } |
||
| 858 | } |
||
| 859 | |||
| 860 | return $indexQueueItems; |
||
| 861 | } |
||
| 862 | |||
| 863 | /** |
||
| 864 | * Marks an item as failed and causes the indexer to skip the item in the |
||
| 865 | * next run. |
||
| 866 | * |
||
| 867 | * @param int|Item $item Either the item's Index Queue |
||
| 868 | * uid or the complete item |
||
| 869 | * @param string $errorMessage Error message |
||
| 870 | */ |
||
| 871 | public function markItemAsFailed($item, $errorMessage = '') |
||
| 894 | } |
||
| 895 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: