Complex classes like Item 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 Item, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | class Item |
||
| 38 | { |
||
| 39 | |||
| 40 | /** |
||
| 41 | * The item's uid in the index queue (tx_solr_indexqueue_item.uid) |
||
| 42 | * |
||
| 43 | * @var int |
||
| 44 | */ |
||
| 45 | protected $indexQueueUid; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * The root page uid of the tree the item is located in (tx_solr_indexqueue_item.root) |
||
| 49 | * |
||
| 50 | * @var int |
||
| 51 | */ |
||
| 52 | protected $rootPageUid; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * The record's type, usually a table name, but could also be a file type (tx_solr_indexqueue_item.item_type) |
||
| 56 | * |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | protected $type; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * The name of the indexing configuration that should be used when indexing (tx_solr_indexqueue_item.indexing_configuration) |
||
| 63 | * the item. |
||
| 64 | * |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | protected $indexingConfigurationName; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * The unix timestamp when the record was last changed (tx_solr_indexqueue_item.changed) |
||
| 71 | * |
||
| 72 | * @var int |
||
| 73 | */ |
||
| 74 | protected $changed; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Indexing properties to provide additional information for the item's |
||
| 78 | * indexer / how to index the item. |
||
| 79 | * |
||
| 80 | * @var array |
||
| 81 | */ |
||
| 82 | protected $indexingProperties = []; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Flag for lazy loading indexing properties. |
||
| 86 | * |
||
| 87 | * @var bool |
||
| 88 | */ |
||
| 89 | protected $indexingPropertiesLoaded = false; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Flag, whether indexing properties exits for this item. |
||
| 93 | * |
||
| 94 | * @var bool |
||
| 95 | */ |
||
| 96 | protected $hasIndexingProperties = false; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * The record's uid. |
||
| 100 | * |
||
| 101 | * @var int |
||
| 102 | */ |
||
| 103 | protected $recordUid = 0; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * The record itself |
||
| 107 | * |
||
| 108 | * @var array |
||
| 109 | */ |
||
| 110 | protected $record; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @var string |
||
| 114 | */ |
||
| 115 | protected $errors = ''; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Constructor, takes item meta data information and resolves that to the full record. |
||
| 119 | * |
||
| 120 | * @param array $itemMetaData Metadata describing the item to index using the index queue. Is expected to contain a record from table tx_solr_indexqueue_item |
||
| 121 | * @param array $fullRecord Optional full record for the item. If provided, can save some SQL queries. |
||
| 122 | */ |
||
| 123 | 41 | public function __construct( |
|
| 124 | array $itemMetaData, |
||
| 125 | array $fullRecord = [] |
||
| 126 | ) { |
||
| 127 | 41 | $this->indexQueueUid = $itemMetaData['uid']; |
|
| 128 | 41 | $this->rootPageUid = $itemMetaData['root']; |
|
| 129 | 41 | $this->type = $itemMetaData['item_type']; |
|
| 130 | 41 | $this->recordUid = $itemMetaData['item_uid']; |
|
| 131 | 41 | $this->changed = $itemMetaData['changed']; |
|
| 132 | 41 | $this->errors = (string) empty($itemMetaData['errors']) ? '' : $itemMetaData['errors']; |
|
| 133 | |||
| 134 | 41 | $this->indexingConfigurationName = $itemMetaData['indexing_configuration']; |
|
| 135 | 41 | $this->hasIndexingProperties = (boolean)$itemMetaData['has_indexing_properties']; |
|
| 136 | |||
| 137 | 41 | if (!empty($fullRecord)) { |
|
| 138 | 25 | $this->record = $fullRecord; |
|
| 139 | 25 | } |
|
| 140 | 41 | } |
|
| 141 | |||
| 142 | /** |
||
| 143 | * @return int|mixed |
||
| 144 | */ |
||
| 145 | 1 | public function getIndexQueueUid() |
|
| 149 | |||
| 150 | /** |
||
| 151 | * Gets the item's root page ID (uid) |
||
| 152 | * |
||
| 153 | * @return int root page ID |
||
| 154 | */ |
||
| 155 | 18 | public function getRootPageUid() |
|
| 156 | { |
||
| 157 | 18 | return $this->rootPageUid; |
|
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @param integer $uid |
||
| 162 | */ |
||
| 163 | public function setRootPageUid($uid) |
||
| 164 | { |
||
| 165 | $this->rootPageUid = intval($uid); |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @return string |
||
| 170 | */ |
||
| 171 | 1 | public function getErrors() |
|
| 172 | { |
||
| 173 | 1 | return $this->errors; |
|
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Gets the site the item belongs to. |
||
| 178 | * |
||
| 179 | * @return Site Site instance the item belongs to. |
||
| 180 | */ |
||
| 181 | 21 | public function getSite() |
|
| 182 | { |
||
| 183 | 21 | return GeneralUtility::makeInstance(Site::class, $this->rootPageUid); |
|
| 184 | } |
||
| 185 | |||
| 186 | 17 | public function getType() |
|
| 187 | { |
||
| 188 | 17 | return $this->type; |
|
| 189 | } |
||
| 190 | |||
| 191 | public function setType($type) |
||
| 195 | |||
| 196 | 24 | public function getIndexingConfigurationName() |
|
| 197 | { |
||
| 198 | 24 | return $this->indexingConfigurationName; |
|
| 199 | } |
||
| 200 | |||
| 201 | public function setIndexingConfigurationName($indexingConfigurationName) |
||
| 202 | { |
||
| 205 | |||
| 206 | public function getChanged() |
||
| 210 | |||
| 211 | public function setChanged($changed) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Sets the timestamp of when an item has been indexed. |
||
| 218 | * |
||
| 219 | * @return void |
||
| 220 | */ |
||
| 221 | 5 | public function updateIndexedTime() |
|
| 229 | |||
| 230 | 4 | public function getRecordUid() |
|
| 236 | |||
| 237 | /** |
||
| 238 | * Gets the item's full record. |
||
| 239 | * |
||
| 240 | * Uses lazy loading. |
||
| 241 | * |
||
| 242 | * @return array The item's DB record. |
||
| 243 | */ |
||
| 244 | 14 | public function getRecord() |
|
| 258 | |||
| 259 | public function setRecord(array $record) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @return int |
||
| 266 | */ |
||
| 267 | 12 | public function getRecordPageId() |
|
| 273 | |||
| 274 | /** |
||
| 275 | * Stores the indexing properties. |
||
| 276 | * |
||
| 277 | */ |
||
| 278 | 3 | public function storeIndexingProperties() |
|
| 288 | |||
| 289 | /** |
||
| 290 | * Removes existing indexing properties. |
||
| 291 | * |
||
| 292 | * @throws \RuntimeException when an SQL error occurs |
||
| 293 | */ |
||
| 294 | 3 | protected function removeIndexingProperties() |
|
| 309 | |||
| 310 | 3 | public function hasIndexingProperties() |
|
| 314 | |||
| 315 | /** |
||
| 316 | * Writes all indexing properties. |
||
| 317 | * |
||
| 318 | * @throws \RuntimeException when an SQL error occurs |
||
| 319 | */ |
||
| 320 | 3 | protected function writeIndexingProperties() |
|
| 345 | |||
| 346 | /** |
||
| 347 | * Updates the "has_indexing_properties" flag in the Index Queue. |
||
| 348 | * |
||
| 349 | * @throws \RuntimeException when an SQL error occurs |
||
| 350 | */ |
||
| 351 | 3 | protected function updateHasIndexingPropertiesFlag() |
|
| 371 | |||
| 372 | /** |
||
| 373 | * @param string $key |
||
| 374 | * @return bool |
||
| 375 | */ |
||
| 376 | 1 | public function hasIndexingProperty($key) |
|
| 382 | |||
| 383 | /** |
||
| 384 | * Loads the indexing properties for the item - if not already loaded. |
||
| 385 | * |
||
| 386 | */ |
||
| 387 | 4 | public function loadIndexingProperties() |
|
| 405 | |||
| 406 | /** |
||
| 407 | * Sets an indexing property for the item. |
||
| 408 | * |
||
| 409 | * @param string $key Indexing property name |
||
| 410 | * @param string|int|float $value Indexing property value |
||
| 411 | * @throws \InvalidArgumentException when $value is not string, integer or float |
||
| 412 | */ |
||
| 413 | 3 | public function setIndexingProperty($key, $value) |
|
| 433 | |||
| 434 | /** |
||
| 435 | * Gets a specific indexing property by its name/key. |
||
| 436 | * |
||
| 437 | * @param string $key Indexing property name/key. |
||
| 438 | * @throws \InvalidArgumentException when the given $key does not exist. |
||
| 439 | * @return string |
||
| 440 | */ |
||
| 441 | public function getIndexingProperty($key) |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Gets all indexing properties set for this item. |
||
| 457 | * |
||
| 458 | * @return array Array of indexing properties. |
||
| 459 | */ |
||
| 460 | public function getIndexingProperties() |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Gets the names/keys of the item's indexing properties. |
||
| 469 | * |
||
| 470 | * @return array Array of indexing property names/keys |
||
| 471 | */ |
||
| 472 | public function getIndexingPropertyKeys() |
||
| 478 | } |
||
| 479 |