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( |
|
141 | |||
142 | /** |
||
143 | * @return int|mixed |
||
144 | */ |
||
145 | 5 | 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) |
||
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 | * @deprecated since 6.1 will be removed in 7.0 |
||
221 | */ |
||
222 | public function updateIndexedTime() |
||
223 | { |
||
224 | GeneralUtility::logDeprecatedFunction(); |
||
225 | $queue = GeneralUtility::makeInstance(Queue::class); |
||
226 | $queue->updateIndexTimeByItem($this); |
||
227 | } |
||
228 | |||
229 | 4 | public function getRecordUid() |
|
235 | |||
236 | /** |
||
237 | * Gets the item's full record. |
||
238 | * |
||
239 | * Uses lazy loading. |
||
240 | * |
||
241 | * @return array The item's DB record. |
||
242 | */ |
||
243 | 14 | public function getRecord() |
|
257 | |||
258 | public function setRecord(array $record) |
||
259 | { |
||
260 | $this->record = $record; |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * @return int |
||
265 | */ |
||
266 | 12 | public function getRecordPageId() |
|
272 | |||
273 | /** |
||
274 | * Stores the indexing properties. |
||
275 | * |
||
276 | */ |
||
277 | 3 | public function storeIndexingProperties() |
|
278 | { |
||
279 | 3 | $this->removeIndexingProperties(); |
|
280 | |||
281 | 3 | if ($this->hasIndexingProperties()) { |
|
282 | 3 | $this->writeIndexingProperties(); |
|
283 | } |
||
284 | |||
285 | 3 | $this->updateHasIndexingPropertiesFlag(); |
|
286 | 3 | } |
|
287 | |||
288 | /** |
||
289 | * Removes existing indexing properties. |
||
290 | * |
||
291 | * @throws \RuntimeException when an SQL error occurs |
||
292 | */ |
||
293 | 3 | protected function removeIndexingProperties() |
|
294 | { |
||
295 | 3 | $GLOBALS['TYPO3_DB']->exec_DELETEquery( |
|
296 | 3 | 'tx_solr_indexqueue_indexing_property', |
|
297 | 3 | 'root = ' . intval($this->rootPageUid) |
|
298 | 3 | . ' AND item_id = ' . intval($this->indexQueueUid) |
|
299 | ); |
||
300 | |||
301 | 3 | if ($GLOBALS['TYPO3_DB']->sql_error()) { |
|
302 | throw new \RuntimeException( |
||
303 | 'Could not remove indexing properties for item ' . $this->indexQueueUid, |
||
304 | 1323802532 |
||
305 | ); |
||
306 | } |
||
307 | 3 | } |
|
308 | |||
309 | 3 | public function hasIndexingProperties() |
|
313 | |||
314 | /** |
||
315 | * Writes all indexing properties. |
||
316 | * |
||
317 | * @throws \RuntimeException when an SQL error occurs |
||
318 | */ |
||
319 | 3 | protected function writeIndexingProperties() |
|
320 | { |
||
321 | 3 | $properties = []; |
|
322 | 3 | foreach ($this->indexingProperties as $propertyKey => $propertyValue) { |
|
323 | 3 | $properties[] = [ |
|
324 | 3 | $this->rootPageUid, |
|
325 | 3 | $this->indexQueueUid, |
|
326 | 3 | $propertyKey, |
|
327 | 3 | $propertyValue |
|
328 | ]; |
||
329 | } |
||
330 | |||
331 | 3 | $GLOBALS['TYPO3_DB']->exec_INSERTmultipleRows( |
|
332 | 3 | 'tx_solr_indexqueue_indexing_property', |
|
333 | 3 | ['root', 'item_id', 'property_key', 'property_value'], |
|
334 | $properties |
||
335 | ); |
||
336 | |||
337 | 3 | if ($GLOBALS['TYPO3_DB']->sql_error()) { |
|
338 | throw new \RuntimeException( |
||
339 | 'Could not insert indexing properties for item ' . $this->indexQueueUid, |
||
340 | 1323802570 |
||
341 | ); |
||
342 | } |
||
343 | 3 | } |
|
344 | |||
345 | /** |
||
346 | * Updates the "has_indexing_properties" flag in the Index Queue. |
||
347 | * |
||
348 | * @throws \RuntimeException when an SQL error occurs |
||
349 | */ |
||
350 | 3 | protected function updateHasIndexingPropertiesFlag() |
|
351 | { |
||
352 | 3 | $hasIndexingProperties = '0'; |
|
353 | 3 | if ($this->hasIndexingProperties()) { |
|
354 | 3 | $hasIndexingProperties = '1'; |
|
355 | } |
||
356 | |||
357 | 3 | $GLOBALS['TYPO3_DB']->exec_UPDATEquery( |
|
358 | 3 | 'tx_solr_indexqueue_item', |
|
359 | 3 | 'uid = ' . intval($this->indexQueueUid), |
|
360 | 3 | ['has_indexing_properties' => $hasIndexingProperties] |
|
361 | ); |
||
362 | |||
363 | 3 | if ($GLOBALS['TYPO3_DB']->sql_error()) { |
|
364 | throw new \RuntimeException( |
||
365 | 'Could not update has_indexing_properties flag in Index Queue for item ' . $this->indexQueueUid, |
||
366 | 1323802610 |
||
367 | ); |
||
368 | } |
||
369 | 3 | } |
|
370 | |||
371 | /** |
||
372 | * @param string $key |
||
373 | * @return bool |
||
374 | */ |
||
375 | 1 | public function hasIndexingProperty($key) |
|
381 | |||
382 | /** |
||
383 | * Loads the indexing properties for the item - if not already loaded. |
||
384 | * |
||
385 | */ |
||
386 | 4 | public function loadIndexingProperties() |
|
387 | { |
||
388 | 4 | if (!$this->indexingPropertiesLoaded) { |
|
389 | 4 | $indexingProperties = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows( |
|
390 | 4 | 'property_key, property_value', |
|
391 | 4 | 'tx_solr_indexqueue_indexing_property', |
|
392 | 4 | 'item_id = ' . intval($this->indexQueueUid) |
|
393 | ); |
||
394 | |||
395 | 4 | if (!empty($indexingProperties)) { |
|
396 | foreach ($indexingProperties as $indexingProperty) { |
||
397 | $this->indexingProperties[$indexingProperty['property_key']] = $indexingProperty['property_value']; |
||
398 | } |
||
399 | } |
||
400 | |||
401 | 4 | $this->indexingPropertiesLoaded = true; |
|
402 | } |
||
403 | 4 | } |
|
404 | |||
405 | /** |
||
406 | * Sets an indexing property for the item. |
||
407 | * |
||
408 | * @param string $key Indexing property name |
||
409 | * @param string|int|float $value Indexing property value |
||
410 | * @throws \InvalidArgumentException when $value is not string, integer or float |
||
411 | */ |
||
412 | 3 | public function setIndexingProperty($key, $value) |
|
413 | { |
||
414 | |||
415 | // make sure to not interfere with existing indexing properties |
||
416 | 3 | $this->loadIndexingProperties(); |
|
417 | |||
418 | 3 | $key = (string)$key; // Scalar typehints now! |
|
419 | |||
420 | 3 | if (!is_string($value) && !is_int($value) && !is_float($value)) { |
|
421 | throw new \InvalidArgumentException( |
||
422 | 'Cannot set indexing property "' . $key |
||
423 | . '", its value must be string, integer or float, ' |
||
424 | . 'type given was "' . gettype($value) . '"', |
||
425 | 1323173209 |
||
426 | ); |
||
427 | } |
||
428 | |||
429 | 3 | $this->indexingProperties[$key] = $value; |
|
430 | 3 | $this->hasIndexingProperties = true; |
|
431 | 3 | } |
|
432 | |||
433 | /** |
||
434 | * Gets a specific indexing property by its name/key. |
||
435 | * |
||
436 | * @param string $key Indexing property name/key. |
||
437 | * @throws \InvalidArgumentException when the given $key does not exist. |
||
438 | * @return string |
||
439 | */ |
||
440 | public function getIndexingProperty($key) |
||
453 | |||
454 | /** |
||
455 | * Gets all indexing properties set for this item. |
||
456 | * |
||
457 | * @return array Array of indexing properties. |
||
458 | */ |
||
459 | public function getIndexingProperties() |
||
465 | |||
466 | /** |
||
467 | * Gets the names/keys of the item's indexing properties. |
||
468 | * |
||
469 | * @return array Array of indexing property names/keys |
||
470 | */ |
||
471 | public function getIndexingPropertyKeys() |
||
477 | } |
||
478 |