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 among 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 | 395 | public static function getPart($optionValue, $string) |
|
| 61 | /** |
||
| 62 | * Get content from url using a proxy or not |
||
| 63 | * @param string $url |
||
| 64 | * @param string $basicAuthLogin |
||
| 65 | * @param string $basicAuthPassword |
||
| 66 | * @param string $proxyHost |
||
| 67 | * @param string $proxyPort |
||
| 68 | * @param string $proxyLogin |
||
| 69 | * @param string $proxyPassword |
||
| 70 | * @param array $contextOptions |
||
| 71 | * @return string |
||
| 72 | */ |
||
| 73 | 10 | public static function getContentFromUrl($url, $basicAuthLogin = null, $basicAuthPassword = null, $proxyHost = null, $proxyPort = null, $proxyLogin = null, $proxyPassword = null, array $contextOptions = []) |
|
| 82 | /** |
||
| 83 | * @param string $basicAuthLogin |
||
| 84 | * @param string $basicAuthPassword |
||
| 85 | * @param string $proxyHost |
||
| 86 | * @param string $proxyPort |
||
| 87 | * @param string $proxyLogin |
||
| 88 | * @param string $proxyPassword |
||
| 89 | * @param array $contextOptions |
||
| 90 | * @return string[] |
||
| 91 | */ |
||
| 92 | 30 | public static function getStreamContextOptions($basicAuthLogin = null, $basicAuthPassword = null, $proxyHost = null, $proxyPort = null, $proxyLogin = null, $proxyPassword = null, array $contextOptions = []) |
|
| 116 | /** |
||
| 117 | * Returns the value with good type |
||
| 118 | * @param mixed $value the value |
||
| 119 | * @param string $knownType the value |
||
| 120 | * @return mixed |
||
| 121 | */ |
||
| 122 | 180 | public static function getValueWithinItsType($value, $knownType = null) |
|
| 149 | /** |
||
| 150 | * @param string $origin |
||
| 151 | * @param string $destination |
||
| 152 | * @return string |
||
| 153 | */ |
||
| 154 | 160 | public static function resolveCompletePath($origin, $destination) |
|
| 195 | /** |
||
| 196 | * Clean comment |
||
| 197 | * @param string $comment the comment to clean |
||
| 198 | * @param string $glueSeparator ths string to use when gathering values |
||
| 199 | * @param bool $uniqueValues indicates if comment values must be unique or not |
||
| 200 | * @return string |
||
| 201 | */ |
||
| 202 | 205 | public static function cleanComment($comment, $glueSeparator = ',', $uniqueValues = true) |
|
| 209 | /** |
||
| 210 | * Clean a string to make it valid as PHP variable |
||
| 211 | * See more about the used regular expression at {@link http://www.regular-expressions.info/unicode.html}: |
||
| 212 | * - \p{L} for any valid letter |
||
| 213 | * - \p{N} for any valid number |
||
| 214 | * - /u for supporting unicode |
||
| 215 | * @param string $string the string to clean |
||
| 216 | * @param bool $keepMultipleUnderscores optional, allows to keep the multiple consecutive underscores |
||
| 217 | * @return string |
||
| 218 | */ |
||
| 219 | 800 | public static function cleanString($string, $keepMultipleUnderscores = true) |
|
| 227 | /** |
||
| 228 | * @param string $namespacedClassName |
||
| 229 | * @return string |
||
| 230 | */ |
||
| 231 | 285 | public static function removeNamespace($namespacedClassName) |
|
| 236 | /** |
||
| 237 | * @param string $directory |
||
| 238 | * @param int $permissions |
||
| 239 | * @return bool |
||
| 240 | */ |
||
| 241 | 325 | public static function createDirectory($directory, $permissions = 0775) |
|
| 248 | /** |
||
| 249 | * Save schemas to schemasFolder |
||
| 250 | * Filename will be extracted from schemasUrl or default schema.wsdl will be used |
||
| 251 | * @param string $destinationFolder |
||
| 252 | * @param string $schemasFolder |
||
| 253 | * @param string $schemasUrl |
||
| 254 | * @param string $content |
||
| 255 | * @return string |
||
| 256 | */ |
||
| 257 | 10 | public static function saveSchemas($destinationFolder, $schemasFolder, $schemasUrl, $content) |
|
| 276 | } |
||
| 277 |