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 |
||
| 42 | class Queue |
||
| 43 | { |
||
| 44 | /** |
||
| 45 | * @var RootPageResolver |
||
| 46 | */ |
||
| 47 | protected $rootPageResolver; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var ConfigurationAwareRecordService |
||
| 51 | */ |
||
| 52 | protected $recordService; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Queue constructor. |
||
| 56 | * @param RootPageResolver|null $rootPageResolver |
||
| 57 | * @param ConfigurationAwareRecordService|null $recordService |
||
| 58 | */ |
||
| 59 | 67 | public function __construct(RootPageResolver $rootPageResolver = null, ConfigurationAwareRecordService $recordService = null) |
|
| 64 | |||
| 65 | // FIXME some of the methods should be renamed to plural forms |
||
| 66 | // FIXME singular form methods should deal with exactly one item only |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Returns the timestamp of the last indexing run. |
||
| 70 | * |
||
| 71 | * @param int $rootPageId The root page uid for which to get |
||
| 72 | * the last indexed item id |
||
| 73 | * @return int Timestamp of last index run. |
||
| 74 | */ |
||
| 75 | public function getLastIndexTime($rootPageId) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Returns the uid of the last indexed item in the queue |
||
| 97 | * |
||
| 98 | * @param int $rootPageId The root page uid for which to get |
||
| 99 | * the last indexed item id |
||
| 100 | * @return int The last indexed item's ID. |
||
| 101 | */ |
||
| 102 | public function getLastIndexedItemId($rootPageId) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Truncate and rebuild the tx_solr_indexqueue_item table. This is the most |
||
| 123 | * complete way to force reindexing, or to build the Index Queue for the |
||
| 124 | * first time. The Index Queue initialization is site-specific. |
||
| 125 | * |
||
| 126 | * @param Site $site The site to initialize |
||
| 127 | * @param string $indexingConfigurationName Name of a specific |
||
| 128 | * indexing configuration |
||
| 129 | * @return array An array of booleans, each representing whether the |
||
| 130 | * initialization for an indexing configuration was successful |
||
| 131 | */ |
||
| 132 | 5 | public function initialize(Site $site, $indexingConfigurationName = '') |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Initializes the Index Queue for a specific indexing configuration. |
||
| 176 | * |
||
| 177 | * @param Site $site The site to initialize |
||
| 178 | * @param string $indexingConfigurationName name of a specific |
||
| 179 | * indexing configuration |
||
| 180 | * @return bool TRUE if the initialization was successful, FALSE otherwise |
||
| 181 | */ |
||
| 182 | 5 | protected function initializeIndexingConfiguration( |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Gets the indexing configuration to use for an item. |
||
| 208 | * Sometimes, when there are multiple configurations for a certain item type |
||
| 209 | * (table) it can be hard or even impossible to find which one to use |
||
| 210 | * though. |
||
| 211 | * Currently selects the first indexing configuration where the name matches |
||
| 212 | * the itemType or where the configured tbale is the same as the itemType. |
||
| 213 | * |
||
| 214 | * !!! Might return incorrect results for complex configurations !!! |
||
| 215 | * Try to set the indexingConfiguration directly when using the updateItem() |
||
| 216 | * method in such situations. |
||
| 217 | * |
||
| 218 | * @param string $itemType The item's type, usually a table name. |
||
| 219 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
| 220 | * different value for non-database-record types. |
||
| 221 | * @param int $rootPageId The configuration's page tree's root page id. |
||
| 222 | * Optional, not needed for all types. |
||
| 223 | * @return string The indexing configuration's name to use when indexing |
||
| 224 | * @deprecated since 6.1 will be removed in 7.0 |
||
| 225 | * Use getIndexingConfigurationsByItem() now, which behaves |
||
| 226 | * almost the same way but returns an array of configurations |
||
| 227 | */ |
||
| 228 | protected function getIndexingConfigurationByItem( |
||
| 229 | $itemType, |
||
| 230 | $itemUid, |
||
|
|
|||
| 231 | $rootPageId = null |
||
| 232 | ) { |
||
| 233 | GeneralUtility::logDeprecatedFunction(); |
||
| 234 | $indexingConfigurationName = ''; |
||
| 235 | |||
| 236 | $configurations = $this->getIndexingConfigurationsByItem($itemType, $rootPageId); |
||
| 237 | if (count($configurations) > 0) { |
||
| 238 | $indexingConfigurationName = $configurations[0]; |
||
| 239 | } |
||
| 240 | |||
| 241 | return $indexingConfigurationName; |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Gets the indexing configurations to use for an item. |
||
| 246 | * Multiple configurations for a certain item type (table) might be available. |
||
| 247 | * |
||
| 248 | * @param string $itemType The item's type, usually a table name. |
||
| 249 | * @param int $rootPageId The configuration's page tree's root page id. |
||
| 250 | * Optional, not needed for all types. |
||
| 251 | * @return array<string> The indexing configurations names to use when indexing |
||
| 252 | * @deprecated since 6.1 will be removed in 7.0 |
||
| 253 | */ |
||
| 254 | protected function getIndexingConfigurationsByItem( |
||
| 255 | $itemType, |
||
| 256 | $rootPageId = null |
||
| 257 | ) { |
||
| 258 | GeneralUtility::logDeprecatedFunction(); |
||
| 259 | |||
| 260 | $possibleIndexingConfigurationNames = []; |
||
| 261 | |||
| 262 | if (!is_null($rootPageId)) { |
||
| 263 | // get configuration for the root's branch |
||
| 264 | $solrConfiguration = Util::getSolrConfigurationFromPageId($rootPageId); |
||
| 265 | $possibleIndexingConfigurationNames = $solrConfiguration->getIndexQueueConfigurationNamesByTableName($itemType); |
||
| 266 | } |
||
| 267 | |||
| 268 | return $possibleIndexingConfigurationNames; |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Marks an item as needing (re)indexing. |
||
| 273 | * |
||
| 274 | * Like with Solr itself, there's no add method, just a simple update method |
||
| 275 | * that handles the adds, too. |
||
| 276 | * |
||
| 277 | * The method creates or updates the index queue items for all related rootPageIds. |
||
| 278 | * |
||
| 279 | * @param string $itemType The item's type, usually a table name. |
||
| 280 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
| 281 | * different value for non-database-record types. |
||
| 282 | * @param int $forcedChangeTime The change time for the item if set, otherwise |
||
| 283 | * value from getItemChangedTime() is used. |
||
| 284 | */ |
||
| 285 | 47 | public function updateItem($itemType, $itemUid, $forcedChangeTime = 0) |
|
| 306 | |||
| 307 | /** |
||
| 308 | * Updates an existing queue entry by $itemType $itemUid and $rootPageId. |
||
| 309 | * |
||
| 310 | * @param string $itemType The item's type, usually a table name. |
||
| 311 | * @param int $itemUid The item's uid, usually an integer uid, could be a |
||
| 312 | * different value for non-database-record types. |
||
| 313 | * @param string $indexingConfiguration The name of the related indexConfiguration |
||
| 314 | * @param int $rootPageId The uid of the rootPage |
||
| 315 | * @param int $forcedChangeTime The forced change time that should be used for updating |
||
| 316 | */ |
||
| 317 | 10 | protected function updateExistingItem($itemType, $itemUid, $indexingConfiguration, $rootPageId, $forcedChangeTime) |
|
| 334 | |||
| 335 | /** |
||
| 336 | * Adds an item to the index queue. |
||
| 337 | * |
||
| 338 | * Not meant for public use. |
||
| 339 | * |
||
| 340 | * @param string $itemType The item's type, usually a table name. |
||
| 341 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
| 342 | * different value for non-database-record types. |
||
| 343 | * @param string $indexingConfiguration The item's indexing configuration to use. |
||
| 344 | * Optional, overwrites existing / determined configuration. |
||
| 345 | * @return void |
||
| 346 | */ |
||
| 347 | 40 | private function addNewItem($itemType, $itemUid, $indexingConfiguration, $rootPageId) |
|
| 372 | |||
| 373 | /** |
||
| 374 | * Get record to be added in addNewItem |
||
| 375 | * |
||
| 376 | * @param string $itemType The item's type, usually a table name. |
||
| 377 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
| 378 | * different value for non-database-record types. |
||
| 379 | * @param string $additionalRecordFields for sql-query |
||
| 380 | * |
||
| 381 | * @return array|NULL |
||
| 382 | */ |
||
| 383 | 40 | protected function getRecordCached($itemType, $itemUid, $additionalRecordFields) |
|
| 396 | |||
| 397 | /** |
||
| 398 | * Determines the time for when an item should be indexed. This timestamp |
||
| 399 | * is then stored in the changed column in the Index Queue. |
||
| 400 | * |
||
| 401 | * The changed timestamp usually is now - time(). For records which are set |
||
| 402 | * to published at a later time, this timestamp is the start time. So if a |
||
| 403 | * future start time has been set, that will be used to delay indexing |
||
| 404 | * of an item. |
||
| 405 | * |
||
| 406 | * @param string $itemType The item's table name. |
||
| 407 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
| 408 | * different value for non-database-record types. |
||
| 409 | * @return int Timestamp of the item's changed time or future start time |
||
| 410 | */ |
||
| 411 | 45 | protected function getItemChangedTime($itemType, $itemUid) |
|
| 456 | |||
| 457 | /** |
||
| 458 | * Gets the most recent changed time of a page's content elements |
||
| 459 | * |
||
| 460 | * @param array $page Partial page record |
||
| 461 | * @return int Timestamp of the most recent content element change |
||
| 462 | */ |
||
| 463 | 32 | protected function getPageItemChangedTime(array $page) |
|
| 479 | |||
| 480 | /** |
||
| 481 | * Gets the most recent changed time for an item taking into account |
||
| 482 | * localized records. |
||
| 483 | * |
||
| 484 | * @param string $itemType The item's type, usually a table name. |
||
| 485 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
| 486 | * different value for non-database-record types. |
||
| 487 | * @return int Timestamp of the most recent content element change |
||
| 488 | */ |
||
| 489 | 45 | protected function getLocalizableItemChangedTime($itemType, $itemUid) |
|
| 508 | |||
| 509 | /** |
||
| 510 | * Checks whether the Index Queue contains a specific item. |
||
| 511 | * |
||
| 512 | * @param string $itemType The item's type, usually a table name. |
||
| 513 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
| 514 | * different value for non-database-record types. |
||
| 515 | * @return bool TRUE if the item is found in the queue, FALSE otherwise |
||
| 516 | */ |
||
| 517 | 3 | public function containsItem($itemType, $itemUid) |
|
| 529 | |||
| 530 | /** |
||
| 531 | * Checks whether the Index Queue contains a specific item. |
||
| 532 | * |
||
| 533 | * @param string $itemType The item's type, usually a table name. |
||
| 534 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
| 535 | * different value for non-database-record types. |
||
| 536 | * @param integer $rootPageId |
||
| 537 | * @return bool TRUE if the item is found in the queue, FALSE otherwise |
||
| 538 | */ |
||
| 539 | 46 | public function containsItemWithRootPageId($itemType, $itemUid, $rootPageId) |
|
| 551 | |||
| 552 | /** |
||
| 553 | * Checks whether the Index Queue contains a specific item that has been |
||
| 554 | * marked as indexed. |
||
| 555 | * |
||
| 556 | * @param string $itemType The item's type, usually a table name. |
||
| 557 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
| 558 | * different value for non-database-record types. |
||
| 559 | * @return bool TRUE if the item is found in the queue and marked as |
||
| 560 | * indexed, FALSE otherwise |
||
| 561 | */ |
||
| 562 | 3 | public function containsIndexedItem($itemType, $itemUid) |
|
| 575 | |||
| 576 | /** |
||
| 577 | * Removes an item from the Index Queue. |
||
| 578 | * |
||
| 579 | * @param string $itemType The type of the item to remove, usually a table name. |
||
| 580 | * @param int $itemUid The uid of the item to remove |
||
| 581 | */ |
||
| 582 | 26 | public function deleteItem($itemType, $itemUid) |
|
| 610 | |||
| 611 | /** |
||
| 612 | * Removes all items of a certain type from the Index Queue. |
||
| 613 | * |
||
| 614 | * @param string $itemType The type of items to remove, usually a table name. |
||
| 615 | */ |
||
| 616 | 1 | public function deleteItemsByType($itemType) |
|
| 645 | |||
| 646 | /** |
||
| 647 | * Removes all items of a certain site from the Index Queue. Accepts an |
||
| 648 | * optional parameter to limit the deleted items by indexing configuration. |
||
| 649 | * |
||
| 650 | * @param Site $site The site to remove items for. |
||
| 651 | * @param string $indexingConfigurationName Name of a specific indexing |
||
| 652 | * configuration |
||
| 653 | */ |
||
| 654 | 5 | public function deleteItemsBySite( |
|
| 704 | |||
| 705 | /** |
||
| 706 | * Removes all items from the Index Queue. |
||
| 707 | * |
||
| 708 | */ |
||
| 709 | public function deleteAllItems() |
||
| 713 | |||
| 714 | /** |
||
| 715 | * Gets a single Index Queue item by its uid. |
||
| 716 | * |
||
| 717 | * @param int $itemId Index Queue item uid |
||
| 718 | * @return Item The request Index Queue item or NULL |
||
| 719 | * if no item with $itemId was found |
||
| 720 | */ |
||
| 721 | 11 | public function getItem($itemId) |
|
| 742 | |||
| 743 | /** |
||
| 744 | * Gets Index Queue items by type and uid. |
||
| 745 | * |
||
| 746 | * @param string $itemType item type, usually the table name |
||
| 747 | * @param int $itemUid item uid |
||
| 748 | * @return Item[] An array of items matching $itemType and $itemUid |
||
| 749 | */ |
||
| 750 | 20 | public function getItems($itemType, $itemUid) |
|
| 762 | |||
| 763 | /** |
||
| 764 | * Gets number of Index Queue items for a specific site / indexing configuration |
||
| 765 | * optional parameter to limit the counted items by indexing configuration. |
||
| 766 | * |
||
| 767 | * @param Site $site The site to search for. |
||
| 768 | * @param string $indexingConfigurationName name of a specific indexing |
||
| 769 | * configuration |
||
| 770 | * @return int Number of items |
||
| 771 | */ |
||
| 772 | 2 | public function getItemsCountBySite(Site $site, $indexingConfigurationName = '') |
|
| 778 | |||
| 779 | /** |
||
| 780 | * Gets number of unprocessed Index Queue items for a specific site / indexing configuration |
||
| 781 | * optional parameter to limit the counted items by indexing configuration. |
||
| 782 | * |
||
| 783 | * @param Site $site The site to search for. |
||
| 784 | * @param string $indexingConfigurationName name of a specific indexing |
||
| 785 | * configuration |
||
| 786 | * @return int Number of items. |
||
| 787 | */ |
||
| 788 | 2 | public function getRemainingItemsCountBySite(Site $site, $indexingConfigurationName = '') |
|
| 794 | |||
| 795 | /** |
||
| 796 | * Returns the number of items for all queues. |
||
| 797 | * |
||
| 798 | * @return int |
||
| 799 | */ |
||
| 800 | 46 | public function getAllItemsCount() |
|
| 804 | |||
| 805 | /** |
||
| 806 | * @param string $where |
||
| 807 | * @return int |
||
| 808 | */ |
||
| 809 | 49 | private function getItemCount($where = '1=1') |
|
| 816 | |||
| 817 | /** |
||
| 818 | * Build a database constraint that limits to a certain indexConfigurationName |
||
| 819 | * |
||
| 820 | * @param string $indexingConfigurationName |
||
| 821 | * @return string |
||
| 822 | */ |
||
| 823 | 3 | protected function buildIndexConfigurationConstraint($indexingConfigurationName) |
|
| 832 | |||
| 833 | /** |
||
| 834 | * Gets $limit number of items to index for a particular $site. |
||
| 835 | * |
||
| 836 | * @param Site $site TYPO3 site |
||
| 837 | * @param int $limit Number of items to get from the queue |
||
| 838 | * @return Item[] Items to index to the given solr server |
||
| 839 | */ |
||
| 840 | 7 | public function getItemsToIndex(Site $site, $limit = 50) |
|
| 863 | |||
| 864 | /** |
||
| 865 | * Creates an array of ApacheSolrForTypo3\Solr\IndexQueue\Item objects from an array of |
||
| 866 | * index queue records. |
||
| 867 | * |
||
| 868 | * @param array $indexQueueItemRecords Array of plain index queue records |
||
| 869 | * @return array Array of ApacheSolrForTypo3\Solr\IndexQueue\Item objects |
||
| 870 | */ |
||
| 871 | 25 | protected function getIndexQueueItemObjectsFromRecords( |
|
| 923 | |||
| 924 | /** |
||
| 925 | * Marks an item as failed and causes the indexer to skip the item in the |
||
| 926 | * next run. |
||
| 927 | * |
||
| 928 | * @param int|Item $item Either the item's Index Queue |
||
| 929 | * uid or the complete item |
||
| 930 | * @param string $errorMessage Error message |
||
| 931 | */ |
||
| 932 | public function markItemAsFailed($item, $errorMessage = '') |
||
| 953 | } |
||
| 954 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.