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 |
||
| 45 | class Util |
||
| 46 | { |
||
| 47 | const SOLR_ISO_DATETIME_FORMAT = 'Y-m-d\TH:i:s\Z'; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Generates a document id for documents representing page records. |
||
| 51 | * |
||
| 52 | * @param int $uid The page's uid |
||
| 53 | * @param int $typeNum The page's typeNum |
||
| 54 | * @param int $language the language id, defaults to 0 |
||
| 55 | * @param string $accessGroups comma separated list of uids of groups that have access to that page |
||
| 56 | * @param string $mountPointParameter The mount point parameter that is used to access the page. |
||
| 57 | * @return string The document id for that page |
||
| 58 | */ |
||
| 59 | 42 | public static function getPageDocumentId( |
|
| 81 | |||
| 82 | /** |
||
| 83 | * Generates a document id in the form $siteHash/$type/$uid. |
||
| 84 | * |
||
| 85 | * @param string $table The records table name |
||
| 86 | * @param int $pid The record's pid |
||
| 87 | * @param int $uid The record's uid |
||
| 88 | * @param string $additionalIdParameters Additional ID parameters |
||
| 89 | * @return string A document id |
||
| 90 | */ |
||
| 91 | 53 | public static function getDocumentId( |
|
| 106 | |||
| 107 | /** |
||
| 108 | * Converts a date from unix timestamp to ISO 8601 format. |
||
| 109 | * |
||
| 110 | * @param int $timestamp unix timestamp |
||
| 111 | * @return string the date in ISO 8601 format |
||
| 112 | */ |
||
| 113 | 56 | public static function timestampToIso($timestamp) |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Converts a date from ISO 8601 format to unix timestamp. |
||
| 120 | * |
||
| 121 | * @param string $isoTime date in ISO 8601 format |
||
| 122 | * @return int unix timestamp |
||
| 123 | */ |
||
| 124 | 18 | public static function isoToTimestamp($isoTime) |
|
| 130 | |||
| 131 | /** |
||
| 132 | * Converts a date from unix timestamp to ISO 8601 format in UTC timezone. |
||
| 133 | * |
||
| 134 | * @param int $timestamp unix timestamp |
||
| 135 | * @return string the date in ISO 8601 format |
||
| 136 | */ |
||
| 137 | public static function timestampToUtcIso($timestamp) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Converts a date from ISO 8601 format in UTC timezone to unix timestamp. |
||
| 144 | * |
||
| 145 | * @param string $isoTime date in ISO 8601 format |
||
| 146 | * @return int unix timestamp |
||
| 147 | */ |
||
| 148 | public static function utcIsoToTimestamp($isoTime) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Returns given word as CamelCased. |
||
| 158 | * |
||
| 159 | * Converts a word like "send_email" to "SendEmail". It |
||
| 160 | * will remove non alphanumeric characters from the word, so |
||
| 161 | * "who's online" will be converted to "WhoSOnline" |
||
| 162 | * |
||
| 163 | * @param string $word Word to convert to camel case |
||
| 164 | * @return string UpperCamelCasedWord |
||
| 165 | */ |
||
| 166 | public static function camelize($word) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Returns a given CamelCasedString as an lowercase string with underscores. |
||
| 174 | * Example: Converts BlogExample to blog_example, and minimalValue to minimal_value |
||
| 175 | * |
||
| 176 | * @param string $string String to be converted to lowercase underscore |
||
| 177 | * @return string lowercase_and_underscored_string |
||
| 178 | */ |
||
| 179 | public static function camelCaseToLowerCaseUnderscored($string) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Returns a given string with underscores as UpperCamelCase. |
||
| 186 | * Example: Converts blog_example to BlogExample |
||
| 187 | * |
||
| 188 | * @param string $string String to be converted to camel case |
||
| 189 | * @return string UpperCamelCasedWord |
||
| 190 | */ |
||
| 191 | 26 | public static function underscoredToUpperCamelCase($string) |
|
| 196 | |||
| 197 | /** |
||
| 198 | * Shortcut to retrieve the TypoScript configuration for EXT:solr |
||
| 199 | * (plugin.tx_solr) from TSFE. |
||
| 200 | * |
||
| 201 | * @return TypoScriptConfiguration |
||
| 202 | */ |
||
| 203 | 148 | public static function getSolrConfiguration() |
|
| 209 | |||
| 210 | /** |
||
| 211 | * @return ConfigurationManager |
||
| 212 | */ |
||
| 213 | 184 | private static function getConfigurationManager() |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Gets the Solr configuration for a specific root page id. |
||
| 222 | * To be used from the backend. |
||
| 223 | * |
||
| 224 | * @param int $pageId Id of the (root) page to get the Solr configuration from. |
||
| 225 | * @param bool $initializeTsfe Optionally initializes a full TSFE to get the configuration, defaults to FALSE |
||
| 226 | * @param int $language System language uid, optional, defaults to 0 |
||
| 227 | * @return TypoScriptConfiguration The Solr configuration for the requested tree. |
||
| 228 | */ |
||
| 229 | 59 | public static function getSolrConfigurationFromPageId( |
|
| 237 | |||
| 238 | /** |
||
| 239 | * Loads the TypoScript configuration for a given page id and language. |
||
| 240 | * Language usage may be disabled to get the default TypoScript |
||
| 241 | * configuration. |
||
| 242 | * |
||
| 243 | * @param int $pageId Id of the (root) page to get the Solr configuration from. |
||
| 244 | * @param string $path The TypoScript configuration path to retrieve. |
||
| 245 | * @param bool $initializeTsfe Optionally initializes a full TSFE to get the configuration, defaults to FALSE |
||
| 246 | * @param int|bool $language System language uid or FALSE to disable language usage, optional, defaults to 0 |
||
| 247 | * @param bool $useTwoLevelCache Flag to enable the two level cache for the typoscript configuration array |
||
| 248 | * @return TypoScriptConfiguration The Solr configuration for the requested tree. |
||
| 249 | */ |
||
| 250 | 61 | public static function getConfigurationFromPageId( |
|
| 290 | |||
| 291 | |||
| 292 | /** |
||
| 293 | * Initializes a TSFE, if required and builds an configuration array, containing the solr configuration. |
||
| 294 | * |
||
| 295 | * @param integer $pageId |
||
| 296 | * @param string $path |
||
| 297 | * @param boolean $initializeTsfe |
||
| 298 | * @param integer $language |
||
| 299 | * @return array |
||
| 300 | */ |
||
| 301 | 60 | protected static function buildConfigurationArray($pageId, $path, $initializeTsfe, $language) |
|
| 312 | |||
| 313 | /** |
||
| 314 | * Builds the configuration object from a config array and returns it. |
||
| 315 | * |
||
| 316 | * @param array $configurationToUse |
||
| 317 | * @param int $pageId |
||
| 318 | * @param int $languageId |
||
| 319 | * @param string $typoScriptPath |
||
| 320 | * @return TypoScriptConfiguration |
||
| 321 | */ |
||
| 322 | 61 | protected static function buildTypoScriptConfigurationFromArray(array $configurationToUse, $pageId, $languageId, $typoScriptPath) |
|
| 327 | |||
| 328 | /** |
||
| 329 | * This function is used to retrieve the configuration from a previous initialized TSFE |
||
| 330 | * (see: getConfigurationFromPageId) |
||
| 331 | * |
||
| 332 | * @param string $path |
||
| 333 | * @return mixed |
||
| 334 | */ |
||
| 335 | 2 | private static function getConfigurationFromInitializedTSFE($path) |
|
| 343 | |||
| 344 | /** |
||
| 345 | * This function is used to retrieve the configuration from an existing TSFE instance |
||
| 346 | * @param $pageId |
||
| 347 | * @param $path |
||
| 348 | * @param $language |
||
| 349 | * @return mixed |
||
| 350 | */ |
||
| 351 | 60 | private static function getConfigurationFromExistingTSFE($pageId, $path, $language) |
|
| 395 | |||
| 396 | /** |
||
| 397 | * Initializes the TSFE for a given page ID and language. |
||
| 398 | * |
||
| 399 | * @param int $pageId The page id to initialize the TSFE for |
||
| 400 | * @param int $language System language uid, optional, defaults to 0 |
||
| 401 | * @param bool $useCache Use cache to reuse TSFE |
||
| 402 | * @return void |
||
| 403 | */ |
||
| 404 | 15 | public static function initializeTsfe( |
|
| 469 | |||
| 470 | /** |
||
| 471 | * Determines the rootpage ID for a given page. |
||
| 472 | * |
||
| 473 | * @param int $pageId A page ID somewhere in a tree. |
||
| 474 | * @param bool $forceFallback Force the explicit detection and do not use the current frontend root line |
||
| 475 | * @return int The page's tree branch's root page ID |
||
| 476 | * @deprecated since 6.1 will be removed in 7.0 |
||
| 477 | 90 | */ |
|
| 478 | public function getRootPageId($pageId = 0, $forceFallback = false) |
||
| 485 | 54 | ||
| 486 | /** |
||
| 487 | * Takes a page Id and checks whether the page is marked as root page. |
||
| 488 | * |
||
| 489 | 90 | * @param int $pageId Page ID |
|
| 490 | 47 | * @return bool TRUE if the page is marked as root page, FALSE otherwise |
|
| 491 | 47 | * @deprecated since 6.1 will be removed in 7.0 |
|
| 492 | 47 | */ |
|
| 493 | public static function isRootPage($pageId) |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Gets the site hash for a domain |
||
| 503 | * |
||
| 504 | * @deprecated since 6.1 will be removed in 7.0. use SiteHashService->getSiteHashForDomain now. |
||
| 505 | 51 | * @param string $domain Domain to calculate the site hash for. |
|
| 506 | * @return string site hash for $domain |
||
| 507 | 51 | */ |
|
| 508 | 51 | public static function getSiteHashForDomain($domain) |
|
| 515 | 51 | ||
| 516 | /** |
||
| 517 | 51 | * Resolves magic keywords in allowed sites configuration. |
|
| 518 | 1 | * Supported keywords: |
|
| 519 | 1 | * __solr_current_site - The domain of the site the query has been started from |
|
| 520 | 1 | * __current_site - Same as __solr_current_site |
|
| 521 | 1 | * __all - Adds all domains as allowed sites |
|
| 522 | * * - Means all sites are allowed, same as no siteHash |
||
| 523 | * |
||
| 524 | * @deprecated since 6.1 will be removed in 7.0. use SiteHashService->getAllowedSitesForPageIdAndAllowedSitesConfiguration now. |
||
| 525 | 50 | * @param int $pageId A page ID that is then resolved to the site it belongs to |
|
| 526 | 50 | * @param string $allowedSitesConfiguration TypoScript setting for allowed sites |
|
| 527 | * @return string List of allowed sites/domains, magic keywords resolved |
||
| 528 | 50 | */ |
|
| 529 | public static function resolveSiteHashAllowedSites($pageId, $allowedSitesConfiguration) |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Check if record ($table, $uid) is a workspace record |
||
| 539 | * |
||
| 540 | * @param string $table The table the record belongs to |
||
| 541 | * @param int $uid The record's uid |
||
| 542 | * @return bool TRUE if the record is in a draft workspace, FALSE if it's a LIVE record |
||
| 543 | */ |
||
| 544 | public static function isDraftRecord($table, $uid) |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Checks whether a record is a localization overlay. |
||
| 561 | * |
||
| 562 | * @param string $tableName The record's table name |
||
| 563 | * @param array $record The record to check |
||
| 564 | * @return bool TRUE if the record is a language overlay, FALSE otherwise |
||
| 565 | */ |
||
| 566 | public static function isLocalizedRecord($tableName, array $record) |
||
| 580 | |||
| 581 | /** |
||
| 582 | * Check if the page type of a page record is allowed |
||
| 583 | * |
||
| 584 | * @param array $pageRecord The pages database row |
||
| 585 | * @param string $configurationName The name of the configuration to use. |
||
| 586 | 35 | * |
|
| 587 | * @return bool TRUE if the page type is allowed, otherwise FALSE |
||
| 588 | */ |
||
| 589 | public static function isAllowedPageType(array $pageRecord, $configurationName = 'pages') |
||
| 601 | 6 | ||
| 602 | /** |
||
| 603 | 6 | * Get allowed page types |
|
| 604 | 3 | * |
|
| 605 | * @param int $pageId Page ID |
||
| 606 | * @param string $configurationName The name of the configuration to use. |
||
| 607 | * |
||
| 608 | 28 | * @return array Allowed page types to compare to a doktype of a page record |
|
| 609 | */ |
||
| 610 | public static function getAllowedPageTypes($pageId, $configurationName = 'pages') |
||
| 616 | |||
| 617 | /** |
||
| 618 | * Method to check if a page exists. |
||
| 619 | 27 | * |
|
| 620 | * @param int $pageId |
||
| 621 | 27 | * @return bool |
|
| 622 | 27 | */ |
|
| 623 | 27 | public static function pageExists($pageId) |
|
| 633 | |||
| 634 | /** |
||
| 635 | * Resolves the configured absRefPrefix to a valid value and resolved if absRefPrefix |
||
| 636 | * is set to "auto". |
||
| 637 | * |
||
| 638 | * @param TypoScriptFrontendController $TSFE |
||
| 639 | * @return string |
||
| 640 | 27 | */ |
|
| 641 | public static function getAbsRefPrefixFromTSFE(TypoScriptFrontendController $TSFE) |
||
| 655 | |||
| 656 | /** |
||
| 657 | * This function can be used to check if one of the strings in needles is |
||
| 658 | * contained in the haystack. |
||
| 659 | * |
||
| 660 | * |
||
| 661 | * Example: |
||
| 662 | * |
||
| 663 | * haystack: the brown fox |
||
| 664 | * needles: ['hello', 'world'] |
||
| 665 | * result: false |
||
| 666 | * |
||
| 667 | * haystack: the brown fox |
||
| 668 | * needles: ['is', 'fox'] |
||
| 669 | * result: true |
||
| 670 | * |
||
| 671 | 15 | * @param string $haystack |
|
| 672 | * @param array $needles |
||
| 673 | 15 | * @return bool |
|
| 674 | 15 | */ |
|
| 675 | 12 | public static function containsOneOfTheStrings($haystack, array $needles) |
|
| 686 | } |
||
| 687 |