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 |
||
46 | class Util |
||
47 | { |
||
48 | const SOLR_ISO_DATETIME_FORMAT = 'Y-m-d\TH:i:s\Z'; |
||
49 | |||
50 | /** |
||
51 | * Generates a document id for documents representing page records. |
||
52 | * |
||
53 | * @param int $uid The page's uid |
||
54 | * @param int $typeNum The page's typeNum |
||
55 | * @param int $language the language id, defaults to 0 |
||
56 | * @param string $accessGroups comma separated list of uids of groups that have access to that page |
||
57 | * @param string $mountPointParameter The mount point parameter that is used to access the page. |
||
58 | * @return string The document id for that page |
||
59 | 42 | */ |
|
60 | public static function getPageDocumentId( |
||
82 | |||
83 | /** |
||
84 | * Generates a document id in the form $siteHash/$type/$uid. |
||
85 | * |
||
86 | * @param string $table The records table name |
||
87 | * @param int $pid The record's pid |
||
88 | * @param int $uid The record's uid |
||
89 | * @param string $additionalIdParameters Additional ID parameters |
||
90 | * @return string A document id |
||
91 | 53 | */ |
|
92 | public static function getDocumentId( |
||
107 | |||
108 | /** |
||
109 | * Converts a date from unix timestamp to ISO 8601 format. |
||
110 | * |
||
111 | * @param int $timestamp unix timestamp |
||
112 | * @return string the date in ISO 8601 format |
||
113 | 56 | */ |
|
114 | public static function timestampToIso($timestamp) |
||
118 | |||
119 | /** |
||
120 | * Converts a date from ISO 8601 format to unix timestamp. |
||
121 | * |
||
122 | * @param string $isoTime date in ISO 8601 format |
||
123 | * @return int unix timestamp |
||
124 | 18 | */ |
|
125 | public static function isoToTimestamp($isoTime) |
||
131 | |||
132 | /** |
||
133 | * Converts a date from unix timestamp to ISO 8601 format in UTC timezone. |
||
134 | * |
||
135 | * @param int $timestamp unix timestamp |
||
136 | * @return string the date in ISO 8601 format |
||
137 | */ |
||
138 | public static function timestampToUtcIso($timestamp) |
||
142 | |||
143 | /** |
||
144 | * Converts a date from ISO 8601 format in UTC timezone to unix timestamp. |
||
145 | * |
||
146 | * @param string $isoTime date in ISO 8601 format |
||
147 | * @return int unix timestamp |
||
148 | */ |
||
149 | public static function utcIsoToTimestamp($isoTime) |
||
156 | |||
157 | /** |
||
158 | * Returns given word as CamelCased. |
||
159 | * |
||
160 | * Converts a word like "send_email" to "SendEmail". It |
||
161 | * will remove non alphanumeric characters from the word, so |
||
162 | * "who's online" will be converted to "WhoSOnline" |
||
163 | * |
||
164 | * @param string $word Word to convert to camel case |
||
165 | * @return string UpperCamelCasedWord |
||
166 | */ |
||
167 | public static function camelize($word) |
||
172 | |||
173 | /** |
||
174 | * Returns a given CamelCasedString as an lowercase string with underscores. |
||
175 | * Example: Converts BlogExample to blog_example, and minimalValue to minimal_value |
||
176 | * |
||
177 | * @param string $string String to be converted to lowercase underscore |
||
178 | * @return string lowercase_and_underscored_string |
||
179 | */ |
||
180 | public static function camelCaseToLowerCaseUnderscored($string) |
||
184 | |||
185 | /** |
||
186 | * Returns a given string with underscores as UpperCamelCase. |
||
187 | * Example: Converts blog_example to BlogExample |
||
188 | * |
||
189 | * @param string $string String to be converted to camel case |
||
190 | * @return string UpperCamelCasedWord |
||
191 | 26 | */ |
|
192 | public static function underscoredToUpperCamelCase($string) |
||
197 | |||
198 | /** |
||
199 | * Shortcut to retrieve the TypoScript configuration for EXT:solr |
||
200 | * (plugin.tx_solr) from TSFE. |
||
201 | * |
||
202 | * @return TypoScriptConfiguration |
||
203 | 148 | */ |
|
204 | public static function getSolrConfiguration() |
||
210 | |||
211 | /** |
||
212 | * @return ConfigurationManager |
||
213 | 184 | */ |
|
214 | private static function getConfigurationManager() |
||
220 | |||
221 | /** |
||
222 | * Gets the Solr configuration for a specific root page id. |
||
223 | * To be used from the backend. |
||
224 | * |
||
225 | * @param int $pageId Id of the (root) page to get the Solr configuration from. |
||
226 | * @param bool $initializeTsfe Optionally initializes a full TSFE to get the configuration, defaults to FALSE |
||
227 | * @param int $language System language uid, optional, defaults to 0 |
||
228 | * @return TypoScriptConfiguration The Solr configuration for the requested tree. |
||
229 | 59 | */ |
|
230 | public static function getSolrConfigurationFromPageId( |
||
238 | |||
239 | /** |
||
240 | * Loads the TypoScript configuration for a given page id and language. |
||
241 | * Language usage may be disabled to get the default TypoScript |
||
242 | * configuration. |
||
243 | * |
||
244 | * @param int $pageId Id of the (root) page to get the Solr configuration from. |
||
245 | * @param string $path The TypoScript configuration path to retrieve. |
||
246 | * @param bool $initializeTsfe Optionally initializes a full TSFE to get the configuration, defaults to FALSE |
||
247 | * @param int|bool $language System language uid or FALSE to disable language usage, optional, defaults to 0 |
||
248 | * @param bool $useTwoLevelCache Flag to enable the two level cache for the typoscript configuration array |
||
249 | * @return TypoScriptConfiguration The Solr configuration for the requested tree. |
||
250 | 61 | */ |
|
251 | public static function getConfigurationFromPageId( |
||
291 | |||
292 | |||
293 | /** |
||
294 | * Initializes a TSFE, if required and builds an configuration array, containing the solr configuration. |
||
295 | * |
||
296 | * @param integer $pageId |
||
297 | * @param string $path |
||
298 | * @param boolean $initializeTsfe |
||
299 | * @param integer $language |
||
300 | * @return array |
||
301 | 60 | */ |
|
302 | protected static function buildConfigurationArray($pageId, $path, $initializeTsfe, $language) |
||
313 | |||
314 | /** |
||
315 | * Builds the configuration object from a config array and returns it. |
||
316 | * |
||
317 | * @param array $configurationToUse |
||
318 | * @param int $pageId |
||
319 | * @param int $languageId |
||
320 | * @param string $typoScriptPath |
||
321 | * @return TypoScriptConfiguration |
||
322 | 61 | */ |
|
323 | protected static function buildTypoScriptConfigurationFromArray(array $configurationToUse, $pageId, $languageId, $typoScriptPath) |
||
328 | |||
329 | /** |
||
330 | * This function is used to retrieve the configuration from a previous initialized TSFE |
||
331 | * (see: getConfigurationFromPageId) |
||
332 | * |
||
333 | * @param string $path |
||
334 | * @return mixed |
||
335 | 2 | */ |
|
336 | private static function getConfigurationFromInitializedTSFE($path) |
||
344 | |||
345 | /** |
||
346 | * This function is used to retrieve the configuration from an existing TSFE instance |
||
347 | * @param $pageId |
||
348 | * @param $path |
||
349 | * @param $language |
||
350 | * @return mixed |
||
351 | 60 | */ |
|
352 | private static function getConfigurationFromExistingTSFE($pageId, $path, $language) |
||
396 | |||
397 | /** |
||
398 | * Initializes the TSFE for a given page ID and language. |
||
399 | * |
||
400 | * @param int $pageId The page id to initialize the TSFE for |
||
401 | * @param int $language System language uid, optional, defaults to 0 |
||
402 | * @param bool $useCache Use cache to reuse TSFE |
||
403 | * @return void |
||
404 | 15 | */ |
|
405 | public static function initializeTsfe( |
||
470 | |||
471 | /** |
||
472 | * Determines the rootpage ID for a given page. |
||
473 | * |
||
474 | * @param int $pageId A page ID somewhere in a tree. |
||
475 | * @param bool $forceFallback Force the explicit detection and do not use the current frontend root line |
||
476 | * @return int The page's tree branch's root page ID |
||
477 | 90 | */ |
|
478 | public static function getRootPageId($pageId = 0, $forceFallback = false) |
||
499 | |||
500 | /** |
||
501 | * Takes a page Id and checks whether the page is marked as root page. |
||
502 | * |
||
503 | * @param int $pageId Page ID |
||
504 | * @return bool TRUE if the page is marked as root page, FALSE otherwise |
||
505 | 51 | * @deprecated since 6.1 will be removed in 7.0 |
|
506 | */ |
||
507 | 51 | public static function isRootPage($pageId) |
|
514 | |||
515 | 51 | /** |
|
516 | * Gets the site hash for a domain |
||
517 | 51 | * |
|
518 | 1 | * @deprecated since 6.1 will be removed in 7.0. use SiteHashService->getSiteHashForDomain now. |
|
519 | 1 | * @param string $domain Domain to calculate the site hash for. |
|
520 | 1 | * @return string site hash for $domain |
|
521 | 1 | */ |
|
522 | public static function getSiteHashForDomain($domain) |
||
529 | |||
530 | /** |
||
531 | * Resolves magic keywords in allowed sites configuration. |
||
532 | * Supported keywords: |
||
533 | * __solr_current_site - The domain of the site the query has been started from |
||
534 | * __current_site - Same as __solr_current_site |
||
535 | * __all - Adds all domains as allowed sites |
||
536 | * * - Means all sites are allowed, same as no siteHash |
||
537 | * |
||
538 | * @deprecated since 6.1 will be removed in 7.0. use SiteHashService->getAllowedSitesForPageIdAndAllowedSitesConfiguration now. |
||
539 | * @param int $pageId A page ID that is then resolved to the site it belongs to |
||
540 | * @param string $allowedSitesConfiguration TypoScript setting for allowed sites |
||
541 | * @return string List of allowed sites/domains, magic keywords resolved |
||
542 | */ |
||
543 | public static function resolveSiteHashAllowedSites($pageId, $allowedSitesConfiguration) |
||
550 | |||
551 | /** |
||
552 | * Check if record ($table, $uid) is a workspace record |
||
553 | * |
||
554 | * @param string $table The table the record belongs to |
||
555 | * @param int $uid The record's uid |
||
556 | * @return bool TRUE if the record is in a draft workspace, FALSE if it's a LIVE record |
||
557 | */ |
||
558 | public static function isDraftRecord($table, $uid) |
||
572 | |||
573 | /** |
||
574 | 35 | * Checks whether a record is a localization overlay. |
|
575 | * |
||
576 | 35 | * @param string $tableName The record's table name |
|
577 | * @param array $record The record to check |
||
578 | 35 | * @return bool TRUE if the record is a language overlay, FALSE otherwise |
|
579 | */ |
||
580 | public static function isLocalizedRecord($tableName, array $record) |
||
594 | |||
595 | /** |
||
596 | 28 | * Check if the page type of a page record is allowed |
|
597 | * |
||
598 | 28 | * @param array $pageRecord The pages database row |
|
599 | * @param string $configurationName The name of the configuration to use. |
||
600 | 28 | * |
|
601 | 6 | * @return bool TRUE if the page type is allowed, otherwise FALSE |
|
602 | */ |
||
603 | 6 | public static function isAllowedPageType(array $pageRecord, $configurationName = 'pages') |
|
615 | |||
616 | /** |
||
617 | * Get allowed page types |
||
618 | * |
||
619 | 27 | * @param int $pageId Page ID |
|
620 | * @param string $configurationName The name of the configuration to use. |
||
621 | 27 | * |
|
622 | 27 | * @return array Allowed page types to compare to a doktype of a page record |
|
623 | 27 | */ |
|
624 | public static function getAllowedPageTypes($pageId, $configurationName = 'pages') |
||
630 | |||
631 | /** |
||
632 | * Method to check if a page exists. |
||
633 | * |
||
634 | * @param int $pageId |
||
635 | * @return bool |
||
636 | */ |
||
637 | public static function pageExists($pageId) |
||
647 | |||
648 | /** |
||
649 | * Resolves the configured absRefPrefix to a valid value and resolved if absRefPrefix |
||
650 | * is set to "auto". |
||
651 | * |
||
652 | * @param TypoScriptFrontendController $TSFE |
||
653 | * @return string |
||
654 | */ |
||
655 | public static function getAbsRefPrefixFromTSFE(TypoScriptFrontendController $TSFE) |
||
669 | |||
670 | /** |
||
671 | 15 | * This function can be used to check if one of the strings in needles is |
|
672 | * contained in the haystack. |
||
673 | 15 | * |
|
674 | 15 | * |
|
675 | 12 | * Example: |
|
676 | * |
||
677 | * haystack: the brown fox |
||
678 | 3 | * needles: ['hello', 'world'] |
|
679 | 3 | * result: false |
|
680 | 1 | * |
|
681 | * haystack: the brown fox |
||
682 | * needles: ['is', 'fox'] |
||
683 | 3 | * result: true |
|
684 | * |
||
685 | * @param string $haystack |
||
686 | * @param array $needles |
||
687 | * @return bool |
||
688 | */ |
||
689 | public static function containsOneOfTheStrings($haystack, array $needles) |
||
700 | } |
||
701 |