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 |
||
8 | class Str |
||
|
|||
9 | { |
||
10 | /** |
||
11 | * The cache of snake-cased words. |
||
12 | * |
||
13 | * @var array |
||
14 | */ |
||
15 | protected static $snakeCache = []; |
||
16 | |||
17 | /** |
||
18 | * The cache of camel-cased words. |
||
19 | * |
||
20 | * @var array |
||
21 | */ |
||
22 | protected static $camelCache = []; |
||
23 | |||
24 | /** |
||
25 | * The cache of studly-cased words. |
||
26 | * |
||
27 | * @var array |
||
28 | */ |
||
29 | protected static $studlyCache = []; |
||
30 | |||
31 | /** |
||
32 | * Transliterate a UTF-8 value to ASCII. |
||
33 | * |
||
34 | * @param string $value |
||
35 | * |
||
36 | * @return string |
||
37 | */ |
||
38 | public static function ascii($value) |
||
46 | |||
47 | /** |
||
48 | * Convert a value to camel case. |
||
49 | * |
||
50 | * @param string $value |
||
51 | * |
||
52 | * @return string |
||
53 | */ |
||
54 | public static function camel($value) |
||
62 | |||
63 | /** |
||
64 | * Determine if a given string contains a given substring. |
||
65 | * |
||
66 | * @param string $haystack |
||
67 | * @param string|array $needles |
||
68 | * |
||
69 | * @return bool |
||
70 | */ |
||
71 | View Code Duplication | public static function contains($haystack, $needles) |
|
81 | |||
82 | /** |
||
83 | * Determine if a given string ends with a given substring. |
||
84 | * |
||
85 | * @param string $haystack |
||
86 | * @param string|array $needles |
||
87 | * |
||
88 | * @return bool |
||
89 | */ |
||
90 | View Code Duplication | public static function endsWith($haystack, $needles) |
|
100 | |||
101 | /** |
||
102 | * Cap a string with a single instance of a given value. |
||
103 | * |
||
104 | * @param string $value |
||
105 | * @param string $cap |
||
106 | * |
||
107 | * @return string |
||
108 | */ |
||
109 | public static function finish($value, $cap) |
||
115 | |||
116 | /** |
||
117 | * Determine if a given string matches a given pattern. |
||
118 | * |
||
119 | * @param string $pattern |
||
120 | * @param string $value |
||
121 | * |
||
122 | * @return bool |
||
123 | */ |
||
124 | public static function is($pattern, $value) |
||
139 | |||
140 | /** |
||
141 | * Return the length of the given string. |
||
142 | * |
||
143 | * @param string $value |
||
144 | * |
||
145 | * @return int |
||
146 | */ |
||
147 | public static function length($value) |
||
151 | |||
152 | /** |
||
153 | * Limit the number of characters in a string. |
||
154 | * |
||
155 | * @param string $value |
||
156 | * @param int $limit |
||
157 | * @param string $end |
||
158 | * |
||
159 | * @return string |
||
160 | */ |
||
161 | public static function limit($value, $limit = 100, $end = '...') |
||
169 | |||
170 | /** |
||
171 | * Convert the given string to lower-case. |
||
172 | * |
||
173 | * @param string $value |
||
174 | * |
||
175 | * @return string |
||
176 | */ |
||
177 | public static function lower($value) |
||
181 | |||
182 | /** |
||
183 | * Limit the number of words in a string. |
||
184 | * |
||
185 | * @param string $value |
||
186 | * @param int $words |
||
187 | * @param string $end |
||
188 | * |
||
189 | * @return string |
||
190 | */ |
||
191 | public static function words($value, $words = 100, $end = '...') |
||
201 | |||
202 | /** |
||
203 | * Parse a Class@method style callback into class and method. |
||
204 | * |
||
205 | * @param string $callback |
||
206 | * @param string $default |
||
207 | * |
||
208 | * @return array |
||
209 | */ |
||
210 | public static function parseCallback($callback, $default) |
||
214 | |||
215 | /** |
||
216 | * Generate a more truly "random" alpha-numeric string. |
||
217 | * |
||
218 | * @param int $length |
||
219 | * |
||
220 | * @throws \RuntimeException |
||
221 | * |
||
222 | * @return string |
||
223 | */ |
||
224 | public static function random($length = 16) |
||
238 | |||
239 | /** |
||
240 | * Generate a more truly "random" bytes. |
||
241 | * |
||
242 | * @param int $length |
||
243 | * |
||
244 | * @throws \RuntimeException |
||
245 | * |
||
246 | * @return string |
||
247 | * |
||
248 | * @deprecated since version 5.2. Use random_bytes instead. |
||
249 | */ |
||
250 | public static function randomBytes($length = 16) |
||
266 | |||
267 | /** |
||
268 | * Generate a "random" alpha-numeric string. |
||
269 | * |
||
270 | * Should not be considered sufficient for cryptography, etc. |
||
271 | * |
||
272 | * @param int $length |
||
273 | * |
||
274 | * @return string |
||
275 | */ |
||
276 | public static function quickRandom($length = 16) |
||
282 | |||
283 | /** |
||
284 | * Compares two strings using a constant-time algorithm. |
||
285 | * |
||
286 | * Note: This method will leak length information. |
||
287 | * |
||
288 | * Note: Adapted from Symfony\Component\Security\Core\Util\StringUtils. |
||
289 | * |
||
290 | * @param string $knownString |
||
291 | * @param string $userInput |
||
292 | * |
||
293 | * @return bool |
||
294 | * |
||
295 | * @deprecated since version 5.2. Use hash_equals instead. |
||
296 | */ |
||
297 | public static function equals($knownString, $userInput) |
||
301 | |||
302 | /** |
||
303 | * Replace the first occurrence of a given value in the string. |
||
304 | * |
||
305 | * @param string $search |
||
306 | * @param string $replace |
||
307 | * @param string $subject |
||
308 | * |
||
309 | * @return string |
||
310 | */ |
||
311 | View Code Duplication | public static function replaceFirst($search, $replace, $subject) |
|
321 | |||
322 | /** |
||
323 | * Replace the last occurrence of a given value in the string. |
||
324 | * |
||
325 | * @param string $search |
||
326 | * @param string $replace |
||
327 | * @param string $subject |
||
328 | * |
||
329 | * @return string |
||
330 | */ |
||
331 | View Code Duplication | public static function replaceLast($search, $replace, $subject) |
|
341 | |||
342 | /** |
||
343 | * Convert the given string to upper-case. |
||
344 | * |
||
345 | * @param string $value |
||
346 | * |
||
347 | * @return string |
||
348 | */ |
||
349 | public static function upper($value) |
||
353 | |||
354 | /** |
||
355 | * Convert the given string to title case. |
||
356 | * |
||
357 | * @param string $value |
||
358 | * |
||
359 | * @return string |
||
360 | */ |
||
361 | public static function title($value) |
||
365 | |||
366 | /** |
||
367 | * Generate a URL friendly "slug" from a given string. |
||
368 | * |
||
369 | * @param string $title |
||
370 | * @param string $separator |
||
371 | * |
||
372 | * @return string |
||
373 | */ |
||
374 | public static function slug($title, $separator = '-') |
||
391 | |||
392 | /** |
||
393 | * Convert a string to snake case. |
||
394 | * |
||
395 | * @param string $value |
||
396 | * @param string $delimiter |
||
397 | * |
||
398 | * @return string |
||
399 | */ |
||
400 | public static function snake($value, $delimiter = '_') |
||
416 | |||
417 | /** |
||
418 | * Determine if a given string starts with a given substring. |
||
419 | * |
||
420 | * @param string $haystack |
||
421 | * @param string|array $needles |
||
422 | * |
||
423 | * @return bool |
||
424 | */ |
||
425 | View Code Duplication | public static function startsWith($haystack, $needles) |
|
435 | |||
436 | /** |
||
437 | * Convert a value to studly caps case. |
||
438 | * |
||
439 | * @param string $value |
||
440 | * |
||
441 | * @return string |
||
442 | */ |
||
443 | public static function studly($value) |
||
455 | |||
456 | /** |
||
457 | * Returns the portion of string specified by the start and length parameters. |
||
458 | * |
||
459 | * @param string $string |
||
460 | * @param int $start |
||
461 | * @param int|null $length |
||
462 | * |
||
463 | * @return string |
||
464 | */ |
||
465 | public static function substr($string, $start, $length = null) |
||
469 | |||
470 | /** |
||
471 | * Make a string's first character uppercase. |
||
472 | * |
||
473 | * @param string $string |
||
474 | * |
||
475 | * @return string |
||
476 | */ |
||
477 | public static function ucfirst($string) |
||
481 | |||
482 | /** |
||
483 | * Returns the replacements for the ascii method. |
||
484 | * |
||
485 | * Note: Adapted from Stringy\Stringy. |
||
486 | * |
||
487 | * @see https://github.com/danielstjules/Stringy/blob/2.2.0/LICENSE.txt |
||
488 | * |
||
489 | * @return array |
||
490 | */ |
||
491 | protected static function charsArray() |
||
615 | } |
||
616 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.