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 | 756 | public static function getPart($optionValue, $string) |
|
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 | 846 | 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 |