Complex classes like Utils 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 Utils, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class Utils |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * Gets upper case word admong a string from the end or from the beginning part |
||
| 11 | * @param string $optionValue |
||
| 12 | * @param string $string the string from which we can extract the part |
||
| 13 | * @return string |
||
| 14 | */ |
||
| 15 | 762 | public static function getPart($optionValue, $string) |
|
| 16 | { |
||
| 17 | 762 | $elementType = ''; |
|
| 18 | 762 | $string = str_replace('_', '', $string); |
|
| 19 | 762 | $string = preg_replace('/([0-9])/', '', $string); |
|
| 20 | 762 | if (!empty($string)) { |
|
| 21 | switch ($optionValue) { |
||
| 22 | 762 | case GeneratorOptions::VALUE_END: |
|
| 23 | 24 | $parts = preg_split('/[A-Z]/', ucfirst($string)); |
|
| 24 | 24 | $partsCount = count($parts); |
|
| 25 | 24 | if (!empty($parts[$partsCount - 1])) { |
|
| 26 | 18 | $elementType = substr($string, strrpos($string, implode('', array_slice($parts, -1))) - 1); |
|
| 27 | 12 | } else { |
|
| 28 | 6 | for ($i = $partsCount - 1; $i >= 0; $i--) { |
|
| 29 | 6 | $part = trim($parts[$i]); |
|
| 30 | 6 | if (!empty($part)) { |
|
| 31 | break; |
||
| 32 | } |
||
| 33 | 4 | } |
|
| 34 | 6 | $elementType = substr($string, ((count($parts) - 2 - $i) + 1) * -1); |
|
| 35 | } |
||
| 36 | 24 | break; |
|
| 37 | 738 | case GeneratorOptions::VALUE_START: |
|
| 38 | 732 | $parts = preg_split('/[A-Z]/', ucfirst($string)); |
|
| 39 | 732 | $partsCount = count($parts); |
|
| 40 | 732 | if (empty($parts[0]) && !empty($parts[1])) { |
|
| 41 | 732 | $elementType = substr($string, 0, strlen($parts[1]) + 1); |
|
| 42 | 488 | } else { |
|
| 43 | 42 | for ($i = 0; $i < $partsCount; $i++) { |
|
| 44 | 42 | $part = trim($parts[$i]); |
|
| 45 | 42 | if (!empty($part)) { |
|
| 46 | 42 | break; |
|
| 47 | } |
||
| 48 | 28 | } |
|
| 49 | 42 | $elementType = substr($string, 0, $i); |
|
| 50 | } |
||
| 51 | 732 | break; |
|
| 52 | 4 | default: |
|
| 53 | 6 | break; |
|
| 54 | 4 | } |
|
| 55 | 508 | } |
|
| 56 | 762 | return $elementType; |
|
| 57 | } |
||
| 58 | /** |
||
| 59 | * Get content from url using a proxy or not |
||
| 60 | * @param string $url |
||
| 61 | * @param string $basicAuthLogin |
||
| 62 | * @param string $basicAuthPassword |
||
| 63 | * @param string $proxyHost |
||
| 64 | * @param string $proxyPort |
||
| 65 | * @param string $proxyLogin |
||
| 66 | * @param string $proxyPassword |
||
| 67 | * @param array $contextOptions |
||
| 68 | * @return string |
||
| 69 | */ |
||
| 70 | 12 | public static function getContentFromUrl($url, $basicAuthLogin = null, $basicAuthPassword = null, $proxyHost = null, $proxyPort = null, $proxyLogin = null, $proxyPassword = null, array $contextOptions = array()) |
|
| 79 | /** |
||
| 80 | * @param string $basicAuthLogin |
||
| 81 | * @param string $basicAuthPassword |
||
| 82 | * @param string $proxyHost |
||
| 83 | * @param string $proxyPort |
||
| 84 | * @param string $proxyLogin |
||
| 85 | * @param string $proxyPassword |
||
| 86 | * @param array $contextOptions |
||
| 87 | * @return string[] |
||
| 88 | */ |
||
| 89 | 36 | public static function getStreamContextOptions($basicAuthLogin = null, $basicAuthPassword = null, $proxyHost = null, $proxyPort = null, $proxyLogin = null, $proxyPassword = null, array $contextOptions = array()) |
|
| 113 | /** |
||
| 114 | * Returns the value with good type |
||
| 115 | * @param mixed $value the value |
||
| 116 | * @param string $knownType the value |
||
| 117 | * @return mixed |
||
| 118 | */ |
||
| 119 | 198 | public static function getValueWithinItsType($value, $knownType = null) |
|
| 146 | /** |
||
| 147 | * @param string $origin |
||
| 148 | * @param string $destination |
||
| 149 | * @return string |
||
| 150 | */ |
||
| 151 | 300 | public static function resolveCompletePath($origin, $destination) |
|
| 192 | /** |
||
| 193 | * Clean comment |
||
| 194 | * @param string $comment the comment to clean |
||
| 195 | * @param string $glueSeparator ths string to use when gathering values |
||
| 196 | * @param bool $uniqueValues indicates if comment values must be unique or not |
||
| 197 | * @return string |
||
| 198 | */ |
||
| 199 | 234 | public static function cleanComment($comment, $glueSeparator = ',', $uniqueValues = true) |
|
| 206 | /** |
||
| 207 | * Clean a string to make it valid as PHP variable |
||
| 208 | * See more about the used regular expression at {@link http://www.regular-expressions.info/unicode.html}: |
||
| 209 | * - \p{L} for any valid letter |
||
| 210 | * - \p{N} for any valid number |
||
| 211 | * - /u for suporting unicode |
||
| 212 | * @param string $string the string to clean |
||
| 213 | * @param bool $keepMultipleUnderscores optional, allows to keep the multiple consecutive underscores |
||
| 214 | * @return string |
||
| 215 | */ |
||
| 216 | 894 | public static function cleanString($string, $keepMultipleUnderscores = true) |
|
| 224 | /** |
||
| 225 | * @param string $namespacedClassName |
||
| 226 | * @return string |
||
| 227 | */ |
||
| 228 | 324 | public static function removeNamespace($namespacedClassName) |
|
| 233 | /** |
||
| 234 | * @param string $directory |
||
| 235 | * @param int $permissions |
||
| 236 | * @return bool |
||
| 237 | */ |
||
| 238 | 360 | public static function createDirectory($directory, $permissions = 0775) |
|
| 245 | } |
||
| 246 |