Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Str 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 Str, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class Str |
||
12 | { |
||
13 | use Macroable; |
||
14 | |||
15 | /** |
||
16 | * The cache of snake-cased words. |
||
17 | * |
||
18 | * @var array |
||
19 | */ |
||
20 | protected static $snakeCache = []; |
||
21 | |||
22 | /** |
||
23 | * The cache of camel-cased words. |
||
24 | * |
||
25 | * @var array |
||
26 | */ |
||
27 | protected static $camelCache = []; |
||
28 | |||
29 | /** |
||
30 | * The cache of studly-cased words. |
||
31 | * |
||
32 | * @var array |
||
33 | */ |
||
34 | protected static $studlyCache = []; |
||
35 | |||
36 | /** |
||
37 | * Return the remainder of a string after a given value. |
||
38 | * |
||
39 | * @param string $subject |
||
40 | * @param string $search |
||
41 | * @return string |
||
42 | */ |
||
43 | 1 | public static function after($subject, $search) |
|
47 | |||
48 | /** |
||
49 | * Transliterate a UTF-8 value to ASCII. |
||
50 | * |
||
51 | * @param string $value |
||
52 | * @param string $language |
||
53 | * @return string |
||
54 | */ |
||
55 | 3 | public static function ascii($value, $language = 'en') |
|
69 | |||
70 | /** |
||
71 | * Get the portion of a string before a given value. |
||
72 | * |
||
73 | * @param string $subject |
||
74 | * @param string $search |
||
75 | * @return string |
||
76 | */ |
||
77 | 1 | public static function before($subject, $search) |
|
81 | |||
82 | /** |
||
83 | * Convert a value to camel case. |
||
84 | * |
||
85 | * @param string $value |
||
86 | * @return string |
||
87 | */ |
||
88 | 1 | public static function camel($value) |
|
96 | |||
97 | /** |
||
98 | * Determine if a given string contains a given substring. |
||
99 | * |
||
100 | * @param string $haystack |
||
101 | * @param string|array $needles |
||
102 | * @return bool |
||
103 | */ |
||
104 | 2 | public static function contains($haystack, $needles) |
|
114 | |||
115 | /** |
||
116 | * Determine if a given string ends with a given substring. |
||
117 | * |
||
118 | * @param string $haystack |
||
119 | * @param string|array $needles |
||
120 | * @return bool |
||
121 | */ |
||
122 | 1 | View Code Duplication | public static function endsWith($haystack, $needles) |
132 | |||
133 | /** |
||
134 | * Cap a string with a single instance of a given value. |
||
135 | * |
||
136 | * @param string $value |
||
137 | * @param string $cap |
||
138 | * @return string |
||
139 | */ |
||
140 | 1 | public static function finish($value, $cap) |
|
146 | |||
147 | /** |
||
148 | * Determine if a given string matches a given pattern. |
||
149 | * |
||
150 | * @param string|array $pattern |
||
151 | * @param string $value |
||
152 | * @return bool |
||
153 | */ |
||
154 | 1 | public static function is($pattern, $value) |
|
184 | |||
185 | /** |
||
186 | * Convert a string to kebab case. |
||
187 | * |
||
188 | * @param string $value |
||
189 | * @return string |
||
190 | */ |
||
191 | 1 | public static function kebab($value) |
|
195 | |||
196 | /** |
||
197 | * Return the length of the given string. |
||
198 | * |
||
199 | * @param string $value |
||
200 | * @param string $encoding |
||
201 | * @return int |
||
202 | */ |
||
203 | 3 | public static function length($value, $encoding = null) |
|
211 | |||
212 | /** |
||
213 | * Limit the number of characters in a string. |
||
214 | * |
||
215 | * @param string $value |
||
216 | * @param int $limit |
||
217 | * @param string $end |
||
218 | * @return string |
||
219 | */ |
||
220 | 1 | public static function limit($value, $limit = 100, $end = '...') |
|
228 | |||
229 | /** |
||
230 | * Convert the given string to lower-case. |
||
231 | * |
||
232 | * @param string $value |
||
233 | * @return string |
||
234 | */ |
||
235 | 3 | public static function lower($value) |
|
239 | |||
240 | /** |
||
241 | * Limit the number of words in a string. |
||
242 | * |
||
243 | * @param string $value |
||
244 | * @param int $words |
||
245 | * @param string $end |
||
246 | * @return string |
||
247 | */ |
||
248 | 3 | public static function words($value, $words = 100, $end = '...') |
|
258 | |||
259 | /** |
||
260 | * Parse a Class@method style callback into class and method. |
||
261 | * |
||
262 | * @param string $callback |
||
263 | * @param string|null $default |
||
264 | * @return array |
||
265 | */ |
||
266 | 1 | public static function parseCallback($callback, $default = null) |
|
270 | |||
271 | /** |
||
272 | * Get the plural form of an English word. |
||
273 | * |
||
274 | * @param string $value |
||
275 | * @param int $count |
||
276 | * @return string |
||
277 | */ |
||
278 | public static function plural($value, $count = 2) |
||
282 | |||
283 | /** |
||
284 | * Generate a more truly "random" alpha-numeric string. |
||
285 | * |
||
286 | * @param int $length |
||
287 | * @return string |
||
288 | */ |
||
289 | 1 | public static function random($length = 16) |
|
303 | |||
304 | /** |
||
305 | * Replace a given value in the string sequentially with an array. |
||
306 | * |
||
307 | * @param string $search |
||
308 | * @param array $replace |
||
309 | * @param string $subject |
||
310 | * @return string |
||
311 | */ |
||
312 | 1 | public static function replaceArray($search, array $replace, $subject) |
|
320 | |||
321 | /** |
||
322 | * Replace the first occurrence of a given value in the string. |
||
323 | * |
||
324 | * @param string $search |
||
325 | * @param string $replace |
||
326 | * @param string $subject |
||
327 | * @return string |
||
328 | */ |
||
329 | 2 | View Code Duplication | public static function replaceFirst($search, $replace, $subject) |
343 | |||
344 | /** |
||
345 | * Replace the last occurrence of a given value in the string. |
||
346 | * |
||
347 | * @param string $search |
||
348 | * @param string $replace |
||
349 | * @param string $subject |
||
350 | * @return string |
||
351 | */ |
||
352 | 1 | View Code Duplication | public static function replaceLast($search, $replace, $subject) |
362 | |||
363 | /** |
||
364 | * Begin a string with a single instance of a given value. |
||
365 | * |
||
366 | * @param string $value |
||
367 | * @param string $prefix |
||
368 | * @return string |
||
369 | */ |
||
370 | public static function start($value, $prefix) |
||
376 | |||
377 | /** |
||
378 | * Convert the given string to upper-case. |
||
379 | * |
||
380 | * @param string $value |
||
381 | * @return string |
||
382 | */ |
||
383 | 2 | public static function upper($value) |
|
387 | |||
388 | /** |
||
389 | * Convert the given string to title case. |
||
390 | * |
||
391 | * @param string $value |
||
392 | * @return string |
||
393 | */ |
||
394 | 1 | public static function title($value) |
|
398 | |||
399 | /** |
||
400 | * Get the singular form of an English word. |
||
401 | * |
||
402 | * @param string $value |
||
403 | * @return string |
||
404 | */ |
||
405 | public static function singular($value) |
||
409 | |||
410 | /** |
||
411 | * Generate a URL friendly "slug" from a given string. |
||
412 | * |
||
413 | * @param string $title |
||
414 | * @param string $separator |
||
415 | * @param string $language |
||
416 | * @return string |
||
417 | */ |
||
418 | 1 | public static function slug($title, $separator = '-', $language = 'en') |
|
438 | |||
439 | /** |
||
440 | * Convert a string to snake case. |
||
441 | * |
||
442 | * @param string $value |
||
443 | * @param string $delimiter |
||
444 | * @return string |
||
445 | */ |
||
446 | 2 | public static function snake($value, $delimiter = '_') |
|
462 | |||
463 | /** |
||
464 | * Determine if a given string starts with a given substring. |
||
465 | * |
||
466 | * @param string $haystack |
||
467 | * @param string|array $needles |
||
468 | * @return bool |
||
469 | */ |
||
470 | 1 | View Code Duplication | public static function startsWith($haystack, $needles) |
480 | |||
481 | /** |
||
482 | * Convert a value to studly caps case. |
||
483 | * |
||
484 | * @param string $value |
||
485 | * @return string |
||
486 | */ |
||
487 | 2 | public static function studly($value) |
|
499 | |||
500 | /** |
||
501 | * Returns the portion of string specified by the start and length parameters. |
||
502 | * |
||
503 | * @param string $string |
||
504 | * @param int $start |
||
505 | * @param int|null $length |
||
506 | * @return string |
||
507 | */ |
||
508 | 2 | public static function substr($string, $start, $length = null) |
|
512 | |||
513 | /** |
||
514 | * Make a string's first character uppercase. |
||
515 | * |
||
516 | * @param string $string |
||
517 | * @return string |
||
518 | */ |
||
519 | 1 | public static function ucfirst($string) |
|
523 | |||
524 | /** |
||
525 | * Generate a UUID (version 4). |
||
526 | * |
||
527 | * @return \Ramsey\Uuid\Uuid |
||
528 | */ |
||
529 | 1 | public static function uuid() |
|
533 | |||
534 | /** |
||
535 | * Generate a time-ordered UUID (version 4). |
||
536 | * |
||
537 | * @return \Ramsey\Uuid\Uuid |
||
538 | */ |
||
539 | 1 | public static function orderedUuid() |
|
554 | |||
555 | /** |
||
556 | * Returns the replacements for the ascii method. |
||
557 | * |
||
558 | * Note: Adapted from Stringy\Stringy. |
||
559 | * |
||
560 | * @see https://github.com/danielstjules/Stringy/blob/3.1.0/LICENSE.txt |
||
561 | * |
||
562 | * @return array |
||
563 | */ |
||
564 | 3 | protected static function charsArray() |
|
688 | |||
689 | /** |
||
690 | * Returns the language specific replacements for the ascii method. |
||
691 | * |
||
692 | * Note: Adapted from Stringy\Stringy. |
||
693 | * |
||
694 | * @see https://github.com/danielstjules/Stringy/blob/3.1.0/LICENSE.txt |
||
695 | * |
||
696 | * @param string $language |
||
697 | * @return array|null |
||
698 | */ |
||
699 | 3 | protected static function languageSpecificCharsArray($language) |
|
718 | } |
||
719 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.