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 |
||
21 | class Util |
||
22 | { |
||
23 | /** |
||
24 | * Constructor. |
||
25 | * |
||
26 | * @throws LogicException |
||
27 | */ |
||
28 | public function __construct() |
||
32 | |||
33 | /** |
||
34 | * Converts all line endings to Unix format. |
||
35 | * |
||
36 | * @param string $string The string |
||
37 | * |
||
38 | * @return string The normalized string |
||
39 | */ |
||
40 | public static function normalizeLineFeeds($string) |
||
44 | |||
45 | /** |
||
46 | * Removes potential UTF Byte Order Mark. |
||
47 | * |
||
48 | * @param string $string The string to fix |
||
49 | * |
||
50 | * @return string Fixed string |
||
51 | */ |
||
52 | public static function removeUtf8ByteOrderMark($string) |
||
56 | |||
57 | /** |
||
58 | * Php version of javascript's `encodeURIComponent` function. |
||
59 | * |
||
60 | * @param string $string The string to encode |
||
61 | * |
||
62 | * @return string The encoded string |
||
63 | */ |
||
64 | public static function encodeURIComponent($string) |
||
70 | |||
71 | /** |
||
72 | * Returns the line number and column from the $string for a character at specified $index. |
||
73 | * Also includes an extract from the string (optionally). |
||
74 | * |
||
75 | * @param string $string The string |
||
76 | * @param int $index The current position |
||
77 | * @param bool|int $extract Include extract from the string at specified line? Integer value means how many lines will be extracted |
||
78 | * |
||
79 | * @return array Array of line, column and extract from the string |
||
80 | */ |
||
81 | public static function getLocation($string, $index, $column = null, $extract = false) |
||
101 | |||
102 | /** |
||
103 | * Returns the excerpt from the string at given line. |
||
104 | * |
||
105 | * @param string $string The string |
||
106 | * @param int $currentLine The current line. If -1 is passed, the whole string will be returned |
||
107 | * @param int $currentColumn The current column |
||
108 | * @param int $limitLines How many lines? |
||
109 | * |
||
110 | * @return \ILess\Util\StringExcerpt |
||
111 | */ |
||
112 | public static function getExcerpt($string, $currentLine, $currentColumn = null, $limitLines = 3) |
||
124 | |||
125 | /** |
||
126 | * Generates unique cache key for given $filename. |
||
127 | * |
||
128 | * @param string $filename |
||
129 | * |
||
130 | * @return string |
||
131 | */ |
||
132 | public static function generateCacheKey($filename) |
||
136 | |||
137 | /** |
||
138 | * Is the path absolute? |
||
139 | * |
||
140 | * @param string $path |
||
141 | * |
||
142 | * @return bool |
||
143 | */ |
||
144 | public static function isPathAbsolute($path) |
||
159 | |||
160 | /** |
||
161 | * Returns fragment and path components from the path. |
||
162 | * |
||
163 | * @param string $path |
||
164 | * |
||
165 | * @return array fragment, path |
||
166 | */ |
||
167 | public static function getFragmentAndPath($path) |
||
178 | |||
179 | /** |
||
180 | * Is the path relative? |
||
181 | * |
||
182 | * @param string $path The path |
||
183 | * |
||
184 | * @return bool |
||
185 | */ |
||
186 | public static function isPathRelative($path) |
||
190 | |||
191 | /** |
||
192 | * Normalizes the path. |
||
193 | * |
||
194 | * @param string $path The path or url |
||
195 | * @param bool $sanitize Sanitize before normalizing? |
||
196 | * |
||
197 | * @return string The normalized path |
||
198 | */ |
||
199 | public static function normalizePath($path, $sanitize = true) |
||
239 | |||
240 | /** |
||
241 | * Sanitizes a path. Replaces Windows path separator. |
||
242 | * |
||
243 | * @param string $path The path to sanitize |
||
244 | * |
||
245 | * @return string |
||
246 | */ |
||
247 | public static function sanitizePath($path) |
||
251 | |||
252 | /** |
||
253 | * Normalizes the string to be used. |
||
254 | * |
||
255 | * @param string $string |
||
256 | * |
||
257 | * @return string |
||
258 | */ |
||
259 | public static function normalizeString($string) |
||
263 | |||
264 | /** |
||
265 | * Compares the nodes. Returns: |
||
266 | * -1: a < b |
||
267 | * 0: a = b |
||
268 | * 1: a > b |
||
269 | * and *any* other value for a != b (e.g. null, NaN, -2 etc.). |
||
270 | * |
||
271 | * @param mixed $a |
||
272 | * @param mixed $b |
||
273 | * |
||
274 | * @return int |
||
275 | */ |
||
276 | public static function compareNodes($a, $b) |
||
311 | |||
312 | /** |
||
313 | * @param $a |
||
314 | * @param $b |
||
315 | * |
||
316 | * @return int|null |
||
317 | */ |
||
318 | public static function numericCompare($a, $b) |
||
332 | |||
333 | /** |
||
334 | * Round the value using the `$context->precision` setting. |
||
335 | * |
||
336 | * @param Context $context |
||
337 | * @param mixed $value |
||
338 | * |
||
339 | * @return string |
||
340 | */ |
||
341 | public static function round(Context $context, $value) |
||
346 | } |
||
347 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: