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 |
||
| 39 | class Item |
||
| 40 | { |
||
| 41 | |||
| 42 | /** |
||
| 43 | * The item's uid in the index queue (tx_solr_indexqueue_item.uid) |
||
| 44 | * |
||
| 45 | * @var int |
||
| 46 | */ |
||
| 47 | protected $indexQueueUid; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * The root page uid of the tree the item is located in (tx_solr_indexqueue_item.root) |
||
| 51 | * |
||
| 52 | * @var int |
||
| 53 | */ |
||
| 54 | protected $rootPageUid; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * The record's type, usually a table name, but could also be a file type (tx_solr_indexqueue_item.item_type) |
||
| 58 | * |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | protected $type; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * The name of the indexing configuration that should be used when indexing (tx_solr_indexqueue_item.indexing_configuration) |
||
| 65 | * the item. |
||
| 66 | * |
||
| 67 | * @var string |
||
| 68 | */ |
||
| 69 | protected $indexingConfigurationName; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * The unix timestamp when the record was last changed (tx_solr_indexqueue_item.changed) |
||
| 73 | * |
||
| 74 | * @var int |
||
| 75 | */ |
||
| 76 | protected $changed; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Indexing properties to provide additional information for the item's |
||
| 80 | * indexer / how to index the item. |
||
| 81 | * |
||
| 82 | * @var array |
||
| 83 | */ |
||
| 84 | protected $indexingProperties = []; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Flag for lazy loading indexing properties. |
||
| 88 | * |
||
| 89 | * @var bool |
||
| 90 | */ |
||
| 91 | protected $indexingPropertiesLoaded = false; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Flag, whether indexing properties exits for this item. |
||
| 95 | * |
||
| 96 | * @var bool |
||
| 97 | */ |
||
| 98 | protected $hasIndexingProperties = false; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * The record's uid. |
||
| 102 | * |
||
| 103 | * @var int |
||
| 104 | */ |
||
| 105 | protected $recordUid = 0; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * The record itself |
||
| 109 | * |
||
| 110 | * @var array |
||
| 111 | */ |
||
| 112 | protected $record; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Moint point identifier. |
||
| 116 | * |
||
| 117 | * @var string |
||
| 118 | */ |
||
| 119 | protected $mountPointIdentifier; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @var string |
||
| 123 | */ |
||
| 124 | protected $errors = ''; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @var IndexQueueIndexingPropertyRepository |
||
| 128 | */ |
||
| 129 | protected $indexQueueIndexingPropertyRepository; |
||
| 130 | |||
| 131 | 64 | /** |
|
| 132 | * Constructor, takes item meta data information and resolves that to the full record. |
||
| 133 | * |
||
| 134 | * @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 |
||
| 135 | 64 | * @param array $fullRecord Optional full record for the item. If provided, can save some SQL queries. |
|
| 136 | 64 | * @param IndexQueueIndexingPropertyRepository|null $indexQueueIndexingPropertyRepository |
|
| 137 | 64 | */ |
|
| 138 | 64 | public function __construct(array $itemMetaData, array $fullRecord = [], IndexQueueIndexingPropertyRepository $indexQueueIndexingPropertyRepository = null) |
|
| 156 | 8 | ||
| 157 | /** |
||
| 158 | * Getter for Index Queue UID |
||
| 159 | * |
||
| 160 | * @return integer |
||
| 161 | */ |
||
| 162 | public function getIndexQueueUid() |
||
| 163 | { |
||
| 164 | 27 | return $this->indexQueueUid; |
|
| 165 | } |
||
| 166 | 27 | ||
| 167 | /** |
||
| 168 | * Gets the item's root page ID (uid) |
||
| 169 | * |
||
| 170 | * @return int root page ID |
||
| 171 | */ |
||
| 172 | public function getRootPageUid() |
||
| 173 | { |
||
| 174 | 21 | return $this->rootPageUid; |
|
| 175 | } |
||
| 176 | 21 | ||
| 177 | /** |
||
| 178 | * Returns mount point identifier |
||
| 179 | * |
||
| 180 | * @return string |
||
| 181 | */ |
||
| 182 | public function getMountPointIdentifier() |
||
| 183 | { |
||
| 184 | return $this->mountPointIdentifier; |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @param integer $uid |
||
| 189 | */ |
||
| 190 | 5 | public function setRootPageUid($uid) |
|
| 191 | { |
||
| 192 | 5 | $this->rootPageUid = intval($uid); |
|
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @return string |
||
| 197 | */ |
||
| 198 | public function getErrors() |
||
| 199 | { |
||
| 200 | 27 | return $this->errors; |
|
| 201 | } |
||
| 202 | 27 | ||
| 203 | 27 | /** |
|
| 204 | * Gets the site the item belongs to. |
||
| 205 | * |
||
| 206 | 23 | * @return Site Site instance the item belongs to. |
|
| 207 | */ |
||
| 208 | 23 | public function getSite() |
|
| 209 | { |
||
| 210 | $siteRepository = GeneralUtility::makeInstance(SiteRepository::class); |
||
| 211 | return $siteRepository->getSiteByRootPageId($this->rootPageUid); |
||
| 212 | } |
||
| 213 | |||
| 214 | public function getType() |
||
| 215 | { |
||
| 216 | 32 | return $this->type; |
|
| 217 | } |
||
| 218 | 32 | ||
| 219 | public function setType($type) |
||
| 220 | { |
||
| 221 | $this->type = $type; |
||
| 222 | } |
||
| 223 | |||
| 224 | public function getIndexingConfigurationName() |
||
| 225 | { |
||
| 226 | return $this->indexingConfigurationName; |
||
| 227 | } |
||
| 228 | |||
| 229 | public function setIndexingConfigurationName($indexingConfigurationName) |
||
| 230 | { |
||
| 231 | $this->indexingConfigurationName = $indexingConfigurationName; |
||
| 232 | } |
||
| 233 | |||
| 234 | public function getChanged() |
||
| 235 | { |
||
| 236 | 5 | return $this->changed; |
|
| 237 | } |
||
| 238 | 5 | ||
| 239 | public function setChanged($changed) |
||
| 240 | 5 | { |
|
| 241 | $this->changed = intval($changed); |
||
| 242 | } |
||
| 243 | |||
| 244 | public function getRecordUid() |
||
| 245 | { |
||
| 246 | $this->getRecord(); |
||
| 247 | |||
| 248 | return $this->record['uid']; |
||
| 249 | } |
||
| 250 | 20 | ||
| 251 | /** |
||
| 252 | 20 | * Gets the item's full record. |
|
| 253 | 1 | * |
|
| 254 | 1 | * Uses lazy loading. |
|
| 255 | 1 | * |
|
| 256 | 1 | * @return array The item's DB record. |
|
| 257 | 1 | */ |
|
| 258 | 1 | public function getRecord() |
|
| 259 | { |
||
| 260 | if (empty($this->record)) { |
||
| 261 | $this->record = (array)BackendUtility::getRecord( |
||
| 262 | 20 | $this->type, |
|
| 263 | $this->recordUid, |
||
| 264 | '*', |
||
| 265 | '', |
||
| 266 | false |
||
| 267 | ); |
||
| 268 | } |
||
| 269 | |||
| 270 | return $this->record; |
||
| 271 | } |
||
| 272 | |||
| 273 | 17 | public function setRecord(array $record) |
|
| 274 | { |
||
| 275 | 17 | $this->record = $record; |
|
| 276 | } |
||
| 277 | 17 | ||
| 278 | /** |
||
| 279 | * @return int |
||
| 280 | */ |
||
| 281 | public function getRecordPageId() |
||
| 282 | { |
||
| 283 | $this->getRecord(); |
||
| 284 | 7 | ||
| 285 | return $this->record['pid']; |
||
| 286 | 7 | } |
|
| 287 | |||
| 288 | 7 | /** |
|
| 289 | 7 | * Stores the indexing properties. |
|
| 290 | * |
||
| 291 | */ |
||
| 292 | 7 | public function storeIndexingProperties() |
|
| 293 | 7 | { |
|
| 294 | $this->indexQueueIndexingPropertyRepository->removeByRootPidAndIndexQueueUid(intval($this->rootPageUid), intval($this->indexQueueUid)); |
||
| 295 | |||
| 296 | if ($this->hasIndexingProperties()) { |
||
| 297 | $this->writeIndexingProperties(); |
||
| 298 | } |
||
| 299 | |||
| 300 | 7 | $this->updateHasIndexingPropertiesFlag(); |
|
| 301 | } |
||
| 302 | 7 | ||
| 303 | 7 | public function hasIndexingProperties() |
|
| 307 | |||
| 308 | 7 | /** |
|
| 309 | * Writes all indexing properties. |
||
| 310 | */ |
||
| 311 | protected function writeIndexingProperties() |
||
| 312 | { |
||
| 313 | $properties = []; |
||
| 314 | 7 | foreach ($this->indexingProperties as $propertyKey => $propertyValue) { |
|
| 315 | $properties[] = [ |
||
| 316 | 7 | 'root' => $this->rootPageUid, |
|
| 317 | 'item_id' => $this->indexQueueUid, |
||
| 318 | 7 | 'property_key' => $propertyKey, |
|
| 319 | 'property_value' => $propertyValue |
||
| 320 | ]; |
||
| 321 | } |
||
| 322 | if (empty($properties)) { |
||
| 323 | return; |
||
| 324 | } |
||
| 327 | |||
| 328 | 7 | /** |
|
| 329 | 7 | * Updates the "has_indexing_properties" flag in the Index Queue. |
|
| 330 | 7 | */ |
|
| 331 | 7 | protected function updateHasIndexingPropertiesFlag() |
|
| 351 | |||
| 352 | /** |
||
| 353 | * @param string $key |
||
| 354 | * @return bool |
||
| 355 | */ |
||
| 356 | public function hasIndexingProperty($key) |
||
| 362 | |||
| 363 | /** |
||
| 364 | 7 | * Loads the indexing properties for the item - if not already loaded. |
|
| 365 | 7 | */ |
|
| 366 | 7 | public function loadIndexingProperties() |
|
| 380 | |||
| 381 | /** |
||
| 382 | 1 | * Sets an indexing property for the item. |
|
| 383 | * |
||
| 384 | 1 | * @param string $key Indexing property name |
|
| 385 | * @param string|int|float $value Indexing property value |
||
| 386 | 1 | * @throws \InvalidArgumentException when $value is not string, integer or float |
|
| 387 | */ |
||
| 388 | public function setIndexingProperty($key, $value) |
||
| 408 | 8 | ||
| 409 | /** |
||
| 410 | 8 | * Gets a specific indexing property by its name/key. |
|
| 411 | * |
||
| 412 | * @param string $key Indexing property name/key. |
||
| 413 | * @throws \InvalidArgumentException when the given $key does not exist. |
||
| 414 | * @return string |
||
| 415 | */ |
||
| 416 | public function getIndexingProperty($key) |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Gets all indexing properties set for this item. |
||
| 432 | * |
||
| 433 | * @return array Array of indexing properties. |
||
| 434 | */ |
||
| 435 | public function getIndexingProperties() |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Gets the names/keys of the item's indexing properties. |
||
| 444 | * |
||
| 445 | * @return array Array of indexing property names/keys |
||
| 446 | */ |
||
| 447 | public function getIndexingPropertyKeys() |
||
| 453 | } |
||
| 454 |