Complex classes like Relation 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 Relation, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
48 | class Relation |
||
49 | { |
||
50 | const CONTENT_OBJECT_NAME = 'SOLR_RELATION'; |
||
51 | |||
52 | /** |
||
53 | * Content object configuration |
||
54 | * |
||
55 | * @var array |
||
56 | */ |
||
57 | protected $configuration = []; |
||
58 | |||
59 | /** |
||
60 | * Constructor. |
||
61 | * |
||
62 | */ |
||
63 | 7 | public function __construct() |
|
68 | |||
69 | /** |
||
70 | * Executes the SOLR_RELATION content object. |
||
71 | * |
||
72 | * Resolves relations between records. Currently supported relations are |
||
73 | * TYPO3-style m:n relations. |
||
74 | * May resolve single value and multi value relations. |
||
75 | * |
||
76 | * @param string $name content object name 'SOLR_RELATION' |
||
77 | * @param array $configuration for the content object |
||
78 | * @param string $TyposcriptKey not used |
||
79 | * @param \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer $parentContentObject parent content object |
||
80 | * @return string serialized array representation of the given list |
||
81 | */ |
||
82 | 7 | public function cObjGetSingleExt( |
|
112 | |||
113 | /** |
||
114 | * Gets the related items of the current record's configured field. |
||
115 | * |
||
116 | * @param ContentObjectRenderer $parentContentObject parent content object |
||
117 | * @return array Array of related items, values already resolved from related records |
||
118 | */ |
||
119 | 7 | protected function getRelatedItems( |
|
120 | ContentObjectRenderer $parentContentObject |
||
121 | ) { |
||
122 | 7 | $relatedItems = []; |
|
123 | |||
124 | 7 | list($localTableName, $localRecordUid) = explode(':', |
|
125 | 7 | $parentContentObject->currentRecord); |
|
126 | |||
127 | 7 | $localTableNameOrg = $localTableName; |
|
128 | // pages has a special overlay table constriction |
||
129 | 7 | if ($GLOBALS['TSFE']->sys_language_uid > 0 && $localTableName === 'pages') { |
|
130 | 1 | $localTableName = 'pages_language_overlay'; |
|
131 | } |
||
132 | |||
133 | 7 | $localTableTca = $GLOBALS['TCA'][$localTableName]; |
|
134 | 7 | $localFieldName = $this->configuration['localField']; |
|
135 | |||
136 | 7 | if (isset($localTableTca['columns'][$localFieldName])) { |
|
137 | 7 | $localFieldTca = $localTableTca['columns'][$localFieldName]; |
|
138 | 7 | $localRecordUid = $this->getUidOfRecordOverlay($localTableNameOrg, $localRecordUid); |
|
139 | 7 | if (isset($localFieldTca['config']['MM']) && trim($localFieldTca['config']['MM']) !== '') { |
|
140 | 5 | $relatedItems = $this->getRelatedItemsFromMMTable($localTableName, |
|
141 | 5 | $localRecordUid, $localFieldTca); |
|
142 | } else { |
||
143 | 2 | $relatedItems = $this->getRelatedItemsFromForeignTable($localTableName, |
|
144 | 2 | $localRecordUid, $localFieldTca, $parentContentObject); |
|
145 | } |
||
146 | } |
||
147 | |||
148 | 7 | return $relatedItems; |
|
149 | } |
||
150 | |||
151 | /** |
||
152 | * Gets the related items from a table using a n:m relation. |
||
153 | * |
||
154 | * @param string $localTableName Local table name |
||
155 | * @param int $localRecordUid Local record uid |
||
156 | * @param array $localFieldTca The local table's TCA |
||
157 | * @return array Array of related items, values already resolved from related records |
||
158 | */ |
||
159 | 5 | protected function getRelatedItemsFromMMTable( |
|
213 | |||
214 | /** |
||
215 | * Resolves the field to use as the related item's label depending on TCA |
||
216 | * and TypoScript configuration |
||
217 | * |
||
218 | * @param array $foreignTableTca The foreign table's TCA |
||
219 | * @return string The field to use for the related item's label |
||
220 | */ |
||
221 | 7 | protected function resolveForeignTableLabelField(array $foreignTableTca) |
|
238 | |||
239 | /** |
||
240 | * Return the translated record |
||
241 | * |
||
242 | * @param string $tableName |
||
243 | * @param array $record |
||
244 | * @return array |
||
245 | */ |
||
246 | 3 | protected function getTranslationOverlay($tableName, $record) |
|
254 | |||
255 | /** |
||
256 | * Gets the related items from a table using a 1:n relation. |
||
257 | * |
||
258 | * @param string $localTableName Local table name |
||
259 | * @param int $localRecordUid Local record uid |
||
260 | * @param array $localFieldTca The local table's TCA |
||
261 | * @param ContentObjectRenderer $parentContentObject parent content object |
||
262 | * @return array Array of related items, values already resolved from related records |
||
263 | */ |
||
264 | 2 | protected function getRelatedItemsFromForeignTable( |
|
265 | $localTableName, |
||
266 | $localRecordUid, |
||
267 | array $localFieldTca, |
||
268 | ContentObjectRenderer $parentContentObject |
||
269 | ) { |
||
270 | 2 | $relatedItems = []; |
|
271 | 2 | $foreignTableName = $localFieldTca['config']['foreign_table']; |
|
272 | 2 | $foreignTableTca = $GLOBALS['TCA'][$foreignTableName]; |
|
273 | 2 | $foreignTableLabelField = $this->resolveForeignTableLabelField($foreignTableTca); |
|
274 | |||
275 | /** @var $relationHandler RelationHandler */ |
||
276 | 2 | $relationHandler = GeneralUtility::makeInstance(RelationHandler::class); |
|
277 | |||
278 | 2 | $itemList = isset($parentContentObject->data[$this->configuration['localField']]) ? |
|
279 | 2 | $parentContentObject->data[$this->configuration['localField']] : ''; |
|
280 | |||
281 | 2 | $relationHandler->start($itemList, $foreignTableName, '', $localRecordUid, $localTableName, $localFieldTca['config']); |
|
282 | 2 | $selectUids = $relationHandler->tableArray[$foreignTableName]; |
|
283 | |||
284 | 2 | if (!is_array($selectUids) || count($selectUids) <= 0) { |
|
285 | return $relatedItems; |
||
286 | } |
||
287 | |||
288 | 2 | $pageSelector = GeneralUtility::makeInstance(PageRepository::class); |
|
289 | 2 | $whereClause = $pageSelector->enableFields($foreignTableName); |
|
290 | 2 | $relatedRecords = $this->getRelatedRecords($foreignTableName, $selectUids, $whereClause); |
|
291 | |||
292 | 2 | foreach ($relatedRecords as $relatedRecord) { |
|
293 | 2 | $resolveRelatedValue = $this->resolveRelatedValue( |
|
294 | 2 | $relatedRecord, |
|
295 | 2 | $foreignTableTca, |
|
296 | 2 | $foreignTableLabelField, |
|
297 | 2 | $parentContentObject, |
|
298 | 2 | $foreignTableName |
|
299 | ); |
||
300 | 2 | if (!empty($resolveRelatedValue) || !$this->configuration['removeEmptyValues']) { |
|
301 | 2 | $relatedItems[] = $resolveRelatedValue; |
|
302 | } |
||
303 | } |
||
304 | |||
305 | 2 | return $relatedItems; |
|
306 | } |
||
307 | |||
308 | /** |
||
309 | * Resolves the value of the related field. If the related field's value is |
||
310 | * a relation itself, this method takes care of resolving it recursively. |
||
311 | * |
||
312 | * @param array $relatedRecord Related record as array |
||
313 | * @param array $foreignTableTca TCA of the related table |
||
314 | * @param string $foreignTableLabelField Field name of the foreign label field |
||
315 | * @param ContentObjectRenderer $parentContentObject cObject |
||
316 | * @param string $foreignTableName Related record table name |
||
317 | * |
||
318 | * @return string |
||
319 | */ |
||
320 | 2 | protected function resolveRelatedValue( |
|
321 | array $relatedRecord, |
||
322 | $foreignTableTca, |
||
323 | $foreignTableLabelField, |
||
324 | ContentObjectRenderer $parentContentObject, |
||
325 | $foreignTableName = '' |
||
326 | ) { |
||
327 | 2 | if ($GLOBALS['TSFE']->sys_language_uid > 0 && !empty($foreignTableName)) { |
|
328 | $relatedRecord = $this->getTranslationOverlay($foreignTableName, $relatedRecord); |
||
329 | } |
||
330 | |||
331 | 2 | $value = $relatedRecord[$foreignTableLabelField]; |
|
332 | |||
333 | if ( |
||
334 | 2 | !empty($foreignTableName) |
|
335 | 2 | && isset($foreignTableTca['columns'][$foreignTableLabelField]['config']['foreign_table']) |
|
336 | 2 | && $this->configuration['enableRecursiveValueResolution'] |
|
337 | ) { |
||
338 | // backup |
||
339 | 1 | $backupRecord = $parentContentObject->data; |
|
340 | 1 | $backupConfiguration = $this->configuration; |
|
341 | |||
342 | // adjust configuration for next level |
||
343 | 1 | $this->configuration['localField'] = $foreignTableLabelField; |
|
344 | 1 | $parentContentObject->data = $relatedRecord; |
|
345 | 1 | if (strpos($this->configuration['foreignLabelField'], '.') !== false) { |
|
346 | 1 | list(, $this->configuration['foreignLabelField']) = explode('.', |
|
347 | 1 | $this->configuration['foreignLabelField'], 2); |
|
348 | } else { |
||
349 | 1 | $this->configuration['foreignLabelField'] = ''; |
|
350 | } |
||
351 | |||
352 | // recursion |
||
353 | 1 | $relatedItemsFromForeignTable = $this->getRelatedItemsFromForeignTable( |
|
354 | 1 | $foreignTableName, |
|
355 | 1 | $relatedRecord['uid'], |
|
356 | 1 | $foreignTableTca['columns'][$foreignTableLabelField], |
|
357 | 1 | $parentContentObject |
|
358 | ); |
||
359 | 1 | $value = array_pop($relatedItemsFromForeignTable); |
|
360 | |||
361 | // restore |
||
362 | 1 | $this->configuration = $backupConfiguration; |
|
363 | 1 | $parentContentObject->data = $backupRecord; |
|
364 | } |
||
365 | |||
366 | 2 | return $parentContentObject->stdWrap($value, $this->configuration); |
|
367 | } |
||
368 | |||
369 | /** |
||
370 | * When the record has an overlay we retrieve the uid of the translated record, |
||
371 | * to resolve the relations from the translation. |
||
372 | * |
||
373 | * @param string $localTableName |
||
374 | * @param int $localRecordUid |
||
375 | * @return int |
||
376 | */ |
||
377 | 7 | protected function getUidOfRecordOverlay($localTableName, $localRecordUid) |
|
400 | |||
401 | /** |
||
402 | * This method retrieves the _PAGES_OVERLAY_UID or _LOCALIZED_UID from the localized record. |
||
403 | * |
||
404 | * @param string $localTableName |
||
405 | * @param array $overlayRecord |
||
406 | * @return int |
||
407 | */ |
||
408 | 3 | protected function getLocalRecordUidFromOverlay($localTableName, $overlayRecord) |
|
421 | |||
422 | /** |
||
423 | * Return records via relation. |
||
424 | * |
||
425 | * @param string $foreignTable The table to fetch records from. |
||
426 | * @param array $uids The uids to fetch from table. |
||
427 | * @param string $whereClause The where clause to append. |
||
428 | * |
||
429 | * @return array |
||
430 | */ |
||
431 | 7 | protected function getRelatedRecords($foreignTable, array $uids, $whereClause) |
|
446 | } |
||
447 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.