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 |
||
9 | class Str implements PrimitiveInterface, StringableInterface |
||
10 | { |
||
11 | const PAD_RIGHT = STR_PAD_RIGHT; |
||
12 | const PAD_LEFT = STR_PAD_LEFT; |
||
13 | const PAD_BOTH = STR_PAD_BOTH; |
||
14 | const PREG_NO_FLAGS = 0; |
||
15 | const PREG_SPLIT_NO_EMPTY = PREG_SPLIT_NO_EMPTY; |
||
16 | const PREG_SPLIT_DELIM_CAPTURE = PREG_SPLIT_DELIM_CAPTURE; |
||
17 | const PREG_SPLIT_OFFSET_CAPTURE = PREG_SPLIT_OFFSET_CAPTURE; |
||
18 | const PREG_OFFSET_CAPTURE = PREG_OFFSET_CAPTURE; |
||
19 | |||
20 | private $value; |
||
21 | |||
22 | 168 | public function __construct(string $value) |
|
26 | |||
27 | /** |
||
28 | * {@inheritdoc} |
||
29 | */ |
||
30 | 2 | public function toPrimitive() |
|
34 | |||
35 | /** |
||
36 | * {@inheritdoc} |
||
37 | */ |
||
38 | 108 | public function __toString(): string |
|
42 | |||
43 | /** |
||
44 | * Split the string into a collection of ones |
||
45 | * |
||
46 | * @param string $delimiter |
||
47 | * |
||
48 | * @return StreamInterface<self> |
||
|
|||
49 | */ |
||
50 | 4 | View Code Duplication | public function split(string $delimiter = null): StreamInterface |
62 | |||
63 | /** |
||
64 | * Returns a collection of the string splitted by the given chunk size |
||
65 | * |
||
66 | * @param int $size |
||
67 | * |
||
68 | * @return StreamInterface<self> |
||
69 | */ |
||
70 | 2 | View Code Duplication | public function chunk(int $size = 1): StreamInterface |
81 | |||
82 | /** |
||
83 | * Returns the position of the first occurence of the string |
||
84 | * |
||
85 | * @param string $needle |
||
86 | * @param int $offset |
||
87 | * |
||
88 | * @throws SubstringException If the string is not found |
||
89 | * |
||
90 | * @return int |
||
91 | */ |
||
92 | 8 | View Code Duplication | public function position(string $needle, int $offset = 0): int |
105 | |||
106 | /** |
||
107 | * Replace all occurences of the search string with the replacement one |
||
108 | * |
||
109 | * @param string $search |
||
110 | * @param string $replacement |
||
111 | * |
||
112 | * @return self |
||
113 | */ |
||
114 | 2 | public function replace(string $search, string $replacement): self |
|
122 | |||
123 | /** |
||
124 | * Returns the string following the given delimiter |
||
125 | * |
||
126 | * @param string $delimiter |
||
127 | * |
||
128 | * @throws SubstringException If the string is not found |
||
129 | * |
||
130 | * @return self |
||
131 | */ |
||
132 | 4 | View Code Duplication | public function str(string $delimiter): self |
145 | |||
146 | /** |
||
147 | * Return the string in upper case |
||
148 | * |
||
149 | * @return self |
||
150 | */ |
||
151 | 2 | public function toUpper(): self |
|
155 | |||
156 | /** |
||
157 | * Return the string in lower case |
||
158 | * |
||
159 | * @return self |
||
160 | */ |
||
161 | 2 | public function toLower(): self |
|
165 | |||
166 | /** |
||
167 | * Return the string length |
||
168 | * |
||
169 | * @return int |
||
170 | */ |
||
171 | 4 | public function length(): int |
|
175 | |||
176 | /** |
||
177 | * Reverse the string |
||
178 | * |
||
179 | * @return self |
||
180 | */ |
||
181 | 2 | public function reverse(): self |
|
185 | |||
186 | /** |
||
187 | * Pad to the right |
||
188 | * |
||
189 | * @param int $length |
||
190 | * @param string $character |
||
191 | * |
||
192 | * @return self |
||
193 | */ |
||
194 | 2 | public function rightPad(int $length, string $character = ' '): self |
|
198 | |||
199 | /** |
||
200 | * Pad to the left |
||
201 | * |
||
202 | * @param int $length |
||
203 | * @param string $character |
||
204 | * |
||
205 | * @return self |
||
206 | */ |
||
207 | 2 | public function leftPad(int $length, string $character = ' '): self |
|
211 | |||
212 | /** |
||
213 | * Pad both sides |
||
214 | * |
||
215 | * @param int $length |
||
216 | * @param string $character |
||
217 | * |
||
218 | * @return self |
||
219 | */ |
||
220 | 2 | public function uniPad(int $length, string $character = ' '): self |
|
224 | |||
225 | /** |
||
226 | * Find length of initial segment not matching mask |
||
227 | * |
||
228 | * @param string $mask |
||
229 | * @param int $start |
||
230 | * @param int $length |
||
231 | * |
||
232 | * @return int |
||
233 | */ |
||
234 | 2 | View Code Duplication | public function cspn(string $mask, int $start = 0, int $length = null): int |
249 | |||
250 | /** |
||
251 | * Repeat the string n times |
||
252 | * |
||
253 | * @param int $repeat |
||
254 | * |
||
255 | * @return self |
||
256 | */ |
||
257 | 2 | public function repeat(int $repeat): self |
|
261 | |||
262 | /** |
||
263 | * Shuffle the string |
||
264 | * |
||
265 | * @return self |
||
266 | */ |
||
267 | 2 | public function shuffle(): self |
|
271 | |||
272 | /** |
||
273 | * Strip slashes |
||
274 | * |
||
275 | * @return self |
||
276 | */ |
||
277 | 2 | public function stripSlashes(): self |
|
281 | |||
282 | /** |
||
283 | * Strip C-like slashes |
||
284 | * |
||
285 | * @return self |
||
286 | */ |
||
287 | 2 | public function stripCSlashes(): self |
|
291 | |||
292 | /** |
||
293 | * Return the word count |
||
294 | * |
||
295 | * @param string $charlist |
||
296 | * |
||
297 | * @return int |
||
298 | */ |
||
299 | 2 | public function wordCount(string $charlist = ''): int |
|
307 | |||
308 | /** |
||
309 | * Return the collection of words |
||
310 | * |
||
311 | * @param string $charlist |
||
312 | * |
||
313 | * @return MapInterface<int, self> |
||
314 | */ |
||
315 | 2 | public function words(string $charlist = ''): MapInterface |
|
326 | |||
327 | /** |
||
328 | * Split the string using a regular expression |
||
329 | * |
||
330 | * @param string $regex |
||
331 | * @param int $limit |
||
332 | * |
||
333 | * @return StreamInterface<self> |
||
334 | */ |
||
335 | 4 | View Code Duplication | public function pregSplit(string $regex, int $limit = -1): StreamInterface |
346 | |||
347 | /** |
||
348 | * Check if the string match the given regular expression |
||
349 | * |
||
350 | * @param string $regex |
||
351 | * @param int $offset |
||
352 | * |
||
353 | * @throws Exception If the regex failed |
||
354 | * |
||
355 | * @return bool |
||
356 | */ |
||
357 | 4 | View Code Duplication | public function matches(string $regex, int $offset = 0): bool |
368 | |||
369 | /** |
||
370 | * Return a collection of the elements matching the regex |
||
371 | * |
||
372 | * @param string $regex |
||
373 | * @param int $offset |
||
374 | * @param int $flags |
||
375 | * |
||
376 | * @throws Exception If the regex failed |
||
377 | * |
||
378 | * @return MapInterface<scalar, self> |
||
379 | */ |
||
380 | 4 | public function getMatches( |
|
405 | |||
406 | /** |
||
407 | * Replace part of the string by using a regular expression |
||
408 | * |
||
409 | * @param string $regex |
||
410 | * @param string $replacement |
||
411 | * @param int $limit |
||
412 | * |
||
413 | * @throws Exception If the regex failed |
||
414 | * |
||
415 | * @return self |
||
416 | */ |
||
417 | 2 | View Code Duplication | public function pregReplace( |
435 | |||
436 | /** |
||
437 | * Return part of the string |
||
438 | * |
||
439 | * @param int $start |
||
440 | * @param int $length |
||
441 | * |
||
442 | * @return self |
||
443 | */ |
||
444 | 2 | View Code Duplication | public function substring(int $start, int $length = null): self |
454 | |||
455 | /** |
||
456 | * Return a formatted string |
||
457 | * |
||
458 | * @return self |
||
459 | */ |
||
460 | 2 | public function sprintf(...$values): self |
|
467 | |||
468 | /** |
||
469 | * Return the string with the first letter as uppercase |
||
470 | * |
||
471 | * @return self |
||
472 | */ |
||
473 | 4 | public function ucfirst(): self |
|
477 | |||
478 | /** |
||
479 | * Return the string with the first letter as lowercase |
||
480 | * |
||
481 | * @return self |
||
482 | */ |
||
483 | 2 | public function lcfirst(): self |
|
487 | |||
488 | /** |
||
489 | * Return a CamelCase representation of the string |
||
490 | * |
||
491 | * @return self |
||
492 | */ |
||
493 | 2 | public function camelize(): self |
|
504 | |||
505 | /** |
||
506 | * Append a string at the end of the current one |
||
507 | * |
||
508 | * @param string $string |
||
509 | * |
||
510 | * @return self |
||
511 | */ |
||
512 | 2 | public function append(string $string): self |
|
516 | |||
517 | /** |
||
518 | * Prepend a string at the beginning of the current one |
||
519 | * |
||
520 | * @param string $string |
||
521 | * |
||
522 | * @return self |
||
523 | */ |
||
524 | 2 | public function prepend(string $string): self |
|
528 | |||
529 | /** |
||
530 | * Check if the 2 strings are equal |
||
531 | * |
||
532 | * @param self $string |
||
533 | * |
||
534 | * @return bool |
||
535 | */ |
||
536 | 16 | public function equals(self $string): bool |
|
540 | |||
541 | /** |
||
542 | * Trim the string |
||
543 | * |
||
544 | * @param string $mask |
||
545 | * |
||
546 | * @return self |
||
547 | */ |
||
548 | 2 | public function trim(string $mask = null): self |
|
554 | |||
555 | /** |
||
556 | * Trim the right side of the string |
||
557 | * |
||
558 | * @param string $mask |
||
559 | * |
||
560 | * @return self |
||
561 | */ |
||
562 | 2 | public function rightTrim(string $mask = null): self |
|
568 | |||
569 | /** |
||
570 | * Trim the left side of the string |
||
571 | * |
||
572 | * @param string $mask |
||
573 | * |
||
574 | * @return self |
||
575 | */ |
||
576 | 2 | public function leftTrim(string $mask = null): self |
|
582 | |||
583 | /** |
||
584 | * Check if the given string is present in the current one |
||
585 | * |
||
586 | * @param string $value |
||
587 | * |
||
588 | * @return bool |
||
589 | */ |
||
590 | 2 | public function contains(string $value): bool |
|
600 | |||
601 | /** |
||
602 | * Quote regular expression characters |
||
603 | * |
||
604 | * @param string $delimitier |
||
605 | * |
||
606 | * @return self |
||
607 | */ |
||
608 | 2 | public function pregQuote(string $delimiter = ''): self |
|
612 | |||
613 | /** |
||
614 | * Pad the string |
||
615 | * |
||
616 | * @param int $length |
||
617 | * @param string $character |
||
618 | * @param int $direction |
||
619 | * |
||
620 | * @return self |
||
621 | */ |
||
622 | 2 | private function pad( |
|
634 | } |
||
635 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.