Complex classes like GarbageCollector 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 GarbageCollector, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
44 | class GarbageCollector extends AbstractDataHandlerListener implements SingletonInterface |
||
45 | { |
||
46 | /** |
||
47 | * @var array |
||
48 | */ |
||
49 | protected $trackedRecords = []; |
||
50 | |||
51 | /** |
||
52 | * @var TCAService |
||
53 | */ |
||
54 | protected $tcaService; |
||
55 | |||
56 | /** |
||
57 | * GarbageCollector constructor. |
||
58 | * @param TCAService|null $TCAService |
||
59 | */ |
||
60 | 10 | public function __construct(TCAService $TCAService = null) |
|
64 | |||
65 | /** |
||
66 | * Hooks into TCE main and tracks record deletion commands. |
||
67 | * |
||
68 | * @param string $command The command. |
||
69 | * @param string $table The table the record belongs to |
||
70 | * @param int $uid The record's uid |
||
71 | * @param string $value Not used |
||
72 | * @param DataHandler $tceMain TYPO3 Core Engine parent object, not used |
||
73 | * @return void |
||
74 | */ |
||
75 | 2 | public function processCmdmap_preProcess( |
|
76 | $command, |
||
77 | $table, |
||
78 | $uid, |
||
79 | $value, |
||
|
|||
80 | DataHandler $tceMain |
||
81 | ) { |
||
82 | // workspaces: collect garbage only for LIVE workspace |
||
83 | 2 | if ($command == 'delete' && $GLOBALS['BE_USER']->workspace == 0) { |
|
84 | 2 | $this->collectGarbage($table, $uid); |
|
85 | |||
86 | 2 | if ($table == 'pages') { |
|
87 | 1 | $this->getIndexQueue()->deleteItem($table, $uid); |
|
88 | } |
||
89 | } |
||
90 | 2 | } |
|
91 | |||
92 | /** |
||
93 | * Holds the configuration when a recursive page queing should be triggered. |
||
94 | * |
||
95 | * @var array |
||
96 | * @return array |
||
97 | */ |
||
98 | 3 | protected function getUpdateSubPagesRecursiveTriggerConfiguration() |
|
113 | |||
114 | /** |
||
115 | * Tracks down index documents belonging to a particular record or page and |
||
116 | * removes them from the index and the Index Queue. |
||
117 | * |
||
118 | * @param string $table The record's table name. |
||
119 | * @param int $uid The record's uid. |
||
120 | * @throws \UnexpectedValueException if a hook object does not implement interface \ApacheSolrForTypo3\Solr\GarbageCollectorPostProcessor |
||
121 | */ |
||
122 | 10 | public function collectGarbage($table, $uid) |
|
146 | |||
147 | /** |
||
148 | * Tracks down index documents belonging to a particular page and |
||
149 | * removes them from the index and the Index Queue. |
||
150 | * |
||
151 | * @param string $table The record's table name. |
||
152 | * @param int $uid The record's uid. |
||
153 | */ |
||
154 | 10 | protected function collectPageGarbage($table, $uid) |
|
155 | { |
||
156 | switch ($table) { |
||
157 | 10 | case 'tt_content': |
|
158 | 4 | $contentElement = BackendUtility::getRecord('tt_content', $uid, 'uid, pid', '', false); |
|
159 | |||
160 | 4 | $table = 'pages'; |
|
161 | 4 | $uid = $contentElement['pid']; |
|
162 | |||
163 | 4 | $this->deleteIndexDocuments($table, $uid); |
|
164 | // only a content element was removed, now update/re-index the page |
||
165 | 4 | $this->getIndexQueue()->updateItem($table, $uid); |
|
166 | 4 | break; |
|
167 | 6 | case 'pages_language_overlay': |
|
168 | $pageOverlayRecord = BackendUtility::getRecord('pages_language_overlay', $uid, 'uid, pid', '', false); |
||
169 | |||
170 | $table = 'pages'; |
||
171 | $uid = $pageOverlayRecord['pid']; |
||
172 | |||
173 | $this->deleteIndexDocuments($table, $uid); |
||
174 | // only a page overlay was removed, now update/re-index the page |
||
175 | $this->getIndexQueue()->updateItem($table, $uid); |
||
176 | break; |
||
177 | 6 | case 'pages': |
|
178 | |||
179 | 6 | $this->deleteIndexDocuments($table, $uid); |
|
180 | 6 | $this->getIndexQueue()->deleteItem($table, $uid); |
|
181 | |||
182 | 6 | break; |
|
183 | } |
||
184 | 10 | } |
|
185 | |||
186 | /** |
||
187 | * @param string $table |
||
188 | * @param int $uid |
||
189 | * @param array $changedFields |
||
190 | */ |
||
191 | 3 | protected function deleteSubpagesWhenExtendToSubpagesIsSet($table, $uid, $changedFields) |
|
192 | { |
||
193 | 3 | if (!$this->isRecursivePageUpdateRequired($uid, $changedFields)) { |
|
194 | 1 | return; |
|
195 | } |
||
196 | |||
197 | 2 | $indexQueue = $this->getIndexQueue(); |
|
198 | // get affected subpages when "extendToSubpages" flag was set |
||
199 | 2 | $pagesToDelete = $this->getSubPageIds($uid); |
|
200 | // we need to at least remove this page |
||
201 | 2 | foreach ($pagesToDelete as $pageToDelete) { |
|
202 | 2 | $this->deleteIndexDocuments($table, $pageToDelete); |
|
203 | 2 | $indexQueue->deleteItem($table, $pageToDelete); |
|
204 | } |
||
205 | 2 | } |
|
206 | |||
207 | /** |
||
208 | * Deletes index documents for a given record identification. |
||
209 | * |
||
210 | * @param string $table The record's table name. |
||
211 | * @param int $uid The record's uid. |
||
212 | */ |
||
213 | 10 | protected function deleteIndexDocuments($table, $uid) |
|
235 | |||
236 | /** |
||
237 | * Tracks down index documents belonging to a particular record and |
||
238 | * removes them from the index and the Index Queue. |
||
239 | * |
||
240 | * @param string $table The record's table name. |
||
241 | * @param int $uid The record's uid. |
||
242 | */ |
||
243 | protected function collectRecordGarbage($table, $uid) |
||
248 | |||
249 | // methods checking whether to trigger garbage collection |
||
250 | |||
251 | /** |
||
252 | * Hooks into TCE main and tracks page move commands. |
||
253 | * |
||
254 | * @param string $command The command. |
||
255 | * @param string $table The table the record belongs to |
||
256 | * @param int $uid The record's uid |
||
257 | * @param string $value Not used |
||
258 | * @param DataHandler $tceMain TYPO3 Core Engine parent object, not used |
||
259 | */ |
||
260 | 2 | public function processCmdmap_postProcess( |
|
280 | |||
281 | /** |
||
282 | * Hooks into TCE main and tracks changed records. In this case the current |
||
283 | * record's values are stored to do a change comparison later on for fields |
||
284 | * like fe_group. |
||
285 | * |
||
286 | * @param array $incomingFields An array of incoming fields, new or changed, not used |
||
287 | * @param string $table The table the record belongs to |
||
288 | * @param mixed $uid The record's uid, [integer] or [string] (like 'NEW...') |
||
289 | * @param DataHandler $tceMain TYPO3 Core Engine parent object, not used |
||
290 | */ |
||
291 | 4 | public function processDatamap_preProcessFieldArray( |
|
292 | $incomingFields, |
||
293 | $table, |
||
294 | $uid, |
||
295 | DataHandler $tceMain |
||
296 | ) { |
||
297 | 4 | if (!is_int($uid)) { |
|
298 | // a newly created record, skip |
||
299 | return; |
||
300 | } |
||
301 | |||
302 | 4 | if (Util::isDraftRecord($table, $uid)) { |
|
303 | // skip workspaces: collect garbage only for LIVE workspace |
||
304 | return; |
||
305 | } |
||
306 | |||
307 | 4 | $hasConfiguredEnableColumnForFeGroup = $this->tcaService->isEnableColumn($table, 'fe_group'); |
|
308 | |||
309 | 4 | if ($hasConfiguredEnableColumnForFeGroup) { |
|
310 | 4 | $visibilityAffectingFields = $this->tcaService->getVisibilityAffectingFieldsByTable($table); |
|
311 | 4 | $record = BackendUtility::getRecord( |
|
312 | $table, |
||
313 | $uid, |
||
314 | $visibilityAffectingFields, |
||
315 | 4 | '', |
|
316 | 4 | false |
|
317 | ); |
||
318 | 4 | $record = $this->tcaService->normalizeFrontendGroupField($table, $record); |
|
319 | |||
320 | // keep previous state of important fields for later comparison |
||
321 | 4 | $this->trackedRecords[$table][$uid] = $record; |
|
322 | } |
||
323 | 4 | } |
|
324 | |||
325 | /** |
||
326 | * Hooks into TCE Main and watches all record updates. If a change is |
||
327 | * detected that would remove the record from the website, we try to find |
||
328 | * related documents and remove them from the index. |
||
329 | * |
||
330 | * @param string $status Status of the current operation, 'new' or 'update' |
||
331 | * @param string $table The table the record belongs to |
||
332 | * @param mixed $uid The record's uid, [integer] or [string] (like 'NEW...') |
||
333 | * @param array $fields The record's data, not used |
||
334 | * @param DataHandler $tceMain TYPO3 Core Engine parent object, not used |
||
335 | */ |
||
336 | 6 | public function processDatamap_afterDatabaseOperations( |
|
337 | $status, |
||
338 | $table, |
||
339 | $uid, |
||
340 | array $fields, |
||
341 | DataHandler $tceMain |
||
342 | ) { |
||
343 | 6 | if ($status == 'new') { |
|
344 | // a newly created record, skip |
||
345 | return; |
||
346 | } |
||
347 | |||
348 | 6 | if (Util::isDraftRecord($table, $uid)) { |
|
349 | // skip workspaces: collect garbage only for LIVE workspace |
||
350 | return; |
||
351 | } |
||
352 | |||
353 | 6 | $garbageCollectionRelevantFields = $this->tcaService->getVisibilityAffectingFieldsByTable($table); |
|
354 | |||
355 | 6 | $record = BackendUtility::getRecord($table, $uid, $garbageCollectionRelevantFields, '', false); |
|
356 | 6 | $record = $this->tcaService->normalizeFrontendGroupField($table, $record); |
|
357 | |||
358 | 6 | if ($this->tcaService->isHidden($table, $record) |
|
359 | 2 | || $this->isInvisibleByStartOrEndtime($table, $record) |
|
360 | || $this->hasFrontendGroupsRemoved($table, $record) |
||
361 | || ($table == 'pages' && $this->isPageExcludedFromSearch($record)) |
||
362 | 6 | || ($table == 'pages' && !$this->isIndexablePageType($record)) |
|
363 | ) { |
||
364 | 6 | $this->collectGarbage($table, $uid); |
|
365 | |||
366 | 6 | if ($table == 'pages') { |
|
367 | 3 | $this->deleteSubpagesWhenExtendToSubpagesIsSet($table, $uid, $fields); |
|
368 | } |
||
369 | } |
||
370 | 6 | } |
|
371 | |||
372 | /** |
||
373 | * Check if a record is getting invisible due to changes in start or endtime. In addition it is checked that the related |
||
374 | * queue item was marked as indexed. |
||
375 | * |
||
376 | * @param string $table |
||
377 | * @param array $record |
||
378 | * @return bool |
||
379 | */ |
||
380 | 2 | protected function isInvisibleByStartOrEndtime($table, $record) |
|
381 | { |
||
382 | return ( |
||
383 | 2 | ($this->tcaService->isStartTimeInFuture($table, $record) || $this->tcaService->isEndTimeInPast($table, $record)) && |
|
384 | 2 | $this->isRelatedQueueRecordMarkedAsIndexed($table, $record) |
|
385 | ); |
||
386 | } |
||
387 | |||
388 | /** |
||
389 | * Checks if the related index queue item is indexed. |
||
390 | * |
||
391 | * * For tt_content and pages_language_overlay the page from the pid is checked |
||
392 | * * For all other records the table it's self is checked |
||
393 | * |
||
394 | * @param string $table The table name. |
||
395 | * @param array $record An array with record fields that may affect visibility. |
||
396 | * @return bool True if the record is marked as being indexed |
||
397 | */ |
||
398 | 2 | protected function isRelatedQueueRecordMarkedAsIndexed($table, $record) |
|
399 | { |
||
400 | 2 | if ($table == 'tt_content' || $table == 'pages_language_overlay') { |
|
401 | 2 | $table = 'pages'; |
|
402 | 2 | $uid = $record['pid']; |
|
403 | } else { |
||
404 | $uid = $record['uid']; |
||
405 | } |
||
406 | |||
407 | 2 | return $this->getIndexQueue()->containsIndexedItem($table, $uid); |
|
408 | } |
||
409 | |||
410 | /** |
||
411 | * @return Queue |
||
412 | */ |
||
413 | 10 | private function getIndexQueue() |
|
417 | |||
418 | /** |
||
419 | * Checks whether the a frontend group field exists for the record and if so |
||
420 | * whether groups have been removed from accessing the record thus making |
||
421 | * the record invisible to at least some people. |
||
422 | * |
||
423 | * @param string $table The table name. |
||
424 | * @param array $record An array with record fields that may affect visibility. |
||
425 | * @return bool TRUE if frontend groups have been removed from access to the record, FALSE otherwise. |
||
426 | */ |
||
427 | protected function hasFrontendGroupsRemoved($table, $record) |
||
446 | |||
447 | /** |
||
448 | * Checks whether the page has been excluded from searching. |
||
449 | * |
||
450 | * @param array $record An array with record fields that may affect visibility. |
||
451 | * @return bool True if the page has been excluded from searching, FALSE otherwise |
||
452 | */ |
||
453 | protected function isPageExcludedFromSearch($record) |
||
457 | |||
458 | /** |
||
459 | * Checks whether a page has a page type that can be indexed. |
||
460 | * Currently standard pages and mount pages can be indexed. |
||
461 | * |
||
462 | * @param array $record A page record |
||
463 | * @return bool TRUE if the page can be indexed according to its page type, FALSE otherwise |
||
464 | */ |
||
465 | protected function isIndexablePageType(array $record) |
||
469 | |||
470 | /** |
||
471 | * Cleans an index from garbage entries. |
||
472 | * |
||
473 | * Was used to clean the index from expired documents/past endtime. Solr 4.8 |
||
474 | * introduced DocExpirationUpdateProcessor to do that job by itself. |
||
475 | * |
||
476 | * The method remains as a dummy for possible later cleanups and to prevent |
||
477 | * things from breaking if others were using it. |
||
478 | * |
||
479 | * @deprecated since 6.0 will be removed in 7.0. deletion is done by DocExpirationUpdateProcessor |
||
480 | * @param Site $site The site to clean indexes on |
||
481 | * @param bool $commitAfterCleanUp Whether to commit right after the clean up, defaults to TRUE |
||
482 | * @return void |
||
483 | */ |
||
484 | public function cleanIndex(Site $site, $commitAfterCleanUp = true) |
||
488 | } |
||
489 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.