Total Complexity | 42 |
Total Lines | 301 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Util 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.
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 Util, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class Util |
||
9 | { |
||
10 | /** |
||
11 | * Get normalized pathinfo. |
||
12 | * |
||
13 | * @param string $path |
||
14 | * |
||
15 | * @return array pathinfo |
||
16 | */ |
||
17 | public static function pathinfo($path) |
||
18 | { |
||
19 | $pathinfo = pathinfo($path) + compact('path'); |
||
20 | $pathinfo['dirname'] = array_key_exists('dirname', $pathinfo) |
||
21 | ? static::normalizeDirname($pathinfo['dirname']) : ''; |
||
22 | |||
23 | return $pathinfo; |
||
24 | } |
||
25 | |||
26 | /** |
||
27 | * Normalize a dirname return value. |
||
28 | * |
||
29 | * @param string $dirname |
||
30 | * |
||
31 | * @return string normalized dirname |
||
32 | */ |
||
33 | public static function normalizeDirname($dirname) |
||
34 | { |
||
35 | return $dirname === '.' ? '' : $dirname; |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * Get a normalized dirname from a path. |
||
40 | * |
||
41 | * @param string $path |
||
42 | * |
||
43 | * @return string dirname |
||
44 | */ |
||
45 | public static function dirname($path) |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * Map result arrays. |
||
52 | * |
||
53 | * @param array $object |
||
54 | * @param array $map |
||
55 | * |
||
56 | * @return array mapped result |
||
57 | */ |
||
58 | public static function map(array $object, array $map) |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Normalize path. |
||
75 | * |
||
76 | * @param string $path |
||
77 | * |
||
78 | * @throws LogicException |
||
79 | * |
||
80 | * @return string |
||
81 | */ |
||
82 | public static function normalizePath($path) |
||
83 | { |
||
84 | return static::normalizeRelativePath($path); |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Normalize relative directories in a path. |
||
89 | * |
||
90 | * @param string $path |
||
91 | * |
||
92 | * @throws LogicException |
||
93 | * |
||
94 | * @return string |
||
95 | */ |
||
96 | public static function normalizeRelativePath($path) |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Removes unprintable characters and invalid unicode characters. |
||
129 | * |
||
130 | * @param string $path |
||
131 | * |
||
132 | * @return string $path |
||
133 | */ |
||
134 | protected static function removeFunkyWhiteSpace($path) { |
||
135 | // We do this check in a loop, since removing invalid unicode characters |
||
136 | // can lead to new characters being created. |
||
137 | while (preg_match('#\p{C}+|^\./#u', $path)) { |
||
138 | $path = preg_replace('#\p{C}+|^\./#u', '', $path); |
||
139 | } |
||
140 | |||
141 | return $path; |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Normalize prefix. |
||
146 | * |
||
147 | * @param string $prefix |
||
148 | * @param string $separator |
||
149 | * |
||
150 | * @return string normalized path |
||
151 | */ |
||
152 | public static function normalizePrefix($prefix, $separator) |
||
153 | { |
||
154 | return rtrim($prefix, $separator) . $separator; |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * Get content size. |
||
159 | * |
||
160 | * @param string $contents |
||
161 | * |
||
162 | * @return int content size |
||
163 | */ |
||
164 | public static function contentSize($contents) |
||
165 | { |
||
166 | return defined('MB_OVERLOAD_STRING') ? mb_strlen($contents, '8bit') : strlen($contents); |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Guess MIME Type based on the path of the file and it's content. |
||
171 | * |
||
172 | * @param string $path |
||
173 | * @param string|resource $content |
||
174 | * |
||
175 | * @return string|null MIME Type or NULL if no extension detected |
||
176 | */ |
||
177 | public static function guessMimeType($path, $content) |
||
178 | { |
||
179 | $mimeType = MimeType::detectByContent($content); |
||
180 | |||
181 | if ( ! (empty($mimeType) || in_array($mimeType, ['application/x-empty', 'text/plain', 'text/x-asm']))) { |
||
182 | return $mimeType; |
||
183 | } |
||
184 | |||
185 | return MimeType::detectByFilename($path); |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Emulate directories. |
||
190 | * |
||
191 | * @param array $listing |
||
192 | * |
||
193 | * @return array listing with emulated directories |
||
194 | */ |
||
195 | public static function emulateDirectories(array $listing) |
||
196 | { |
||
197 | $directories = []; |
||
198 | $listedDirectories = []; |
||
199 | |||
200 | foreach ($listing as $object) { |
||
201 | list($directories, $listedDirectories) = static::emulateObjectDirectories( |
||
202 | $object, |
||
203 | $directories, |
||
204 | $listedDirectories |
||
205 | ); |
||
206 | } |
||
207 | |||
208 | $directories = array_diff(array_unique($directories), array_unique($listedDirectories)); |
||
209 | |||
210 | foreach ($directories as $directory) { |
||
211 | $listing[] = static::pathinfo($directory) + ['type' => 'dir']; |
||
212 | } |
||
213 | |||
214 | return $listing; |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * Ensure a Config instance. |
||
219 | * |
||
220 | * @param null|array|Config $config |
||
221 | * |
||
222 | * @return Config config instance |
||
223 | * |
||
224 | * @throw LogicException |
||
225 | */ |
||
226 | public static function ensureConfig($config) |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * Rewind a stream. |
||
245 | * |
||
246 | * @param resource $resource |
||
247 | */ |
||
248 | public static function rewindStream($resource) |
||
249 | { |
||
250 | if (ftell($resource) !== 0 && static::isSeekableStream($resource)) { |
||
251 | rewind($resource); |
||
252 | } |
||
253 | } |
||
254 | |||
255 | public static function isSeekableStream($resource) |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * Get the size of a stream. |
||
264 | * |
||
265 | * @param resource $resource |
||
266 | * |
||
267 | * @return int stream size |
||
268 | */ |
||
269 | public static function getStreamSize($resource) |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * Emulate the directories of a single object. |
||
278 | * |
||
279 | * @param array $object |
||
280 | * @param array $directories |
||
281 | * @param array $listedDirectories |
||
282 | * |
||
283 | * @return array |
||
284 | */ |
||
285 | protected static function emulateObjectDirectories(array $object, array $directories, array $listedDirectories) |
||
309 | } |
||
310 | } |
||
311 |