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 | 472 | public static function getPart($optionValue, $string) |
|
| 62 | /** |
||
| 63 | * Get content from url using a proxy or not |
||
| 64 | * @param string $url |
||
| 65 | * @param string $basicAuthLogin |
||
| 66 | * @param string $basicAuthPassword |
||
| 67 | * @param string $proxyHost |
||
| 68 | * @param string $proxyPort |
||
| 69 | * @param string $proxyLogin |
||
| 70 | * @param string $proxyPassword |
||
| 71 | * @param array $contextOptions |
||
| 72 | * @return string |
||
| 73 | */ |
||
| 74 | 8 | public static function getContentFromUrl($url, $basicAuthLogin = null, $basicAuthPassword = null, $proxyHost = null, $proxyPort = null, $proxyLogin = null, $proxyPassword = null, array $contextOptions = array()) |
|
| 83 | /** |
||
| 84 | * @param string $basicAuthLogin |
||
| 85 | * @param string $basicAuthPassword |
||
| 86 | * @param string $proxyHost |
||
| 87 | * @param string $proxyPort |
||
| 88 | * @param string $proxyLogin |
||
| 89 | * @param string $proxyPassword |
||
| 90 | * @param array $contextOptions |
||
| 91 | * @return string[] |
||
| 92 | */ |
||
| 93 | 24 | public static function getStreamContextOptions($basicAuthLogin = null, $basicAuthPassword = null, $proxyHost = null, $proxyPort = null, $proxyLogin = null, $proxyPassword = null, array $contextOptions = array()) |
|
| 94 | { |
||
| 95 | 24 | $proxyOptions = $basicAuthOptions = array(); |
|
| 96 | 24 | if (!empty($basicAuthLogin) && !empty($basicAuthPassword)) { |
|
| 97 | $basicAuthOptions = array( |
||
| 98 | 'http' => array( |
||
| 99 | 'header' => array( |
||
| 100 | 12 | sprintf('Authorization: Basic %s', base64_encode(sprintf('%s:%s', $basicAuthLogin, $basicAuthPassword))), |
|
| 101 | 6 | ), |
|
| 102 | 6 | ), |
|
| 103 | 6 | ); |
|
| 104 | 6 | } |
|
| 105 | 24 | if (!empty($proxyHost)) { |
|
| 106 | $proxyOptions = array( |
||
| 107 | 'http' => array( |
||
| 108 | 12 | 'proxy' => sprintf('tcp://%s%s', $proxyHost, empty($proxyPort) ? '' : sprintf(':%s', $proxyPort)), |
|
| 109 | 'header' => array( |
||
| 110 | 12 | sprintf('Proxy-Authorization: Basic %s', base64_encode(sprintf('%s:%s', $proxyLogin, $proxyPassword))), |
|
| 111 | 6 | ), |
|
| 112 | 6 | ), |
|
| 113 | 6 | ); |
|
| 114 | 6 | } |
|
| 115 | 24 | return array_merge_recursive($contextOptions, $proxyOptions, $basicAuthOptions); |
|
| 116 | } |
||
| 117 | /** |
||
| 118 | * Returns the value with good type |
||
| 119 | * @param mixed $value the value |
||
| 120 | * @param string $knownType the value |
||
| 121 | * @return mixed |
||
| 122 | */ |
||
| 123 | 580 | public static function getValueWithinItsType($value, $knownType = null) |
|
| 150 | /** |
||
| 151 | * @param string $origin |
||
| 152 | * @param string $destination |
||
| 153 | * @return string |
||
| 154 | */ |
||
| 155 | 188 | public static function resolveCompletePath($origin, $destination) |
|
| 196 | /** |
||
| 197 | * Clean comment |
||
| 198 | * @param string $comment the comment to clean |
||
| 199 | * @param string $glueSeparator ths string to use when gathering values |
||
| 200 | * @param bool $uniqueValues indicates if comment values must be unique or not |
||
| 201 | * @return string |
||
| 202 | */ |
||
| 203 | 136 | public static function cleanComment($comment, $glueSeparator = ',', $uniqueValues = true) |
|
| 210 | /** |
||
| 211 | * Clean a string to make it valid as PHP variable |
||
| 212 | * See more about the used regular expression at {@link http://www.regular-expressions.info/unicode.html}: |
||
| 213 | * - \p{L} for any valid letter |
||
| 214 | * - \p{N} for any valid number |
||
| 215 | * - /u for suporting unicode |
||
| 216 | * @param string $string the string to clean |
||
| 217 | * @param bool $keepMultipleUnderscores optional, allows to keep the multiple consecutive underscores |
||
| 218 | * @return string |
||
| 219 | */ |
||
| 220 | 532 | public static function cleanString($string, $keepMultipleUnderscores = true) |
|
| 228 | /** |
||
| 229 | * @param string $namespacedClassName |
||
| 230 | * @return string |
||
| 231 | */ |
||
| 232 | 196 | public static function removeNamespace($namespacedClassName) |
|
| 237 | /** |
||
| 238 | * @param string $directory |
||
| 239 | * @param int $permissions |
||
| 240 | * @return bool |
||
| 241 | */ |
||
| 242 | 220 | public static function createDirectory($directory, $permissions = 0775) |
|
| 249 | } |
||
| 250 |