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 |
||
| 38 | class Item |
||
| 39 | { |
||
| 40 | |||
| 41 | /** |
||
| 42 | * The item's uid in the index queue (tx_solr_indexqueue_item.uid) |
||
| 43 | * |
||
| 44 | * @var int |
||
| 45 | */ |
||
| 46 | protected $indexQueueUid; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * The root page uid of the tree the item is located in (tx_solr_indexqueue_item.root) |
||
| 50 | * |
||
| 51 | * @var int |
||
| 52 | */ |
||
| 53 | protected $rootPageUid; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * The record's type, usually a table name, but could also be a file type (tx_solr_indexqueue_item.item_type) |
||
| 57 | * |
||
| 58 | * @var string |
||
| 59 | */ |
||
| 60 | protected $type; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * The name of the indexing configuration that should be used when indexing (tx_solr_indexqueue_item.indexing_configuration) |
||
| 64 | * the item. |
||
| 65 | * |
||
| 66 | * @var string |
||
| 67 | */ |
||
| 68 | protected $indexingConfigurationName; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * The unix timestamp when the record was last changed (tx_solr_indexqueue_item.changed) |
||
| 72 | * |
||
| 73 | * @var int |
||
| 74 | */ |
||
| 75 | protected $changed; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Indexing properties to provide additional information for the item's |
||
| 79 | * indexer / how to index the item. |
||
| 80 | * |
||
| 81 | * @var array |
||
| 82 | */ |
||
| 83 | protected $indexingProperties = []; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Flag for lazy loading indexing properties. |
||
| 87 | * |
||
| 88 | * @var bool |
||
| 89 | */ |
||
| 90 | protected $indexingPropertiesLoaded = false; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Flag, whether indexing properties exits for this item. |
||
| 94 | * |
||
| 95 | * @var bool |
||
| 96 | */ |
||
| 97 | protected $hasIndexingProperties = false; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * The record's uid. |
||
| 101 | * |
||
| 102 | * @var int |
||
| 103 | */ |
||
| 104 | protected $recordUid = 0; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * The record itself |
||
| 108 | * |
||
| 109 | * @var array |
||
| 110 | */ |
||
| 111 | protected $record; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @var string |
||
| 115 | */ |
||
| 116 | protected $errors = ''; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Constructor, takes item meta data information and resolves that to the full record. |
||
| 120 | * |
||
| 121 | * @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 |
||
| 122 | * @param array $fullRecord Optional full record for the item. If provided, can save some SQL queries. |
||
| 123 | */ |
||
| 124 | 51 | public function __construct( |
|
| 125 | array $itemMetaData, |
||
| 126 | array $fullRecord = [] |
||
| 127 | ) { |
||
| 128 | 51 | $this->indexQueueUid = $itemMetaData['uid']; |
|
| 129 | 51 | $this->rootPageUid = $itemMetaData['root']; |
|
| 130 | 51 | $this->type = $itemMetaData['item_type']; |
|
| 131 | 51 | $this->recordUid = $itemMetaData['item_uid']; |
|
| 132 | 51 | $this->changed = $itemMetaData['changed']; |
|
| 133 | 51 | $this->errors = (string) empty($itemMetaData['errors']) ? '' : $itemMetaData['errors']; |
|
| 134 | |||
| 135 | 51 | $this->indexingConfigurationName = $itemMetaData['indexing_configuration']; |
|
| 136 | 51 | $this->hasIndexingProperties = (boolean)$itemMetaData['has_indexing_properties']; |
|
| 137 | |||
| 138 | 51 | if (!empty($fullRecord)) { |
|
| 139 | 26 | $this->record = $fullRecord; |
|
| 140 | 26 | } |
|
| 141 | 51 | } |
|
| 142 | |||
| 143 | /** |
||
| 144 | * @return int|mixed |
||
| 145 | */ |
||
| 146 | 8 | public function getIndexQueueUid() |
|
| 147 | { |
||
| 148 | 8 | return $this->indexQueueUid; |
|
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Gets the item's root page ID (uid) |
||
| 153 | * |
||
| 154 | * @return int root page ID |
||
| 155 | */ |
||
| 156 | 18 | public function getRootPageUid() |
|
| 157 | { |
||
| 158 | 18 | return $this->rootPageUid; |
|
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @param integer $uid |
||
| 163 | */ |
||
| 164 | public function setRootPageUid($uid) |
||
| 165 | { |
||
| 166 | $this->rootPageUid = intval($uid); |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @return string |
||
| 171 | */ |
||
| 172 | 5 | public function getErrors() |
|
| 173 | { |
||
| 174 | 5 | return $this->errors; |
|
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Gets the site the item belongs to. |
||
| 179 | * |
||
| 180 | * @return Site Site instance the item belongs to. |
||
| 181 | */ |
||
| 182 | 21 | public function getSite() |
|
| 183 | { |
||
| 184 | 21 | $siteRepository = GeneralUtility::makeInstance(SiteRepository::class); |
|
| 185 | 21 | return $siteRepository->getSiteByRootPageId($this->rootPageUid); |
|
| 186 | } |
||
| 187 | |||
| 188 | 17 | public function getType() |
|
| 189 | { |
||
| 190 | 17 | return $this->type; |
|
| 191 | } |
||
| 192 | |||
| 193 | public function setType($type) |
||
| 194 | { |
||
| 195 | $this->type = $type; |
||
| 196 | } |
||
| 197 | |||
| 198 | 24 | public function getIndexingConfigurationName() |
|
| 199 | { |
||
| 200 | 24 | return $this->indexingConfigurationName; |
|
| 201 | } |
||
| 202 | |||
| 203 | public function setIndexingConfigurationName($indexingConfigurationName) |
||
| 207 | |||
| 208 | public function getChanged() |
||
| 212 | |||
| 213 | public function setChanged($changed) |
||
| 214 | { |
||
| 215 | $this->changed = intval($changed); |
||
| 216 | } |
||
| 217 | |||
| 218 | public function getRecordUid() |
||
| 219 | { |
||
| 220 | $this->getRecord(); |
||
| 221 | |||
| 222 | return $this->record['uid']; |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Gets the item's full record. |
||
| 227 | * |
||
| 228 | * Uses lazy loading. |
||
| 229 | * |
||
| 230 | * @return array The item's DB record. |
||
| 231 | 4 | */ |
|
| 232 | public function getRecord() |
||
| 246 | |||
| 247 | 14 | public function setRecord(array $record) |
|
| 251 | |||
| 252 | /** |
||
| 253 | * @return int |
||
| 254 | */ |
||
| 255 | public function getRecordPageId() |
||
| 256 | { |
||
| 257 | 14 | $this->getRecord(); |
|
| 258 | |||
| 259 | return $this->record['pid']; |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Stores the indexing properties. |
||
| 264 | * |
||
| 265 | */ |
||
| 266 | public function storeIndexingProperties() |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Removes existing indexing properties. |
||
| 279 | 3 | * |
|
| 280 | * @throws \RuntimeException when an SQL error occurs |
||
| 281 | 3 | */ |
|
| 282 | protected function removeIndexingProperties() |
||
| 283 | 3 | { |
|
| 284 | 3 | $GLOBALS['TYPO3_DB']->exec_DELETEquery( |
|
| 285 | 3 | 'tx_solr_indexqueue_indexing_property', |
|
| 286 | 'root = ' . intval($this->rootPageUid) |
||
| 287 | 3 | . ' AND item_id = ' . intval($this->indexQueueUid) |
|
| 288 | 3 | ); |
|
| 289 | |||
| 290 | if ($GLOBALS['TYPO3_DB']->sql_error()) { |
||
| 291 | throw new \RuntimeException( |
||
| 292 | 'Could not remove indexing properties for item ' . $this->indexQueueUid, |
||
| 293 | 1323802532 |
||
| 294 | ); |
||
| 295 | 3 | } |
|
| 296 | } |
||
| 297 | 3 | ||
| 298 | 3 | public function hasIndexingProperties() |
|
| 299 | 3 | { |
|
| 300 | 3 | return $this->hasIndexingProperties; |
|
| 301 | 3 | } |
|
| 302 | |||
| 303 | 3 | /** |
|
| 304 | * Writes all indexing properties. |
||
| 305 | * |
||
| 306 | * @throws \RuntimeException when an SQL error occurs |
||
| 307 | */ |
||
| 308 | protected function writeIndexingProperties() |
||
| 309 | 3 | { |
|
| 310 | $properties = []; |
||
| 311 | 3 | foreach ($this->indexingProperties as $propertyKey => $propertyValue) { |
|
| 312 | $properties[] = [ |
||
| 313 | 3 | $this->rootPageUid, |
|
| 314 | $this->indexQueueUid, |
||
| 315 | $propertyKey, |
||
| 316 | $propertyValue |
||
| 317 | ]; |
||
| 318 | } |
||
| 319 | |||
| 320 | $GLOBALS['TYPO3_DB']->exec_INSERTmultipleRows( |
||
| 321 | 3 | 'tx_solr_indexqueue_indexing_property', |
|
| 322 | ['root', 'item_id', 'property_key', 'property_value'], |
||
| 323 | 3 | $properties |
|
| 324 | 3 | ); |
|
| 325 | 3 | ||
| 326 | 3 | if ($GLOBALS['TYPO3_DB']->sql_error()) { |
|
| 327 | 3 | throw new \RuntimeException( |
|
| 328 | 3 | 'Could not insert indexing properties for item ' . $this->indexQueueUid, |
|
| 329 | 1323802570 |
||
| 330 | 3 | ); |
|
| 331 | 3 | } |
|
| 332 | } |
||
| 333 | 3 | ||
| 334 | 3 | /** |
|
| 335 | 3 | * Updates the "has_indexing_properties" flag in the Index Queue. |
|
| 336 | * |
||
| 337 | 3 | * @throws \RuntimeException when an SQL error occurs |
|
| 338 | */ |
||
| 339 | 3 | protected function updateHasIndexingPropertiesFlag() |
|
| 340 | { |
||
| 341 | $hasIndexingProperties = '0'; |
||
| 342 | if ($this->hasIndexingProperties()) { |
||
| 343 | $hasIndexingProperties = '1'; |
||
| 344 | } |
||
| 345 | 3 | ||
| 346 | $GLOBALS['TYPO3_DB']->exec_UPDATEquery( |
||
| 347 | 'tx_solr_indexqueue_item', |
||
| 348 | 'uid = ' . intval($this->indexQueueUid), |
||
| 349 | ['has_indexing_properties' => $hasIndexingProperties] |
||
| 350 | ); |
||
| 351 | |||
| 352 | 3 | if ($GLOBALS['TYPO3_DB']->sql_error()) { |
|
| 353 | throw new \RuntimeException( |
||
| 354 | 3 | 'Could not update has_indexing_properties flag in Index Queue for item ' . $this->indexQueueUid, |
|
| 355 | 3 | 1323802610 |
|
| 356 | 3 | ); |
|
| 357 | 3 | } |
|
| 358 | } |
||
| 359 | 3 | ||
| 360 | 3 | /** |
|
| 361 | 3 | * @param string $key |
|
| 362 | 3 | * @return bool |
|
| 363 | 3 | */ |
|
| 364 | public function hasIndexingProperty($key) |
||
| 370 | |||
| 371 | 3 | /** |
|
| 372 | * Loads the indexing properties for the item - if not already loaded. |
||
| 373 | * |
||
| 374 | */ |
||
| 375 | public function loadIndexingProperties() |
||
| 393 | 4 | ||
| 394 | 4 | /** |
|
| 395 | 4 | * Sets an indexing property for the item. |
|
| 396 | * |
||
| 397 | 4 | * @param string $key Indexing property name |
|
| 398 | * @param string|int|float $value Indexing property value |
||
| 399 | * @throws \InvalidArgumentException when $value is not string, integer or float |
||
| 400 | */ |
||
| 401 | public function setIndexingProperty($key, $value) |
||
| 402 | { |
||
| 403 | 4 | ||
| 404 | 4 | // make sure to not interfere with existing indexing properties |
|
| 405 | 4 | $this->loadIndexingProperties(); |
|
| 406 | |||
| 407 | $key = (string)$key; // Scalar typehints now! |
||
| 408 | |||
| 409 | if (!is_string($value) && !is_int($value) && !is_float($value)) { |
||
| 410 | throw new \InvalidArgumentException( |
||
| 411 | 'Cannot set indexing property "' . $key |
||
| 412 | . '", its value must be string, integer or float, ' |
||
| 413 | . 'type given was "' . gettype($value) . '"', |
||
| 414 | 3 | 1323173209 |
|
| 415 | ); |
||
| 416 | } |
||
| 417 | |||
| 418 | 3 | $this->indexingProperties[$key] = $value; |
|
| 419 | $this->hasIndexingProperties = true; |
||
| 420 | 3 | } |
|
| 421 | |||
| 422 | 3 | /** |
|
| 423 | * Gets a specific indexing property by its name/key. |
||
| 424 | * |
||
| 425 | * @param string $key Indexing property name/key. |
||
| 426 | * @throws \InvalidArgumentException when the given $key does not exist. |
||
| 427 | * @return string |
||
| 428 | */ |
||
| 429 | public function getIndexingProperty($key) |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Gets all indexing properties set for this item. |
||
| 445 | * |
||
| 446 | * @return array Array of indexing properties. |
||
| 447 | */ |
||
| 448 | public function getIndexingProperties() |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Gets the names/keys of the item's indexing properties. |
||
| 457 | * |
||
| 458 | * @return array Array of indexing property names/keys |
||
| 459 | */ |
||
| 460 | public function getIndexingPropertyKeys() |
||
| 466 | } |
||
| 467 |