Complex classes like Util 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 Util, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
49 | class Util |
||
50 | { |
||
51 | |||
52 | /** |
||
53 | * Generates a document id for documents representing page records. |
||
54 | * |
||
55 | * @param int $uid The page's uid |
||
56 | * @param int $typeNum The page's typeNum |
||
57 | * @param int $language the language id, defaults to 0 |
||
58 | * @param string $accessGroups comma separated list of uids of groups that have access to that page |
||
59 | * @param string $mountPointParameter The mount point parameter that is used to access the page. |
||
60 | * @return string The document id for that page |
||
61 | */ |
||
62 | 53 | public static function getPageDocumentId( |
|
63 | $uid, |
||
64 | $typeNum = 0, |
||
65 | $language = 0, |
||
66 | $accessGroups = '0,-1', |
||
67 | $mountPointParameter = '' |
||
68 | ) { |
||
69 | 53 | $additionalParameters = $typeNum . '/' . $language . '/' . $accessGroups; |
|
70 | |||
71 | 53 | if ((string)$mountPointParameter !== '') { |
|
72 | 2 | $additionalParameters = $mountPointParameter . '/' . $additionalParameters; |
|
73 | } |
||
74 | |||
75 | 53 | $documentId = self::getDocumentId( |
|
76 | 53 | 'pages', |
|
77 | $uid, |
||
78 | $uid, |
||
79 | $additionalParameters |
||
80 | ); |
||
81 | |||
82 | 53 | return $documentId; |
|
83 | } |
||
84 | |||
85 | /** |
||
86 | * Generates a document id in the form $siteHash/$type/$uid. |
||
87 | * |
||
88 | * @param string $table The records table name |
||
89 | * @param int $rootPageId The record's site root id |
||
90 | * @param int $uid The record's uid |
||
91 | * @param string $additionalIdParameters Additional ID parameters |
||
92 | * @return string A document id |
||
93 | */ |
||
94 | 69 | public static function getDocumentId( |
|
111 | |||
112 | /** |
||
113 | * Shortcut to retrieve the TypoScript configuration for EXT:solr |
||
114 | * (plugin.tx_solr) from TSFE. |
||
115 | * |
||
116 | * @return TypoScriptConfiguration |
||
117 | */ |
||
118 | 153 | public static function getSolrConfiguration() |
|
123 | |||
124 | /** |
||
125 | * Gets the Solr configuration for a specific root page id. |
||
126 | * To be used from the backend. |
||
127 | * |
||
128 | * @param int $pageId Id of the (root) page to get the Solr configuration from. |
||
129 | * @param bool $initializeTsfe Optionally initializes a full TSFE to get the configuration, defaults to FALSE |
||
130 | * @param int $language System language uid, optional, defaults to 0 |
||
131 | * @return TypoScriptConfiguration The Solr configuration for the requested tree. |
||
132 | */ |
||
133 | 72 | public static function getSolrConfigurationFromPageId( |
|
141 | |||
142 | /** |
||
143 | * Loads the TypoScript configuration for a given page id and language. |
||
144 | * Language usage may be disabled to get the default TypoScript |
||
145 | * configuration. |
||
146 | * |
||
147 | * @param int $pageId Id of the (root) page to get the Solr configuration from. |
||
148 | * @param string $path The TypoScript configuration path to retrieve. |
||
149 | * @param bool $initializeTsfe Optionally initializes a full TSFE to get the configuration, defaults to FALSE |
||
150 | * @param int $language System language uid, optional, defaults to 0 |
||
151 | * @param bool $useTwoLevelCache Flag to enable the two level cache for the typoscript configuration array |
||
152 | * @return TypoScriptConfiguration The Solr configuration for the requested tree. |
||
153 | */ |
||
154 | 74 | public static function getConfigurationFromPageId( |
|
196 | |||
197 | /** |
||
198 | * This method retrieves the closest pageId where a configuration is located, when this |
||
199 | * feature is enabled. |
||
200 | * |
||
201 | * @param int $pageId |
||
202 | * @return int |
||
203 | */ |
||
204 | 74 | protected static function getConfigurationPageIdToUse($pageId) |
|
215 | |||
216 | /** |
||
217 | * Initializes a TSFE, if required and builds an configuration array, containing the solr configuration. |
||
218 | * |
||
219 | * @param integer $pageId |
||
220 | * @param string $path |
||
221 | * @param boolean $initializeTsfe |
||
222 | * @param integer $language |
||
223 | * @return array |
||
224 | */ |
||
225 | 73 | protected static function buildConfigurationArray($pageId, $path, $initializeTsfe, $language) |
|
236 | |||
237 | /** |
||
238 | * Builds the configuration object from a config array and returns it. |
||
239 | * |
||
240 | * @param array $configurationToUse |
||
241 | * @param int $pageId |
||
242 | * @param int $languageId |
||
243 | * @param string $typoScriptPath |
||
244 | * @return TypoScriptConfiguration |
||
245 | */ |
||
246 | 74 | protected static function buildTypoScriptConfigurationFromArray(array $configurationToUse, $pageId, $languageId, $typoScriptPath) |
|
251 | |||
252 | /** |
||
253 | * This function is used to retrieve the configuration from a previous initialized TSFE |
||
254 | * (see: getConfigurationFromPageId) |
||
255 | * |
||
256 | * @param string $path |
||
257 | * @return mixed |
||
258 | */ |
||
259 | 6 | private static function getConfigurationFromInitializedTSFE($path) |
|
267 | |||
268 | /** |
||
269 | * This function is used to retrieve the configuration from an existing TSFE instance |
||
270 | * @param $pageId |
||
271 | * @param $path |
||
272 | * @param $language |
||
273 | * @return mixed |
||
274 | */ |
||
275 | 73 | private static function getConfigurationFromExistingTSFE($pageId, $path, $language) |
|
319 | |||
320 | /** |
||
321 | * Initializes the TSFE for a given page ID and language. |
||
322 | * |
||
323 | * @param int $pageId The page id to initialize the TSFE for |
||
324 | * @param int $language System language uid, optional, defaults to 0 |
||
325 | * @param bool $useCache Use cache to reuse TSFE |
||
326 | * @return void |
||
327 | */ |
||
328 | 21 | public static function initializeTsfe( |
|
393 | |||
394 | /** |
||
395 | * Check if record ($table, $uid) is a workspace record |
||
396 | * |
||
397 | * @param string $table The table the record belongs to |
||
398 | * @param int $uid The record's uid |
||
399 | * @return bool TRUE if the record is in a draft workspace, FALSE if it's a LIVE record |
||
400 | */ |
||
401 | 39 | public static function isDraftRecord($table, $uid) |
|
415 | |||
416 | /** |
||
417 | * Checks whether a record is a localization overlay. |
||
418 | * |
||
419 | * @param string $tableName The record's table name |
||
420 | * @param array $record The record to check |
||
421 | * @return bool TRUE if the record is a language overlay, FALSE otherwise |
||
422 | */ |
||
423 | 32 | public static function isLocalizedRecord($tableName, array $record) |
|
437 | |||
438 | /** |
||
439 | * Check if the page type of a page record is allowed |
||
440 | * |
||
441 | * @param array $pageRecord The pages database row |
||
442 | * @param string $configurationName The name of the configuration to use. |
||
443 | * |
||
444 | * @return bool TRUE if the page type is allowed, otherwise FALSE |
||
445 | */ |
||
446 | 30 | public static function isAllowedPageType(array $pageRecord, $configurationName = 'pages') |
|
458 | |||
459 | /** |
||
460 | * Get allowed page types |
||
461 | * |
||
462 | * @param int $pageId Page ID |
||
463 | * @param string $configurationName The name of the configuration to use. |
||
464 | * |
||
465 | * @return array Allowed page types to compare to a doktype of a page record |
||
466 | */ |
||
467 | 30 | public static function getAllowedPageTypes($pageId, $configurationName = 'pages') |
|
473 | |||
474 | /** |
||
475 | * Resolves the configured absRefPrefix to a valid value and resolved if absRefPrefix |
||
476 | * is set to "auto". |
||
477 | * |
||
478 | * @param TypoScriptFrontendController $TSFE |
||
479 | * @return string |
||
480 | */ |
||
481 | 21 | public static function getAbsRefPrefixFromTSFE(TypoScriptFrontendController $TSFE) |
|
495 | |||
496 | /** |
||
497 | * This function can be used to check if one of the strings in needles is |
||
498 | * contained in the haystack. |
||
499 | * |
||
500 | * |
||
501 | * Example: |
||
502 | * |
||
503 | * haystack: the brown fox |
||
504 | * needles: ['hello', 'world'] |
||
505 | * result: false |
||
506 | * |
||
507 | * haystack: the brown fox |
||
508 | * needles: ['is', 'fox'] |
||
509 | * result: true |
||
510 | * |
||
511 | * @param string $haystack |
||
512 | * @param array $needles |
||
513 | * @return bool |
||
514 | */ |
||
515 | 40 | public static function containsOneOfTheStrings($haystack, array $needles) |
|
526 | } |
||
527 |