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 |
||
| 38 | class Util |
||
| 39 | { |
||
| 40 | const SOLR_ISO_DATETIME_FORMAT = 'Y-m-d\TH:i:s\Z'; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Generates a document id for documents representing page records. |
||
| 44 | * |
||
| 45 | * @param int $uid The page's uid |
||
| 46 | * @param int $typeNum The page's typeNum |
||
| 47 | * @param int $language the language id, defaults to 0 |
||
| 48 | * @param string $accessGroups comma separated list of uids of groups that have access to that page |
||
| 49 | * @param string $mountPointParameter The mount point parameter that is used to access the page. |
||
| 50 | * @return string The document id for that page |
||
| 51 | */ |
||
| 52 | 32 | public static function getPageDocumentId( |
|
| 74 | |||
| 75 | /** |
||
| 76 | * Generates a document id in the form $siteHash/$type/$uid. |
||
| 77 | * |
||
| 78 | * @param string $table The records table name |
||
| 79 | * @param int $pid The record's pid |
||
| 80 | * @param int $uid The record's uid |
||
| 81 | * @param string $additionalIdParameters Additional ID parameters |
||
| 82 | * @return string A document id |
||
| 83 | */ |
||
| 84 | 42 | public static function getDocumentId( |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Converts a date from unix timestamp to ISO 8601 format. |
||
| 102 | * |
||
| 103 | * @param int $timestamp unix timestamp |
||
| 104 | * @return string the date in ISO 8601 format |
||
| 105 | */ |
||
| 106 | 45 | public static function timestampToIso($timestamp) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * Converts a date from ISO 8601 format to unix timestamp. |
||
| 113 | * |
||
| 114 | * @param string $isoTime date in ISO 8601 format |
||
| 115 | * @return int unix timestamp |
||
| 116 | */ |
||
| 117 | 18 | public static function isoToTimestamp($isoTime) |
|
| 123 | |||
| 124 | /** |
||
| 125 | * Converts a date from unix timestamp to ISO 8601 format in UTC timezone. |
||
| 126 | * |
||
| 127 | * @param int $timestamp unix timestamp |
||
| 128 | * @return string the date in ISO 8601 format |
||
| 129 | */ |
||
| 130 | public static function timestampToUtcIso($timestamp) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Converts a date from ISO 8601 format in UTC timezone to unix timestamp. |
||
| 137 | * |
||
| 138 | * @param string $isoTime date in ISO 8601 format |
||
| 139 | * @return int unix timestamp |
||
| 140 | */ |
||
| 141 | public static function utcIsoToTimestamp($isoTime) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Returns given word as CamelCased. |
||
| 151 | * |
||
| 152 | * Converts a word like "send_email" to "SendEmail". It |
||
| 153 | * will remove non alphanumeric characters from the word, so |
||
| 154 | * "who's online" will be converted to "WhoSOnline" |
||
| 155 | * |
||
| 156 | * @param string $word Word to convert to camel case |
||
| 157 | * @return string UpperCamelCasedWord |
||
| 158 | */ |
||
| 159 | public static function camelize($word) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Returns a given CamelCasedString as an lowercase string with underscores. |
||
| 167 | * Example: Converts BlogExample to blog_example, and minimalValue to minimal_value |
||
| 168 | * |
||
| 169 | * @param string $string String to be converted to lowercase underscore |
||
| 170 | * @return string lowercase_and_underscored_string |
||
| 171 | */ |
||
| 172 | public static function camelCaseToLowerCaseUnderscored($string) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Returns a given string with underscores as UpperCamelCase. |
||
| 179 | * Example: Converts blog_example to BlogExample |
||
| 180 | * |
||
| 181 | * @param string $string String to be converted to camel case |
||
| 182 | * @return string UpperCamelCasedWord |
||
| 183 | */ |
||
| 184 | 26 | public static function underscoredToUpperCamelCase($string) |
|
| 189 | |||
| 190 | /** |
||
| 191 | * Shortcut to retrieve the TypoScript configuration for EXT:solr |
||
| 192 | * (plugin.tx_solr) from TSFE. |
||
| 193 | * |
||
| 194 | * @return TypoScriptConfiguration |
||
| 195 | */ |
||
| 196 | 124 | public static function getSolrConfiguration() |
|
| 202 | |||
| 203 | /** |
||
| 204 | * @return ConfigurationManager |
||
| 205 | */ |
||
| 206 | 136 | private static function getConfigurationManager() |
|
| 212 | |||
| 213 | /** |
||
| 214 | * Gets the Solr configuration for a specific root page id. |
||
| 215 | * To be used from the backend. |
||
| 216 | * |
||
| 217 | * @param int $pageId Id of the (root) page to get the Solr configuration from. |
||
| 218 | * @param bool $initializeTsfe Optionally initializes a full TSFE to get the configuration, defaults to FALSE |
||
| 219 | * @param int $language System language uid, optional, defaults to 0 |
||
| 220 | * @return TypoScriptConfiguration The Solr configuration for the requested tree. |
||
| 221 | */ |
||
| 222 | 24 | public static function getSolrConfigurationFromPageId( |
|
| 230 | |||
| 231 | /** |
||
| 232 | * Loads the TypoScript configuration for a given page id and language. |
||
| 233 | * Language usage may be disabled to get the default TypoScript |
||
| 234 | * configuration. |
||
| 235 | * |
||
| 236 | * @param int $pageId Id of the (root) page to get the Solr configuration from. |
||
| 237 | * @param string $path The TypoScript configuration path to retrieve. |
||
| 238 | * @param bool $initializeTsfe Optionally initializes a full TSFE to get the configuration, defaults to FALSE |
||
| 239 | * @param int|bool $language System language uid or FALSE to disable language usage, optional, defaults to 0 |
||
| 240 | * @return TypoScriptConfiguration The Solr configuration for the requested tree. |
||
| 241 | */ |
||
| 242 | 27 | public static function getConfigurationFromPageId( |
|
| 281 | |||
| 282 | /** |
||
| 283 | * Builds the configuration object from a config array and returns it. |
||
| 284 | * |
||
| 285 | * @param array $configurationToUse |
||
| 286 | * @param int $pageId |
||
| 287 | * @param int $languageId |
||
| 288 | * @param string $typoScriptPath |
||
| 289 | * @return TypoScriptConfiguration |
||
| 290 | */ |
||
| 291 | 27 | protected static function buildTypoScriptConfigurationFromArray(array $configurationToUse, $pageId, $languageId, $typoScriptPath) |
|
| 296 | |||
| 297 | /** |
||
| 298 | * This function is used to retrieve the configuration from a previous initialized TSFE |
||
| 299 | * (see: getConfigurationFromPageId) |
||
| 300 | * |
||
| 301 | * @param string $path |
||
| 302 | * @return mixed |
||
| 303 | */ |
||
| 304 | 1 | private static function getConfigurationFromInitializedTSFE($path) |
|
| 311 | |||
| 312 | /** |
||
| 313 | * This function is used to retrieve the configuration from an existing TSFE instance |
||
| 314 | * @param $pageId |
||
| 315 | * @param $path |
||
| 316 | * @param $language |
||
| 317 | * @return mixed |
||
| 318 | */ |
||
| 319 | 26 | private static function getConfigurationFromExistingTSFE($pageId, $path, $language) |
|
| 362 | |||
| 363 | /** |
||
| 364 | * Initializes the TSFE for a given page ID and language. |
||
| 365 | * |
||
| 366 | * @param int $pageId The page id to initialize the TSFE for |
||
| 367 | * @param int $language System language uid, optional, defaults to 0 |
||
| 368 | * @param bool $useCache Use cache to reuse TSFE |
||
| 369 | * @return void |
||
| 370 | */ |
||
| 371 | 14 | public static function initializeTsfe( |
|
| 436 | |||
| 437 | /** |
||
| 438 | * Determines the rootpage ID for a given page. |
||
| 439 | * |
||
| 440 | * @param int $pageId A page ID somewhere in a tree. |
||
| 441 | * @param bool $forceFallback Force the explicit detection and do not use the current frontend root line |
||
| 442 | * @return int The page's tree branch's root page ID |
||
| 443 | */ |
||
| 444 | 52 | public static function getRootPageId($pageId = 0, $forceFallback = false) |
|
| 469 | |||
| 470 | /** |
||
| 471 | * Checks whether a given root line contains a page marked as root page. |
||
| 472 | * |
||
| 473 | * @param array $rootLine A root line array of page records |
||
| 474 | * @return bool TRUE if the root line contains a root page record, FALSE otherwise |
||
| 475 | */ |
||
| 476 | 41 | protected static function rootlineContainsRootPage(array $rootLine) |
|
| 489 | |||
| 490 | /** |
||
| 491 | * Takes a page Id and checks whether the page is marked as root page. |
||
| 492 | * |
||
| 493 | * @param int $pageId Page ID |
||
| 494 | * @return bool TRUE if the page is marked as root page, FALSE otherwise |
||
| 495 | */ |
||
| 496 | 19 | public static function isRootPage($pageId) |
|
| 507 | |||
| 508 | /** |
||
| 509 | * Gets the site hash for a domain |
||
| 510 | * |
||
| 511 | * @param string $domain Domain to calculate the site hash for. |
||
| 512 | * @return string site hash for $domain |
||
| 513 | */ |
||
| 514 | 44 | 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 | * * - Same as __all |
||
| 537 | * |
||
| 538 | * @param int $pageId A page ID that is then resolved to the site it belongs to |
||
| 539 | * @param string $allowedSitesConfiguration TypoScript setting for allowed sites |
||
| 540 | * @return string List of allowed sites/domains, magic keywords resolved |
||
| 541 | */ |
||
| 542 | 23 | public static function resolveSiteHashAllowedSites( |
|
| 564 | |||
| 565 | /** |
||
| 566 | * Check if record ($table, $uid) is a workspace record |
||
| 567 | * |
||
| 568 | * @param string $table The table the record belongs to |
||
| 569 | * @param int $uid The record's uid |
||
| 570 | * @return bool TRUE if the record is in a draft workspace, FALSE if it's a LIVE record |
||
| 571 | */ |
||
| 572 | 8 | public static function isDraftRecord($table, $uid) |
|
| 586 | |||
| 587 | /** |
||
| 588 | * Checks whether a record is a localization overlay. |
||
| 589 | * |
||
| 590 | * @param string $tableName The record's table name |
||
| 591 | * @param array $record The record to check |
||
| 592 | * @return bool TRUE if the record is a language overlay, FALSE otherwise |
||
| 593 | */ |
||
| 594 | 5 | public static function isLocalizedRecord($tableName, array $record) |
|
| 609 | |||
| 610 | /** |
||
| 611 | * Check if the page type of a page record is allowed |
||
| 612 | * |
||
| 613 | * @param array $pageRecord The pages database row |
||
| 614 | * |
||
| 615 | * @return bool TRUE if the page type is allowed, otherwise FALSE |
||
| 616 | */ |
||
| 617 | 9 | public static function isAllowedPageType(array $pageRecord) |
|
| 628 | |||
| 629 | /** |
||
| 630 | * Get allowed page types |
||
| 631 | * |
||
| 632 | * @param int $pageId Page ID |
||
| 633 | * |
||
| 634 | * @return array Allowed page types to compare to a doktype of a page record |
||
| 635 | */ |
||
| 636 | 9 | public static function getAllowedPageTypes($pageId) |
|
| 642 | |||
| 643 | /** |
||
| 644 | * Method to check if a page exists. |
||
| 645 | * |
||
| 646 | * @param int $pageId |
||
| 647 | * @return bool |
||
| 648 | */ |
||
| 649 | public static function pageExists($pageId) |
||
| 659 | |||
| 660 | /** |
||
| 661 | * Resolves the configured absRefPrefix to a valid value and resolved if absRefPrefix |
||
| 662 | * is set to "auto". |
||
| 663 | * |
||
| 664 | * @param TypoScriptFrontendController $TSFE |
||
| 665 | * @return string |
||
| 666 | */ |
||
| 667 | 14 | public static function getAbsRefPrefixFromTSFE(TypoScriptFrontendController $TSFE) |
|
| 681 | |||
| 682 | /** |
||
| 683 | * This function can be used to check if one of the strings in needles is |
||
| 684 | * contained in the haystack. |
||
| 685 | * |
||
| 686 | * |
||
| 687 | * Example: |
||
| 688 | * |
||
| 689 | * haystack: the brown fox |
||
| 690 | * needles: ['hello', 'world'] |
||
| 691 | * result: false |
||
| 692 | * |
||
| 693 | * haystack: the brown fox |
||
| 694 | * needles: ['is', 'fox'] |
||
| 695 | * result: true |
||
| 696 | * |
||
| 697 | * @param string $haystack |
||
| 698 | * @param array $needles |
||
| 699 | * @return bool |
||
| 700 | */ |
||
| 701 | 33 | public static function containsOneOfTheStrings($haystack, array $needles) |
|
| 712 | } |
||
| 713 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: