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 |
||
| 44 | class Util |
||
| 45 | { |
||
| 46 | const SOLR_ISO_DATETIME_FORMAT = 'Y-m-d\TH:i:s\Z'; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Generates a document id for documents representing page records. |
||
| 50 | * |
||
| 51 | * @param int $uid The page's uid |
||
| 52 | * @param int $typeNum The page's typeNum |
||
| 53 | * @param int $language the language id, defaults to 0 |
||
| 54 | * @param string $accessGroups comma separated list of uids of groups that have access to that page |
||
| 55 | * @param string $mountPointParameter The mount point parameter that is used to access the page. |
||
| 56 | * @return string The document id for that page |
||
| 57 | */ |
||
| 58 | 32 | public static function getPageDocumentId( |
|
| 80 | |||
| 81 | /** |
||
| 82 | * Generates a document id in the form $siteHash/$type/$uid. |
||
| 83 | * |
||
| 84 | * @param string $table The records table name |
||
| 85 | * @param int $pid The record's pid |
||
| 86 | * @param int $uid The record's uid |
||
| 87 | * @param string $additionalIdParameters Additional ID parameters |
||
| 88 | * @return string A document id |
||
| 89 | */ |
||
| 90 | 42 | public static function getDocumentId( |
|
| 105 | |||
| 106 | /** |
||
| 107 | * Converts a date from unix timestamp to ISO 8601 format. |
||
| 108 | * |
||
| 109 | * @param int $timestamp unix timestamp |
||
| 110 | * @return string the date in ISO 8601 format |
||
| 111 | */ |
||
| 112 | 45 | public static function timestampToIso($timestamp) |
|
| 116 | |||
| 117 | /** |
||
| 118 | * Converts a date from ISO 8601 format to unix timestamp. |
||
| 119 | * |
||
| 120 | * @param string $isoTime date in ISO 8601 format |
||
| 121 | * @return int unix timestamp |
||
| 122 | */ |
||
| 123 | 18 | public static function isoToTimestamp($isoTime) |
|
| 129 | |||
| 130 | /** |
||
| 131 | * Converts a date from unix timestamp to ISO 8601 format in UTC timezone. |
||
| 132 | * |
||
| 133 | * @param int $timestamp unix timestamp |
||
| 134 | * @return string the date in ISO 8601 format |
||
| 135 | */ |
||
| 136 | public static function timestampToUtcIso($timestamp) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Converts a date from ISO 8601 format in UTC timezone to unix timestamp. |
||
| 143 | * |
||
| 144 | * @param string $isoTime date in ISO 8601 format |
||
| 145 | * @return int unix timestamp |
||
| 146 | */ |
||
| 147 | public static function utcIsoToTimestamp($isoTime) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Returns given word as CamelCased. |
||
| 157 | * |
||
| 158 | * Converts a word like "send_email" to "SendEmail". It |
||
| 159 | * will remove non alphanumeric characters from the word, so |
||
| 160 | * "who's online" will be converted to "WhoSOnline" |
||
| 161 | * |
||
| 162 | * @param string $word Word to convert to camel case |
||
| 163 | * @return string UpperCamelCasedWord |
||
| 164 | */ |
||
| 165 | public static function camelize($word) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Returns a given CamelCasedString as an lowercase string with underscores. |
||
| 173 | * Example: Converts BlogExample to blog_example, and minimalValue to minimal_value |
||
| 174 | * |
||
| 175 | * @param string $string String to be converted to lowercase underscore |
||
| 176 | * @return string lowercase_and_underscored_string |
||
| 177 | */ |
||
| 178 | public static function camelCaseToLowerCaseUnderscored($string) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Returns a given string with underscores as UpperCamelCase. |
||
| 185 | * Example: Converts blog_example to BlogExample |
||
| 186 | * |
||
| 187 | * @param string $string String to be converted to camel case |
||
| 188 | * @return string UpperCamelCasedWord |
||
| 189 | */ |
||
| 190 | 26 | public static function underscoredToUpperCamelCase($string) |
|
| 195 | |||
| 196 | /** |
||
| 197 | * Shortcut to retrieve the TypoScript configuration for EXT:solr |
||
| 198 | * (plugin.tx_solr) from TSFE. |
||
| 199 | * |
||
| 200 | * @return TypoScriptConfiguration |
||
| 201 | */ |
||
| 202 | 133 | public static function getSolrConfiguration() |
|
| 208 | |||
| 209 | /** |
||
| 210 | * @return ConfigurationManager |
||
| 211 | */ |
||
| 212 | 156 | private static function getConfigurationManager() |
|
| 218 | |||
| 219 | /** |
||
| 220 | * Gets the Solr configuration for a specific root page id. |
||
| 221 | * To be used from the backend. |
||
| 222 | * |
||
| 223 | * @param int $pageId Id of the (root) page to get the Solr configuration from. |
||
| 224 | * @param bool $initializeTsfe Optionally initializes a full TSFE to get the configuration, defaults to FALSE |
||
| 225 | * @param int $language System language uid, optional, defaults to 0 |
||
| 226 | * @return TypoScriptConfiguration The Solr configuration for the requested tree. |
||
| 227 | */ |
||
| 228 | 35 | public static function getSolrConfigurationFromPageId( |
|
| 236 | |||
| 237 | /** |
||
| 238 | * Loads the TypoScript configuration for a given page id and language. |
||
| 239 | * Language usage may be disabled to get the default TypoScript |
||
| 240 | * configuration. |
||
| 241 | * |
||
| 242 | * @param int $pageId Id of the (root) page to get the Solr configuration from. |
||
| 243 | * @param string $path The TypoScript configuration path to retrieve. |
||
| 244 | * @param bool $initializeTsfe Optionally initializes a full TSFE to get the configuration, defaults to FALSE |
||
| 245 | * @param int|bool $language System language uid or FALSE to disable language usage, optional, defaults to 0 |
||
| 246 | * @param bool $useTwoLevelCache Flag to enable the two level cache for the typoscript configuration array |
||
| 247 | * @return TypoScriptConfiguration The Solr configuration for the requested tree. |
||
| 248 | */ |
||
| 249 | 38 | public static function getConfigurationFromPageId( |
|
| 289 | |||
| 290 | |||
| 291 | /** |
||
| 292 | * Initializes a TSFE, if required and builds an configuration array, containing the solr configuration. |
||
| 293 | * |
||
| 294 | * @param integer $pageId |
||
| 295 | * @param string $path |
||
| 296 | * @param boolean $initializeTsfe |
||
| 297 | * @param integer $language |
||
| 298 | * @return array |
||
| 299 | */ |
||
| 300 | 37 | protected static function buildConfigurationArray($pageId, $path, $initializeTsfe, $language) |
|
| 311 | |||
| 312 | /** |
||
| 313 | * Builds the configuration object from a config array and returns it. |
||
| 314 | * |
||
| 315 | * @param array $configurationToUse |
||
| 316 | * @param int $pageId |
||
| 317 | * @param int $languageId |
||
| 318 | * @param string $typoScriptPath |
||
| 319 | * @return TypoScriptConfiguration |
||
| 320 | */ |
||
| 321 | 38 | protected static function buildTypoScriptConfigurationFromArray(array $configurationToUse, $pageId, $languageId, $typoScriptPath) |
|
| 326 | |||
| 327 | /** |
||
| 328 | * This function is used to retrieve the configuration from a previous initialized TSFE |
||
| 329 | * (see: getConfigurationFromPageId) |
||
| 330 | * |
||
| 331 | * @param string $path |
||
| 332 | * @return mixed |
||
| 333 | */ |
||
| 334 | 1 | private static function getConfigurationFromInitializedTSFE($path) |
|
| 342 | |||
| 343 | /** |
||
| 344 | * This function is used to retrieve the configuration from an existing TSFE instance |
||
| 345 | * @param $pageId |
||
| 346 | * @param $path |
||
| 347 | * @param $language |
||
| 348 | * @return mixed |
||
| 349 | */ |
||
| 350 | 37 | private static function getConfigurationFromExistingTSFE($pageId, $path, $language) |
|
| 394 | |||
| 395 | /** |
||
| 396 | * Initializes the TSFE for a given page ID and language. |
||
| 397 | * |
||
| 398 | * @param int $pageId The page id to initialize the TSFE for |
||
| 399 | * @param int $language System language uid, optional, defaults to 0 |
||
| 400 | * @param bool $useCache Use cache to reuse TSFE |
||
| 401 | * @return void |
||
| 402 | */ |
||
| 403 | 14 | public static function initializeTsfe( |
|
| 468 | |||
| 469 | /** |
||
| 470 | * Determines the rootpage ID for a given page. |
||
| 471 | * |
||
| 472 | * @param int $pageId A page ID somewhere in a tree. |
||
| 473 | * @param bool $forceFallback Force the explicit detection and do not use the current frontend root line |
||
| 474 | * @return int The page's tree branch's root page ID |
||
| 475 | */ |
||
| 476 | 60 | public static function getRootPageId($pageId = 0, $forceFallback = false) |
|
| 497 | |||
| 498 | /** |
||
| 499 | * Takes a page Id and checks whether the page is marked as root page. |
||
| 500 | * |
||
| 501 | * @param int $pageId Page ID |
||
| 502 | * @return bool TRUE if the page is marked as root page, FALSE otherwise |
||
| 503 | */ |
||
| 504 | 25 | public static function isRootPage($pageId) |
|
| 515 | |||
| 516 | /** |
||
| 517 | * Gets the site hash for a domain |
||
| 518 | * |
||
| 519 | * @param string $domain Domain to calculate the site hash for. |
||
| 520 | * @return string site hash for $domain |
||
| 521 | */ |
||
| 522 | 45 | public static function getSiteHashForDomain($domain) |
|
| 537 | |||
| 538 | /** |
||
| 539 | * Resolves magic keywords in allowed sites configuration. |
||
| 540 | * Supported keywords: |
||
| 541 | * __solr_current_site - The domain of the site the query has been started from |
||
| 542 | * __current_site - Same as __solr_current_site |
||
| 543 | * __all - Adds all domains as allowed sites |
||
| 544 | * * - Same as __all |
||
| 545 | * |
||
| 546 | * @param int $pageId A page ID that is then resolved to the site it belongs to |
||
| 547 | * @param string $allowedSitesConfiguration TypoScript setting for allowed sites |
||
| 548 | * @return string List of allowed sites/domains, magic keywords resolved |
||
| 549 | */ |
||
| 550 | 24 | public static function resolveSiteHashAllowedSites( |
|
| 572 | |||
| 573 | /** |
||
| 574 | * Check if record ($table, $uid) is a workspace record |
||
| 575 | * |
||
| 576 | * @param string $table The table the record belongs to |
||
| 577 | * @param int $uid The record's uid |
||
| 578 | * @return bool TRUE if the record is in a draft workspace, FALSE if it's a LIVE record |
||
| 579 | */ |
||
| 580 | 18 | public static function isDraftRecord($table, $uid) |
|
| 594 | |||
| 595 | /** |
||
| 596 | * Checks whether a record is a localization overlay. |
||
| 597 | * |
||
| 598 | * @param string $tableName The record's table name |
||
| 599 | * @param array $record The record to check |
||
| 600 | * @return bool TRUE if the record is a language overlay, FALSE otherwise |
||
| 601 | */ |
||
| 602 | 13 | public static function isLocalizedRecord($tableName, array $record) |
|
| 616 | |||
| 617 | /** |
||
| 618 | * Check if the page type of a page record is allowed |
||
| 619 | * |
||
| 620 | * @param array $pageRecord The pages database row |
||
| 621 | * @param string $configurationName The name of the configuration to use. |
||
| 622 | * |
||
| 623 | * @return bool TRUE if the page type is allowed, otherwise FALSE |
||
| 624 | */ |
||
| 625 | 15 | public static function isAllowedPageType(array $pageRecord, $configurationName = 'pages') |
|
| 637 | |||
| 638 | /** |
||
| 639 | * Get allowed page types |
||
| 640 | * |
||
| 641 | * @param int $pageId Page ID |
||
| 642 | * @param string $configurationName The name of the configuration to use. |
||
| 643 | * |
||
| 644 | * @return array Allowed page types to compare to a doktype of a page record |
||
| 645 | */ |
||
| 646 | 15 | public static function getAllowedPageTypes($pageId, $configurationName = 'pages') |
|
| 652 | |||
| 653 | /** |
||
| 654 | * Method to check if a page exists. |
||
| 655 | * |
||
| 656 | * @param int $pageId |
||
| 657 | * @return bool |
||
| 658 | */ |
||
| 659 | public static function pageExists($pageId) |
||
| 669 | |||
| 670 | /** |
||
| 671 | * Resolves the configured absRefPrefix to a valid value and resolved if absRefPrefix |
||
| 672 | * is set to "auto". |
||
| 673 | * |
||
| 674 | * @param TypoScriptFrontendController $TSFE |
||
| 675 | * @return string |
||
| 676 | */ |
||
| 677 | 14 | public static function getAbsRefPrefixFromTSFE(TypoScriptFrontendController $TSFE) |
|
| 691 | |||
| 692 | /** |
||
| 693 | * This function can be used to check if one of the strings in needles is |
||
| 694 | * contained in the haystack. |
||
| 695 | * |
||
| 696 | * |
||
| 697 | * Example: |
||
| 698 | * |
||
| 699 | * haystack: the brown fox |
||
| 700 | * needles: ['hello', 'world'] |
||
| 701 | * result: false |
||
| 702 | * |
||
| 703 | * haystack: the brown fox |
||
| 704 | * needles: ['is', 'fox'] |
||
| 705 | * result: true |
||
| 706 | * |
||
| 707 | * @param string $haystack |
||
| 708 | * @param array $needles |
||
| 709 | * @return bool |
||
| 710 | */ |
||
| 711 | 33 | public static function containsOneOfTheStrings($haystack, array $needles) |
|
| 722 | } |
||
| 723 |