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 | 464 | public static function getPart($optionValue, $string) |
|
16 | { |
||
17 | 464 | $elementType = ''; |
|
18 | 464 | $string = str_replace('_', '', $string); |
|
19 | 464 | $string = preg_replace('/([0-9])/', '', $string); |
|
20 | 464 | if (!empty($string)) { |
|
21 | switch ($optionValue) { |
||
22 | 464 | case GeneratorOptions::VALUE_END: |
|
23 | 12 | $parts = preg_split('/[A-Z]/', ucfirst($string)); |
|
24 | 12 | $partsCount = count($parts); |
|
25 | 12 | if ($partsCount == 0) { |
|
26 | $elementType = $string; |
||
27 | 12 | } elseif (!empty($parts[$partsCount - 1])) { |
|
28 | 12 | $elementType = substr($string, strrpos($string, implode('', array_slice($parts, -1))) - 1); |
|
29 | 9 | } else { |
|
30 | for ($i = $partsCount - 1; $i >= 0; $i--) { |
||
31 | $part = trim($parts[$i]); |
||
32 | if (!empty($part)) { |
||
33 | break; |
||
34 | } |
||
35 | } |
||
36 | $elementType = substr($string, ((count($parts) - 2 - $i) + 1) * -1); |
||
37 | } |
||
38 | 12 | break; |
|
39 | 452 | case GeneratorOptions::VALUE_START: |
|
40 | 452 | $parts = preg_split('/[A-Z]/', ucfirst($string)); |
|
41 | 452 | $partsCount = count($parts); |
|
42 | 452 | if ($partsCount == 0) { |
|
43 | $elementType = $string; |
||
44 | 452 | } elseif (empty($parts[0]) && !empty($parts[1])) { |
|
45 | 452 | $elementType = substr($string, 0, strlen($parts[1]) + 1); |
|
46 | 339 | } else { |
|
47 | 16 | for ($i = 0; $i < $partsCount; $i++) { |
|
48 | 16 | $part = trim($parts[$i]); |
|
49 | 16 | if (!empty($part)) { |
|
50 | 16 | break; |
|
51 | } |
||
52 | 12 | } |
|
53 | 16 | $elementType = substr($string, 0, $i); |
|
54 | } |
||
55 | 452 | break; |
|
56 | default: |
||
57 | break; |
||
58 | } |
||
59 | 348 | } |
|
60 | 464 | return $elementType; |
|
61 | } |
||
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()) |
|
75 | { |
||
76 | 8 | $context = null; |
|
77 | 8 | $options = self::getContentFromUrlContextOptions($url, $basicAuthLogin, $basicAuthPassword, $proxyHost, $proxyPort, $proxyLogin, $proxyPassword, $contextOptions); |
|
78 | 8 | if (!empty($options)) { |
|
79 | 4 | $context = stream_context_create($options); |
|
80 | 3 | } |
|
81 | 8 | return file_get_contents($url, false, $context); |
|
82 | } |
||
83 | /** |
||
84 | * @param string $url |
||
85 | * @param string $basicAuthLogin |
||
86 | * @param string $basicAuthPassword |
||
87 | * @param string $proxyHost |
||
88 | * @param string $proxyPort |
||
89 | * @param string $proxyLogin |
||
90 | * @param string $proxyPassword |
||
91 | * @param array $contextOptions |
||
92 | * @return string[] |
||
93 | */ |
||
94 | 24 | public static function getContentFromUrlContextOptions($url, $basicAuthLogin = null, $basicAuthPassword = null, $proxyHost = null, $proxyPort = null, $proxyLogin = null, $proxyPassword = null, array $contextOptions = array()) |
|
95 | { |
||
96 | 24 | $protocol = strpos($url, 'https://') !== false ? 'https' : 'http'; |
|
97 | 24 | $proxyOptions = $basicAuthOptions = array(); |
|
98 | 24 | if (!empty($basicAuthLogin) && !empty($basicAuthPassword)) { |
|
99 | $basicAuthOptions = array( |
||
100 | $protocol => array( |
||
101 | 'header' => array( |
||
102 | 12 | sprintf('Authorization: Basic %s', base64_encode(sprintf('%s:%s', $basicAuthLogin, $basicAuthPassword))), |
|
103 | 9 | ), |
|
104 | 9 | ), |
|
105 | 9 | ); |
|
106 | 9 | } |
|
107 | 24 | if (!empty($proxyHost)) { |
|
108 | $proxyOptions = array( |
||
109 | $protocol => array( |
||
110 | 12 | 'proxy' => sprintf('tcp://%s%s', |
|
111 | 9 | $proxyHost, |
|
112 | 12 | empty($proxyPort) ? '' : sprintf(':%s', $proxyPort) |
|
113 | 9 | ), |
|
114 | 'header' => array( |
||
115 | 12 | sprintf('Proxy-Authorization: Basic %s', base64_encode(sprintf('%s:%s', $proxyLogin, $proxyPassword))), |
|
116 | 9 | ), |
|
117 | 9 | ), |
|
118 | 9 | ); |
|
119 | 9 | } |
|
120 | 24 | return array_merge_recursive($contextOptions, $proxyOptions, $basicAuthOptions); |
|
121 | } |
||
122 | /** |
||
123 | * Returns the value with good type |
||
124 | * @param mixed $value the value |
||
125 | * @return mixed |
||
126 | */ |
||
127 | 572 | public static function getValueWithinItsType($value, $knownType = null) |
|
138 | /** |
||
139 | * @param string $origin |
||
140 | * @param string $destination |
||
141 | * @return string |
||
142 | */ |
||
143 | 180 | public static function resolveCompletePath($origin, $destination) |
|
187 | /** |
||
188 | * Clean comment |
||
189 | * @param string $comment the comment to clean |
||
190 | * @param string $glueSeparator ths string to use when gathering values |
||
191 | * @param bool $uniqueValues indicates if comment values must be unique or not |
||
192 | * @return string |
||
193 | */ |
||
194 | 136 | public static function cleanComment($comment, $glueSeparator = ',', $uniqueValues = true) |
|
201 | /** |
||
202 | * Clean a string to make it valid as PHP variable |
||
203 | * See more about the used regular expression at {@link http://www.regular-expressions.info/unicode.html}: |
||
204 | * - \p{L} for any valid letter |
||
205 | * - \p{N} for any valid number |
||
206 | * - /u for suporting unicode |
||
207 | * @param string $string the string to clean |
||
208 | * @param bool $keepMultipleUnderscores optional, allows to keep the multiple consecutive underscores |
||
209 | * @return string |
||
210 | */ |
||
211 | 524 | public static function cleanString($string, $keepMultipleUnderscores = true) |
|
219 | /** |
||
220 | * @param string $namespacedClassName |
||
221 | * @return string |
||
222 | */ |
||
223 | 196 | public static function removeNamespace($namespacedClassName) |
|
228 | /** |
||
229 | * @param string $directory |
||
230 | * @param int $permissions |
||
231 | * @return bool |
||
232 | */ |
||
233 | 212 | public static function createDirectory($directory, $permissions = 0775) |
|
240 | } |
||
241 |