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 |
||
43 | class Util |
||
44 | { |
||
45 | const SOLR_ISO_DATETIME_FORMAT = 'Y-m-d\TH:i:s\Z'; |
||
46 | |||
47 | /** |
||
48 | * Generates a document id for documents representing page records. |
||
49 | * |
||
50 | * @param int $uid The page's uid |
||
51 | * @param int $typeNum The page's typeNum |
||
52 | * @param int $language the language id, defaults to 0 |
||
53 | * @param string $accessGroups comma separated list of uids of groups that have access to that page |
||
54 | * @param string $mountPointParameter The mount point parameter that is used to access the page. |
||
55 | * @return string The document id for that page |
||
56 | */ |
||
57 | 32 | public static function getPageDocumentId( |
|
79 | |||
80 | /** |
||
81 | * Generates a document id in the form $siteHash/$type/$uid. |
||
82 | * |
||
83 | * @param string $table The records table name |
||
84 | * @param int $pid The record's pid |
||
85 | * @param int $uid The record's uid |
||
86 | * @param string $additionalIdParameters Additional ID parameters |
||
87 | * @return string A document id |
||
88 | */ |
||
89 | 42 | public static function getDocumentId( |
|
104 | |||
105 | /** |
||
106 | * Converts a date from unix timestamp to ISO 8601 format. |
||
107 | * |
||
108 | * @param int $timestamp unix timestamp |
||
109 | * @return string the date in ISO 8601 format |
||
110 | */ |
||
111 | 45 | public static function timestampToIso($timestamp) |
|
115 | |||
116 | /** |
||
117 | * Converts a date from ISO 8601 format to unix timestamp. |
||
118 | * |
||
119 | * @param string $isoTime date in ISO 8601 format |
||
120 | * @return int unix timestamp |
||
121 | */ |
||
122 | 18 | public static function isoToTimestamp($isoTime) |
|
128 | |||
129 | /** |
||
130 | * Converts a date from unix timestamp to ISO 8601 format in UTC timezone. |
||
131 | * |
||
132 | * @param int $timestamp unix timestamp |
||
133 | * @return string the date in ISO 8601 format |
||
134 | */ |
||
135 | public static function timestampToUtcIso($timestamp) |
||
139 | |||
140 | /** |
||
141 | * Converts a date from ISO 8601 format in UTC timezone to unix timestamp. |
||
142 | * |
||
143 | * @param string $isoTime date in ISO 8601 format |
||
144 | * @return int unix timestamp |
||
145 | */ |
||
146 | public static function utcIsoToTimestamp($isoTime) |
||
153 | |||
154 | /** |
||
155 | * Returns given word as CamelCased. |
||
156 | * |
||
157 | * Converts a word like "send_email" to "SendEmail". It |
||
158 | * will remove non alphanumeric characters from the word, so |
||
159 | * "who's online" will be converted to "WhoSOnline" |
||
160 | * |
||
161 | * @param string $word Word to convert to camel case |
||
162 | * @return string UpperCamelCasedWord |
||
163 | */ |
||
164 | public static function camelize($word) |
||
169 | |||
170 | /** |
||
171 | * Returns a given CamelCasedString as an lowercase string with underscores. |
||
172 | * Example: Converts BlogExample to blog_example, and minimalValue to minimal_value |
||
173 | * |
||
174 | * @param string $string String to be converted to lowercase underscore |
||
175 | * @return string lowercase_and_underscored_string |
||
176 | */ |
||
177 | public static function camelCaseToLowerCaseUnderscored($string) |
||
181 | |||
182 | /** |
||
183 | * Returns a given string with underscores as UpperCamelCase. |
||
184 | * Example: Converts blog_example to BlogExample |
||
185 | * |
||
186 | * @param string $string String to be converted to camel case |
||
187 | * @return string UpperCamelCasedWord |
||
188 | */ |
||
189 | 26 | public static function underscoredToUpperCamelCase($string) |
|
194 | |||
195 | /** |
||
196 | * Shortcut to retrieve the TypoScript configuration for EXT:solr |
||
197 | * (plugin.tx_solr) from TSFE. |
||
198 | * |
||
199 | * @return TypoScriptConfiguration |
||
200 | */ |
||
201 | 124 | public static function getSolrConfiguration() |
|
207 | |||
208 | /** |
||
209 | * @return ConfigurationManager |
||
210 | */ |
||
211 | 144 | private static function getConfigurationManager() |
|
217 | |||
218 | /** |
||
219 | * Gets the Solr configuration for a specific root page id. |
||
220 | * To be used from the backend. |
||
221 | * |
||
222 | * @param int $pageId Id of the (root) page to get the Solr configuration from. |
||
223 | * @param bool $initializeTsfe Optionally initializes a full TSFE to get the configuration, defaults to FALSE |
||
224 | * @param int $language System language uid, optional, defaults to 0 |
||
225 | * @return TypoScriptConfiguration The Solr configuration for the requested tree. |
||
226 | */ |
||
227 | 32 | public static function getSolrConfigurationFromPageId( |
|
235 | |||
236 | /** |
||
237 | * Loads the TypoScript configuration for a given page id and language. |
||
238 | * Language usage may be disabled to get the default TypoScript |
||
239 | * configuration. |
||
240 | * |
||
241 | * @param int $pageId Id of the (root) page to get the Solr configuration from. |
||
242 | * @param string $path The TypoScript configuration path to retrieve. |
||
243 | * @param bool $initializeTsfe Optionally initializes a full TSFE to get the configuration, defaults to FALSE |
||
244 | * @param int|bool $language System language uid or FALSE to disable language usage, optional, defaults to 0 |
||
245 | * @param bool $useCache |
||
246 | * @return TypoScriptConfiguration The Solr configuration for the requested tree. |
||
247 | */ |
||
248 | 35 | public static function getConfigurationFromPageId( |
|
294 | |||
295 | /** |
||
296 | * Builds the configuration object from a config array and returns it. |
||
297 | * |
||
298 | * @param array $configurationToUse |
||
299 | * @param int $pageId |
||
300 | * @param int $languageId |
||
301 | * @param string $typoScriptPath |
||
302 | * @return TypoScriptConfiguration |
||
303 | */ |
||
304 | 35 | protected static function buildTypoScriptConfigurationFromArray(array $configurationToUse, $pageId, $languageId, $typoScriptPath) |
|
309 | |||
310 | /** |
||
311 | * This function is used to retrieve the configuration from a previous initialized TSFE |
||
312 | * (see: getConfigurationFromPageId) |
||
313 | * |
||
314 | * @param string $path |
||
315 | * @return mixed |
||
316 | */ |
||
317 | 1 | private static function getConfigurationFromInitializedTSFE($path) |
|
325 | |||
326 | /** |
||
327 | * This function is used to retrieve the configuration from an existing TSFE instance |
||
328 | * @param $pageId |
||
329 | * @param $path |
||
330 | * @param $language |
||
331 | * @return mixed |
||
332 | */ |
||
333 | 34 | private static function getConfigurationFromExistingTSFE($pageId, $path, $language) |
|
377 | |||
378 | /** |
||
379 | * Initializes the TSFE for a given page ID and language. |
||
380 | * |
||
381 | * @param int $pageId The page id to initialize the TSFE for |
||
382 | * @param int $language System language uid, optional, defaults to 0 |
||
383 | * @param bool $useCache Use cache to reuse TSFE |
||
384 | * @return void |
||
385 | */ |
||
386 | 14 | public static function initializeTsfe( |
|
451 | |||
452 | /** |
||
453 | * Determines the rootpage ID for a given page. |
||
454 | * |
||
455 | * @param int $pageId A page ID somewhere in a tree. |
||
456 | * @param bool $forceFallback Force the explicit detection and do not use the current frontend root line |
||
457 | * @return int The page's tree branch's root page ID |
||
458 | */ |
||
459 | 57 | public static function getRootPageId($pageId = 0, $forceFallback = false) |
|
480 | |||
481 | /** |
||
482 | * Takes a page Id and checks whether the page is marked as root page. |
||
483 | * |
||
484 | * @param int $pageId Page ID |
||
485 | * @return bool TRUE if the page is marked as root page, FALSE otherwise |
||
486 | */ |
||
487 | 23 | public static function isRootPage($pageId) |
|
498 | |||
499 | /** |
||
500 | * Gets the site hash for a domain |
||
501 | * |
||
502 | * @param string $domain Domain to calculate the site hash for. |
||
503 | * @return string site hash for $domain |
||
504 | */ |
||
505 | 44 | public static function getSiteHashForDomain($domain) |
|
520 | |||
521 | /** |
||
522 | * Resolves magic keywords in allowed sites configuration. |
||
523 | * Supported keywords: |
||
524 | * __solr_current_site - The domain of the site the query has been started from |
||
525 | * __current_site - Same as __solr_current_site |
||
526 | * __all - Adds all domains as allowed sites |
||
527 | * * - Same as __all |
||
528 | * |
||
529 | * @param int $pageId A page ID that is then resolved to the site it belongs to |
||
530 | * @param string $allowedSitesConfiguration TypoScript setting for allowed sites |
||
531 | * @return string List of allowed sites/domains, magic keywords resolved |
||
532 | */ |
||
533 | 23 | public static function resolveSiteHashAllowedSites( |
|
555 | |||
556 | /** |
||
557 | * Check if record ($table, $uid) is a workspace record |
||
558 | * |
||
559 | * @param string $table The table the record belongs to |
||
560 | * @param int $uid The record's uid |
||
561 | * @return bool TRUE if the record is in a draft workspace, FALSE if it's a LIVE record |
||
562 | */ |
||
563 | 16 | public static function isDraftRecord($table, $uid) |
|
577 | |||
578 | /** |
||
579 | * Checks whether a record is a localization overlay. |
||
580 | * |
||
581 | * @param string $tableName The record's table name |
||
582 | * @param array $record The record to check |
||
583 | * @return bool TRUE if the record is a language overlay, FALSE otherwise |
||
584 | */ |
||
585 | 11 | public static function isLocalizedRecord($tableName, array $record) |
|
599 | |||
600 | /** |
||
601 | * Check if the page type of a page record is allowed |
||
602 | * |
||
603 | * @param array $pageRecord The pages database row |
||
604 | * |
||
605 | * @return bool TRUE if the page type is allowed, otherwise FALSE |
||
606 | */ |
||
607 | 13 | public static function isAllowedPageType(array $pageRecord) |
|
618 | |||
619 | /** |
||
620 | * Get allowed page types |
||
621 | * |
||
622 | * @param int $pageId Page ID |
||
623 | * |
||
624 | * @return array Allowed page types to compare to a doktype of a page record |
||
625 | */ |
||
626 | 13 | public static function getAllowedPageTypes($pageId) |
|
632 | |||
633 | /** |
||
634 | * Method to check if a page exists. |
||
635 | * |
||
636 | * @param int $pageId |
||
637 | * @return bool |
||
638 | */ |
||
639 | public static function pageExists($pageId) |
||
649 | |||
650 | /** |
||
651 | * Resolves the configured absRefPrefix to a valid value and resolved if absRefPrefix |
||
652 | * is set to "auto". |
||
653 | * |
||
654 | * @param TypoScriptFrontendController $TSFE |
||
655 | * @return string |
||
656 | */ |
||
657 | 14 | public static function getAbsRefPrefixFromTSFE(TypoScriptFrontendController $TSFE) |
|
671 | |||
672 | /** |
||
673 | * This function can be used to check if one of the strings in needles is |
||
674 | * contained in the haystack. |
||
675 | * |
||
676 | * |
||
677 | * Example: |
||
678 | * |
||
679 | * haystack: the brown fox |
||
680 | * needles: ['hello', 'world'] |
||
681 | * result: false |
||
682 | * |
||
683 | * haystack: the brown fox |
||
684 | * needles: ['is', 'fox'] |
||
685 | * result: true |
||
686 | * |
||
687 | * @param string $haystack |
||
688 | * @param array $needles |
||
689 | * @return bool |
||
690 | */ |
||
691 | 33 | public static function containsOneOfTheStrings($haystack, array $needles) |
|
702 | } |
||
703 |
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: