Complex classes like PhpString 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 PhpString, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class PhpString |
||
9 | { |
||
10 | |||
11 | /** |
||
12 | * A locale independent basename() implementation |
||
13 | * |
||
14 | * works around a bug in PHP's basename() implementation |
||
15 | * |
||
16 | * @param string $path A path |
||
17 | * @param string $suffix If the name component ends in suffix this will also be cut off |
||
18 | * @return string |
||
19 | * @link https://bugs.php.net/bug.php?id=37738 |
||
20 | * |
||
21 | * @see basename() |
||
22 | */ |
||
23 | public static function basename($path, $suffix = '') |
||
38 | |||
39 | /** |
||
40 | * Unicode aware replacement for strlen() |
||
41 | * |
||
42 | * utf8_decode() converts characters that are not in ISO-8859-1 |
||
43 | * to '?', which, for the purpose of counting, is alright - It's |
||
44 | * even faster than mb_strlen. |
||
45 | * |
||
46 | * @param string $string |
||
47 | * @return int |
||
48 | * @see utf8_decode() |
||
49 | * |
||
50 | * @author <chernyshevsky at hotmail dot com> |
||
51 | * @see strlen() |
||
52 | */ |
||
53 | public static function strlen($string) |
||
69 | |||
70 | /** |
||
71 | * UTF-8 aware alternative to substr |
||
72 | * |
||
73 | * Return part of a string given character offset (and optionally length) |
||
74 | * |
||
75 | * @param string $str |
||
76 | * @param int $offset number of UTF-8 characters offset (from left) |
||
77 | * @param int $length (optional) length in UTF-8 characters from offset |
||
78 | * @return string |
||
79 | * @author Harry Fuecks <[email protected]> |
||
80 | * @author Chris Smith <[email protected]> |
||
81 | * |
||
82 | */ |
||
83 | public static function substr($str, $offset, $length = null) |
||
174 | |||
175 | // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
||
176 | /** |
||
177 | * Unicode aware replacement for substr_replace() |
||
178 | * |
||
179 | * @param string $string input string |
||
180 | * @param string $replacement the replacement |
||
181 | * @param int $start the replacing will begin at the start'th offset into string. |
||
182 | * @param int $length If given and is positive, it represents the length of the portion of string which is |
||
183 | * to be replaced. If length is zero then this function will have the effect of inserting |
||
184 | * replacement into string at the given start offset. |
||
185 | * @return string |
||
186 | * @see substr_replace() |
||
187 | * |
||
188 | * @author Andreas Gohr <[email protected]> |
||
189 | */ |
||
190 | public static function substr_replace($string, $replacement, $start, $length = 0) |
||
198 | // phpcs:enable PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
||
199 | |||
200 | /** |
||
201 | * Unicode aware replacement for ltrim() |
||
202 | * |
||
203 | * @param string $str |
||
204 | * @param string $charlist |
||
205 | * @return string |
||
206 | * @see ltrim() |
||
207 | * |
||
208 | * @author Andreas Gohr <[email protected]> |
||
209 | */ |
||
210 | public static function ltrim($str, $charlist = '') |
||
219 | |||
220 | /** |
||
221 | * Unicode aware replacement for rtrim() |
||
222 | * |
||
223 | * @param string $str |
||
224 | * @param string $charlist |
||
225 | * @return string |
||
226 | * @see rtrim() |
||
227 | * |
||
228 | * @author Andreas Gohr <[email protected]> |
||
229 | */ |
||
230 | public static function rtrim($str, $charlist = '') |
||
239 | |||
240 | /** |
||
241 | * Unicode aware replacement for trim() |
||
242 | * |
||
243 | * @param string $str |
||
244 | * @param string $charlist |
||
245 | * @return string |
||
246 | * @see trim() |
||
247 | * |
||
248 | * @author Andreas Gohr <[email protected]> |
||
249 | */ |
||
250 | public static function trim($str, $charlist = '') |
||
256 | |||
257 | /** |
||
258 | * This is a unicode aware replacement for strtolower() |
||
259 | * |
||
260 | * Uses mb_string extension if available |
||
261 | * |
||
262 | * @param string $string |
||
263 | * @return string |
||
264 | * @see \dokuwiki\Utf8\PhpString::strtoupper() |
||
265 | * |
||
266 | * @author Leo Feyer <[email protected]> |
||
267 | * @see strtolower() |
||
268 | */ |
||
269 | public static function strtolower($string) |
||
279 | |||
280 | /** |
||
281 | * This is a unicode aware replacement for strtoupper() |
||
282 | * |
||
283 | * Uses mb_string extension if available |
||
284 | * |
||
285 | * @param string $string |
||
286 | * @return string |
||
287 | * @see \dokuwiki\Utf8\PhpString::strtoupper() |
||
288 | * |
||
289 | * @author Leo Feyer <[email protected]> |
||
290 | * @see strtoupper() |
||
291 | */ |
||
292 | public static function strtoupper($string) |
||
298 | |||
299 | |||
300 | /** |
||
301 | * UTF-8 aware alternative to ucfirst |
||
302 | * Make a string's first character uppercase |
||
303 | * |
||
304 | * @param string $str |
||
305 | * @return string with first character as upper case (if applicable) |
||
306 | * @author Harry Fuecks |
||
307 | * |
||
308 | */ |
||
309 | public static function ucfirst($str) |
||
321 | |||
322 | /** |
||
323 | * UTF-8 aware alternative to ucwords |
||
324 | * Uppercase the first character of each word in a string |
||
325 | * |
||
326 | * @param string $str |
||
327 | * @return string with first char of each word uppercase |
||
328 | * @author Harry Fuecks |
||
329 | * @see http://php.net/ucwords |
||
330 | * |
||
331 | */ |
||
332 | public static function ucwords($str) |
||
350 | |||
351 | /** |
||
352 | * This is an Unicode aware replacement for strpos |
||
353 | * |
||
354 | * @param string $haystack |
||
355 | * @param string $needle |
||
356 | * @param integer $offset |
||
357 | * @return integer |
||
358 | * @author Leo Feyer <[email protected]> |
||
359 | * @see strpos() |
||
360 | * |
||
361 | */ |
||
362 | public static function strpos($haystack, $needle, $offset = 0) |
||
381 | |||
382 | |||
383 | } |
||
384 |
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.