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