Complex classes like Strings 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 Strings, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class Strings |
||
13 | { |
||
14 | /** |
||
15 | * Changes underscored string to the camel case. |
||
16 | * |
||
17 | * Example: |
||
18 | * <code> |
||
19 | * $string = 'lannisters_always_pay_their_debts'; |
||
20 | * $camelcase = Strings::underscoreToCamelCase($string); |
||
21 | * </code> |
||
22 | * Result: |
||
23 | * <code> |
||
24 | * LannistersAlwaysPayTheirDebts |
||
25 | * </code> |
||
26 | * |
||
27 | * @param string $string |
||
28 | * @return string |
||
29 | */ |
||
30 | public static function underscoreToCamelCase($string) |
||
31 | { |
||
32 | $words = explode('_', $string); |
||
33 | $return = ''; |
||
34 | foreach ($words as $word) { |
||
35 | $return .= ucfirst(trim($word)); |
||
36 | } |
||
37 | return $return; |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * Changes camel case string to underscored. |
||
42 | * |
||
43 | * Example: |
||
44 | * <code> |
||
45 | * $string = 'LannistersAlwaysPayTheirDebts'; |
||
46 | * $underscored = Strings::camelCaseToUnderscore($string); |
||
47 | * </code> |
||
48 | * Result: |
||
49 | * <code> |
||
50 | * lannisters_always_pay_their_debts |
||
51 | * </code> |
||
52 | * |
||
53 | * @param string $string |
||
54 | * @return string |
||
55 | */ |
||
56 | public static function camelCaseToUnderscore($string) |
||
57 | { |
||
58 | return strtolower(preg_replace('/([a-z])([A-Z])/', '$1_$2', $string)); |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Returns a new string without the given prefix. |
||
63 | * |
||
64 | * Example: |
||
65 | * <code> |
||
66 | * $string = 'prefixRest'; |
||
67 | * $withoutPrefix = Strings::removePrefix($string, 'prefix'); |
||
68 | * </code> |
||
69 | * Result: |
||
70 | * <code> |
||
71 | * Rest |
||
72 | * </code> |
||
73 | * |
||
74 | * @param string $string |
||
75 | * @param string $prefix |
||
76 | * @return string |
||
77 | */ |
||
78 | public static function removePrefix($string, $prefix) |
||
79 | { |
||
80 | if (self::startsWith($string, $prefix)) { |
||
81 | return substr($string, strlen($prefix)); |
||
82 | } |
||
83 | return $string; |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * Removes prefixes defined in array from string. |
||
88 | * |
||
89 | * Example: |
||
90 | * <code> |
||
91 | * $string = 'prefixRest'; |
||
92 | * $withoutPrefix = Strings::removePrefixes($string, array('pre', 'fix')); |
||
93 | * </code> |
||
94 | * Result: |
||
95 | * <code> |
||
96 | * Rest |
||
97 | * </code> |
||
98 | * |
||
99 | * @param string $string |
||
100 | * @param array $prefixes |
||
101 | * @return mixed |
||
102 | */ |
||
103 | public static function removePrefixes($string, array $prefixes) |
||
104 | { |
||
105 | return array_reduce($prefixes, function ($string, $prefix) { |
||
106 | return Strings::removePrefix($string, $prefix); |
||
107 | }, $string); |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Returns a new string without the given suffix. |
||
112 | * |
||
113 | * Example: |
||
114 | * <code> |
||
115 | * $string = 'JohnSnow'; |
||
116 | * $withoutSuffix = Strings::removeSuffix($string, 'Snow'); |
||
117 | * </code> |
||
118 | * Result: |
||
119 | * <code> |
||
120 | * John |
||
121 | * </code> |
||
122 | * |
||
123 | * @param string $string |
||
124 | * @param string $suffix |
||
125 | * @return string |
||
126 | */ |
||
127 | public static function removeSuffix($string, $suffix) |
||
128 | { |
||
129 | if (self::endsWith($string, $suffix)) { |
||
130 | return substr($string, 0, -strlen($suffix)); |
||
131 | } |
||
132 | return $string; |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * Method checks if string starts with $prefix. |
||
137 | * |
||
138 | * Example: |
||
139 | * <code> |
||
140 | * $string = 'prefixRest'; |
||
141 | * $result = Strings::startsWith($string, 'prefix'); |
||
142 | * </code> |
||
143 | * Result: |
||
144 | * <code> |
||
145 | * true |
||
146 | * </code> |
||
147 | * |
||
148 | * @param string $string |
||
149 | * @param string $prefix |
||
150 | * @return bool |
||
151 | */ |
||
152 | public static function startsWith($string, $prefix) |
||
153 | { |
||
154 | return $string && $prefix && strpos($string, $prefix) === 0; |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * Method checks if string ends with $suffix. |
||
159 | * |
||
160 | * Example: |
||
161 | * <code> |
||
162 | * $string = 'StringSuffix'; |
||
163 | * $result = Strings::endsWith($string, 'Suffix'); |
||
164 | * </code> |
||
165 | * Result: |
||
166 | * <code> |
||
167 | * true |
||
168 | * </code> |
||
169 | * |
||
170 | * @param string $string |
||
171 | * @param string $suffix |
||
172 | * @return bool |
||
173 | */ |
||
174 | public static function endsWith($string, $suffix) |
||
175 | { |
||
176 | return $string && $suffix && substr($string, -strlen($suffix)) === $suffix; |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * Determines whether two strings contain the same data, ignoring the case of the letters in the strings. |
||
181 | * |
||
182 | * Example: |
||
183 | * <code> |
||
184 | * $equal = Strings::equalsIgnoreCase('ABC123', 'abc123'); |
||
185 | * </code> |
||
186 | * Result: |
||
187 | * <code> |
||
188 | * true |
||
189 | * </code> |
||
190 | * |
||
191 | * @param string $string1 |
||
192 | * @param string $string2 |
||
193 | * @return bool |
||
194 | */ |
||
195 | public static function equalsIgnoreCase($string1, $string2) |
||
196 | { |
||
197 | return strtolower($string1) == strtolower($string2); |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * Removes all occurrences of a substring from string. |
||
202 | * |
||
203 | * Example: |
||
204 | * <code> |
||
205 | * $string = 'winter is coming???!!!'; |
||
206 | * $result = Strings::remove($string, '???'); |
||
207 | * </code> |
||
208 | * Result: |
||
209 | * <code> |
||
210 | * winter is coming!!! |
||
211 | * </code> |
||
212 | * |
||
213 | * @param string $string |
||
214 | * @param string $stringToRemove |
||
215 | * @return mixed |
||
216 | */ |
||
217 | public static function remove($string, $stringToRemove) |
||
218 | { |
||
219 | return $string && $stringToRemove ? str_replace($stringToRemove, '', $string) : $string; |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * Adds suffix to the string. |
||
224 | * |
||
225 | * Example: |
||
226 | * <code> |
||
227 | * $string = 'Daenerys'; |
||
228 | * $stringWithSuffix = Strings::appendSuffix($string, ' Targaryen'); |
||
229 | * </code> |
||
230 | * Result: |
||
231 | * <code> |
||
232 | * Daenerys Targaryen |
||
233 | * </code> |
||
234 | * |
||
235 | * @param string $string |
||
236 | * @param string $suffix |
||
237 | * @return string |
||
238 | */ |
||
239 | public static function appendSuffix($string, $suffix = '') |
||
240 | { |
||
241 | return $string ? $string . $suffix : $string; |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * Adds prefix to the string. |
||
246 | * |
||
247 | * Example: |
||
248 | * <code> |
||
249 | * $string = 'Targaryen'; |
||
250 | * $stringWithPrefix = Strings::appendPrefix($string, 'Daenerys '); |
||
251 | * </code> |
||
252 | * Result: |
||
253 | * <code> |
||
254 | * Daenerys Targaryen |
||
255 | * </code> |
||
256 | * |
||
257 | * @param string $string |
||
258 | * @param string $prefix |
||
259 | * @return string |
||
260 | */ |
||
261 | public static function appendPrefix($string, $prefix = '') |
||
262 | { |
||
263 | return $string ? $prefix . $string : $string; |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * Converts a word into the format for an Ouzo table name. Converts 'ModelName' to 'model_names'. |
||
268 | * |
||
269 | * Example: |
||
270 | * <code> |
||
271 | * $class = "BigFoot"; |
||
272 | * $table = Strings::tableize($class); |
||
273 | * </code> |
||
274 | * Result: |
||
275 | * <code> |
||
276 | * BigFeet |
||
277 | * </code> |
||
278 | * |
||
279 | * @param string $class |
||
280 | * @return string |
||
281 | */ |
||
282 | public static function tableize($class) |
||
283 | { |
||
284 | $underscored = Strings::camelCaseToUnderscore($class); |
||
285 | $parts = explode('_', $underscored); |
||
286 | $suffix = Inflector::pluralize(array_pop($parts)); |
||
287 | $parts[] = $suffix; |
||
288 | return implode('_', $parts); |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * Changes new lines to <br> and converts special characters to HTML entities. |
||
293 | * |
||
294 | * Example: |
||
295 | * <code> |
||
296 | * $string = "My name is <strong>Reek</strong> \nit rhymes with leek"; |
||
297 | * $escaped = Strings::escapeNewLines($string); |
||
298 | * </code> |
||
299 | * Result: |
||
300 | * <code> |
||
301 | * My name is <strong>Reek</strong> <br />it rhymes with leek |
||
302 | * </code> |
||
303 | * |
||
304 | * @param string $string |
||
305 | * @return string |
||
306 | */ |
||
307 | public static function escapeNewLines($string) |
||
308 | { |
||
309 | $string = htmlspecialchars($string); |
||
310 | return nl2br($string); |
||
311 | } |
||
312 | |||
313 | /** |
||
314 | * Alias for html_entity_decode() with UTF-8 and defined flag ENT_COMPAT. |
||
315 | * |
||
316 | * @param string $text |
||
317 | * @return string |
||
318 | */ |
||
319 | public static function htmlEntityDecode($text) |
||
320 | { |
||
321 | return html_entity_decode($text, ENT_COMPAT, 'UTF-8'); |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * Alias for htmlentities() with UTF-8 and flags ENT_COMPAT and ENT_SUBSTITUTE (ENT_IGNORE for php <= 5.3). |
||
326 | * |
||
327 | * @param string $text |
||
328 | * @return string |
||
329 | */ |
||
330 | public static function htmlEntities($text) |
||
331 | { |
||
332 | $flag = defined('ENT_SUBSTITUTE') ? ENT_SUBSTITUTE : ENT_IGNORE; |
||
333 | $htmlentities = htmlentities($text, ENT_COMPAT | $flag, 'UTF-8'); |
||
334 | $htmlentities = str_replace(array('Ó', 'ó'), array('Ó', 'ó'), $htmlentities); |
||
335 | return $htmlentities; |
||
336 | } |
||
337 | |||
338 | /** |
||
339 | * Method checks if string representations of two objects are equal. |
||
340 | * |
||
341 | * Example: |
||
342 | * <code> |
||
343 | * $result = Strings::equal('0123', 123); |
||
344 | * </code> |
||
345 | * Result: |
||
346 | * <code> |
||
347 | * false |
||
348 | * </code> |
||
349 | * |
||
350 | * @param mixed $object1 |
||
351 | * @param mixed $object2 |
||
352 | * @return bool |
||
353 | */ |
||
354 | public static function equal($object1, $object2) |
||
355 | { |
||
356 | return (string)$object1 === (string)$object2; |
||
357 | } |
||
358 | |||
359 | /** |
||
360 | * Method checks if string is blank. |
||
361 | * |
||
362 | * Example: |
||
363 | * <code> |
||
364 | * $result = Strings::isBlank('0'); |
||
365 | * </code> |
||
366 | * Result: |
||
367 | * <code> |
||
368 | * false |
||
369 | * </code> |
||
370 | * |
||
371 | * @param string $string |
||
372 | * @return bool |
||
373 | */ |
||
374 | public static function isBlank($string) |
||
375 | { |
||
376 | return mb_strlen(trim($string)) == 0; |
||
377 | } |
||
378 | |||
379 | /** |
||
380 | * Method checks if string is not blank. |
||
381 | * |
||
382 | * Example: |
||
383 | * <code> |
||
384 | * $result = Strings::isNotBlank('0'); |
||
385 | * </code> |
||
386 | * Result: |
||
387 | * <code> |
||
388 | * true |
||
389 | * </code> |
||
390 | * |
||
391 | * @param string $string |
||
392 | * @return bool |
||
393 | */ |
||
394 | public static function isNotBlank($string) |
||
395 | { |
||
396 | return !Strings::isBlank($string); |
||
397 | } |
||
398 | |||
399 | /** |
||
400 | * Abbreviate - abbreviates a string using ellipsis. |
||
401 | * |
||
402 | * Example: |
||
403 | * <code> |
||
404 | * $result = Strings::abbreviate('ouzo is great', 5); |
||
405 | * </code> |
||
406 | * Result: |
||
407 | * <code> |
||
408 | * ouzo ... |
||
409 | * </code> |
||
410 | * |
||
411 | * @param string $string |
||
412 | * @param string $maxWidth |
||
413 | * @return string |
||
414 | */ |
||
415 | public static function abbreviate($string, $maxWidth) |
||
416 | { |
||
417 | if (mb_strlen($string) > $maxWidth) { |
||
418 | return mb_substr($string, 0, $maxWidth) . '...'; |
||
419 | } |
||
420 | return $string; |
||
421 | } |
||
422 | |||
423 | |||
424 | /** |
||
425 | * Removes control characters from both ends of this string returning null if the string is empty ("") after the trim or if it is null. |
||
426 | * |
||
427 | * Example: |
||
428 | * <code> |
||
429 | * $result = Strings::trimToNull(' '); |
||
430 | * </code> |
||
431 | * Result: |
||
432 | * <code> |
||
433 | * null |
||
434 | * </code> |
||
435 | * |
||
436 | * @param string $string |
||
437 | * @return string |
||
438 | */ |
||
439 | public static function trimToNull($string) |
||
440 | { |
||
441 | $string = trim($string); |
||
442 | if (mb_strlen($string) == 0) { |
||
443 | return null; |
||
444 | } |
||
445 | return $string; |
||
446 | } |
||
447 | |||
448 | /** |
||
449 | * Replace all occurrences of placeholder in string with values from associative array. |
||
450 | * |
||
451 | * Example: |
||
452 | * <code> |
||
453 | * $sprintfString = "This is %{what}! %{what}? This is %{place}!"; |
||
454 | * $assocArray = array( |
||
455 | * 'what' => 'madness', |
||
456 | * 'place' => 'Sparta' |
||
457 | * ); |
||
458 | * </code> |
||
459 | * Result: |
||
460 | * <code> |
||
461 | * 'This is madness! madness? This is Sparta!' |
||
462 | * </code> |
||
463 | * |
||
464 | * @param string $string |
||
465 | * @param array $params |
||
466 | * @return string |
||
467 | */ |
||
468 | public static function sprintAssoc($string, $params) |
||
469 | { |
||
470 | foreach ($params as $k => $v) { |
||
471 | $string = preg_replace("/%{($k)}/", $v, $string); |
||
472 | } |
||
473 | return $string; |
||
474 | } |
||
475 | |||
476 | /** |
||
477 | * Replace all occurrences of placeholder in string with values from associative array. |
||
478 | * When no value for placeholder is found in array, a default empty value is used if not otherwise specified. |
||
479 | * |
||
480 | * Example: |
||
481 | * <code> |
||
482 | * $sprintfString = "This is %{what}! %{what}? This is %{place}!"; |
||
483 | * $assocArray = array( |
||
484 | * 'what' => 'madness', |
||
485 | * 'place' => 'Sparta' |
||
486 | * ); |
||
487 | * </code> |
||
488 | * Result: |
||
489 | * <code> |
||
490 | * 'This is madness! madness? This is Sparta!' |
||
491 | * </code> |
||
492 | * |
||
493 | * @param string $string |
||
494 | * @param array $params |
||
495 | * @param string $default |
||
496 | * @return string |
||
497 | */ |
||
498 | public static function sprintAssocDefault($string, $params, $default = '') |
||
499 | { |
||
500 | foreach ($params as $k => $v) { |
||
501 | $string = preg_replace("/%{($k)}/", $v, $string); |
||
502 | } |
||
503 | $string = preg_replace("/%{\w*}/", $default, $string); |
||
504 | return $string; |
||
505 | } |
||
506 | |||
507 | /** |
||
508 | * Checks if string contains the substring. |
||
509 | * |
||
510 | * @param string $string |
||
511 | * @param string $substring |
||
512 | * @return bool |
||
513 | */ |
||
514 | public static function contains($string, $substring) |
||
515 | { |
||
516 | return strstr($string, $substring) !== false; |
||
517 | } |
||
518 | |||
519 | /** |
||
520 | * Gets the substring before the first occurrence of a separator. The separator is not returned. |
||
521 | * |
||
522 | * @param string $string |
||
523 | * @param string $separator |
||
524 | * @return string |
||
525 | */ |
||
526 | public static function substringBefore($string, $separator) |
||
527 | { |
||
528 | $pos = mb_strpos($string, $separator); |
||
529 | return $pos !== false ? mb_substr($string, 0, $pos) : $string; |
||
530 | } |
||
531 | |||
532 | /** |
||
533 | * @param string $subject |
||
534 | * @param string $search |
||
535 | * @param string $replace |
||
536 | * @param int $nth |
||
537 | * @return string |
||
538 | */ |
||
539 | public static function replaceNth($subject, $search, $replace, $nth) |
||
540 | { |
||
541 | $found = preg_match_all('/' . $search . '/', $subject, $matches, PREG_OFFSET_CAPTURE); |
||
542 | if (false !== $found && $found > $nth) { |
||
543 | return substr_replace($subject, $replace, $matches[0][$nth][1], strlen($search)); |
||
544 | } |
||
545 | return $subject; |
||
546 | } |
||
547 | } |
||
548 |