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 | private $encoding; |
||
22 | |||
23 | 312 | public function __construct(string $value, string $encoding = null) |
|
28 | |||
29 | /** |
||
30 | * {@inheritdoc} |
||
31 | */ |
||
32 | 2 | public function toPrimitive() |
|
36 | |||
37 | /** |
||
38 | * {@inheritdoc} |
||
39 | */ |
||
40 | 204 | public function __toString(): string |
|
44 | |||
45 | 4 | public function encoding(): self |
|
49 | |||
50 | 30 | public function toEncoding(string $encoding): self |
|
54 | |||
55 | /** |
||
56 | * Split the string into a collection of ones |
||
57 | * |
||
58 | * @param string $delimiter |
||
59 | * |
||
60 | * @return StreamInterface<self> |
||
|
|||
61 | */ |
||
62 | 12 | public function split(string $delimiter = null): StreamInterface |
|
77 | |||
78 | /** |
||
79 | * Returns a collection of the string splitted by the given chunk size |
||
80 | * |
||
81 | * @param int $size |
||
82 | * |
||
83 | * @return StreamInterface<self> |
||
84 | */ |
||
85 | 14 | public function chunk(int $size = 1): StreamInterface |
|
97 | |||
98 | /** |
||
99 | * Returns the position of the first occurence of the string |
||
100 | * |
||
101 | * @param string $needle |
||
102 | * @param int $offset |
||
103 | * |
||
104 | * @throws SubstringException If the string is not found |
||
105 | * |
||
106 | * @return int |
||
107 | */ |
||
108 | 10 | public function position(string $needle, int $offset = 0): int |
|
121 | |||
122 | /** |
||
123 | * Replace all occurences of the search string with the replacement one |
||
124 | * |
||
125 | * @param string $search |
||
126 | * @param string $replacement |
||
127 | * |
||
128 | * @return self |
||
129 | */ |
||
130 | 4 | public function replace(string $search, string $replacement): self |
|
150 | |||
151 | /** |
||
152 | * Returns the string following the given delimiter |
||
153 | * |
||
154 | * @param string $delimiter |
||
155 | * |
||
156 | * @throws SubstringException If the string is not found |
||
157 | * |
||
158 | * @return self |
||
159 | */ |
||
160 | 6 | public function str(string $delimiter): self |
|
173 | |||
174 | /** |
||
175 | * Return the string in upper case |
||
176 | * |
||
177 | * @return self |
||
178 | */ |
||
179 | 6 | public function toUpper(): self |
|
183 | |||
184 | /** |
||
185 | * Return the string in lower case |
||
186 | * |
||
187 | * @return self |
||
188 | */ |
||
189 | 4 | public function toLower(): self |
|
193 | |||
194 | /** |
||
195 | * Return the string length |
||
196 | * |
||
197 | * @return int |
||
198 | */ |
||
199 | 28 | public function length(): int |
|
203 | |||
204 | /** |
||
205 | * Reverse the string |
||
206 | * |
||
207 | * @return self |
||
208 | */ |
||
209 | 2 | public function reverse(): self |
|
216 | |||
217 | /** |
||
218 | * Pad to the right |
||
219 | * |
||
220 | * @param int $length |
||
221 | * @param string $character |
||
222 | * |
||
223 | * @return self |
||
224 | */ |
||
225 | 2 | public function rightPad(int $length, string $character = ' '): self |
|
229 | |||
230 | /** |
||
231 | * Pad to the left |
||
232 | * |
||
233 | * @param int $length |
||
234 | * @param string $character |
||
235 | * |
||
236 | * @return self |
||
237 | */ |
||
238 | 2 | public function leftPad(int $length, string $character = ' '): self |
|
242 | |||
243 | /** |
||
244 | * Pad both sides |
||
245 | * |
||
246 | * @param int $length |
||
247 | * @param string $character |
||
248 | * |
||
249 | * @return self |
||
250 | */ |
||
251 | 2 | public function uniPad(int $length, string $character = ' '): self |
|
255 | |||
256 | /** |
||
257 | * Find length of initial segment not matching mask |
||
258 | * |
||
259 | * @param string $mask |
||
260 | * @param int $start |
||
261 | * @param int $length |
||
262 | * |
||
263 | * @return int |
||
264 | */ |
||
265 | 2 | public function cspn(string $mask, int $start = 0, int $length = null): int |
|
280 | |||
281 | /** |
||
282 | * Repeat the string n times |
||
283 | * |
||
284 | * @param int $repeat |
||
285 | * |
||
286 | * @return self |
||
287 | */ |
||
288 | 2 | public function repeat(int $repeat): self |
|
292 | |||
293 | /** |
||
294 | * Shuffle the string |
||
295 | * |
||
296 | * @return self |
||
297 | */ |
||
298 | 4 | public function shuffle(): self |
|
305 | |||
306 | /** |
||
307 | * Strip slashes |
||
308 | * |
||
309 | * @return self |
||
310 | */ |
||
311 | 2 | public function stripSlashes(): self |
|
315 | |||
316 | /** |
||
317 | * Strip C-like slashes |
||
318 | * |
||
319 | * @return self |
||
320 | */ |
||
321 | 2 | public function stripCSlashes(): self |
|
325 | |||
326 | /** |
||
327 | * Return the word count |
||
328 | * |
||
329 | * @param string $charlist |
||
330 | * |
||
331 | * @return int |
||
332 | */ |
||
333 | 2 | public function wordCount(string $charlist = ''): int |
|
341 | |||
342 | /** |
||
343 | * Return the collection of words |
||
344 | * |
||
345 | * @param string $charlist |
||
346 | * |
||
347 | * @return MapInterface<int, self> |
||
348 | */ |
||
349 | 2 | View Code Duplication | public function words(string $charlist = ''): MapInterface |
360 | |||
361 | /** |
||
362 | * Split the string using a regular expression |
||
363 | * |
||
364 | * @param string $regex |
||
365 | * @param int $limit |
||
366 | * |
||
367 | * @return StreamInterface<self> |
||
368 | */ |
||
369 | 4 | View Code Duplication | public function pregSplit(string $regex, int $limit = -1): StreamInterface |
380 | |||
381 | /** |
||
382 | * Check if the string match the given regular expression |
||
383 | * |
||
384 | * @param string $regex |
||
385 | * @param int $offset |
||
386 | * |
||
387 | * @throws Exception If the regex failed |
||
388 | * |
||
389 | * @return bool |
||
390 | */ |
||
391 | 4 | public function matches(string $regex, int $offset = 0): bool |
|
402 | |||
403 | /** |
||
404 | * Return a collection of the elements matching the regex |
||
405 | * |
||
406 | * @deprecated replaced by self::capture, to be removed in 3.0 |
||
407 | * |
||
408 | * @param string $regex |
||
409 | * @param int $offset |
||
410 | * @param int $flags |
||
411 | * |
||
412 | * @throws Exception If the regex failed |
||
413 | * |
||
414 | * @return MapInterface<scalar, self> |
||
415 | */ |
||
416 | 2 | public function getMatches( |
|
423 | |||
424 | /** |
||
425 | * Return a collection of the elements matching the regex |
||
426 | * |
||
427 | * @param string $regex |
||
428 | * @param int $offset |
||
429 | * @param int $flags |
||
430 | * |
||
431 | * @throws Exception If the regex failed |
||
432 | * |
||
433 | * @return MapInterface<scalar, self> |
||
434 | */ |
||
435 | 6 | public function capture( |
|
460 | |||
461 | /** |
||
462 | * Replace part of the string by using a regular expression |
||
463 | * |
||
464 | * @param string $regex |
||
465 | * @param string $replacement |
||
466 | * @param int $limit |
||
467 | * |
||
468 | * @throws Exception If the regex failed |
||
469 | * |
||
470 | * @return self |
||
471 | */ |
||
472 | 2 | public function pregReplace( |
|
490 | |||
491 | /** |
||
492 | * Return part of the string |
||
493 | * |
||
494 | * @param int $start |
||
495 | * @param int $length |
||
496 | * |
||
497 | * @return self |
||
498 | */ |
||
499 | 24 | public function substring(int $start, int $length = null): self |
|
509 | |||
510 | /** |
||
511 | * Return a formatted string |
||
512 | * |
||
513 | * @return self |
||
514 | */ |
||
515 | 2 | public function sprintf(...$values): self |
|
519 | |||
520 | /** |
||
521 | * Return the string with the first letter as uppercase |
||
522 | * |
||
523 | * @return self |
||
524 | */ |
||
525 | 4 | public function ucfirst(): self |
|
532 | |||
533 | /** |
||
534 | * Return the string with the first letter as lowercase |
||
535 | * |
||
536 | * @return self |
||
537 | */ |
||
538 | 2 | public function lcfirst(): self |
|
545 | |||
546 | /** |
||
547 | * Return a CamelCase representation of the string |
||
548 | * |
||
549 | * @return self |
||
550 | */ |
||
551 | 2 | public function camelize(): self |
|
561 | |||
562 | /** |
||
563 | * Append a string at the end of the current one |
||
564 | * |
||
565 | * @param string $string |
||
566 | * |
||
567 | * @return self |
||
568 | */ |
||
569 | 8 | public function append(string $string): self |
|
573 | |||
574 | /** |
||
575 | * Prepend a string at the beginning of the current one |
||
576 | * |
||
577 | * @param string $string |
||
578 | * |
||
579 | * @return self |
||
580 | */ |
||
581 | 2 | public function prepend(string $string): self |
|
585 | |||
586 | /** |
||
587 | * Check if the 2 strings are equal |
||
588 | * |
||
589 | * @param self $string |
||
590 | * |
||
591 | * @return bool |
||
592 | */ |
||
593 | 56 | public function equals(self $string): bool |
|
597 | |||
598 | /** |
||
599 | * Trim the string |
||
600 | * |
||
601 | * @param string $mask |
||
602 | * |
||
603 | * @return self |
||
604 | */ |
||
605 | 2 | View Code Duplication | public function trim(string $mask = null): self |
612 | |||
613 | /** |
||
614 | * Trim the right side of the string |
||
615 | * |
||
616 | * @param string $mask |
||
617 | * |
||
618 | * @return self |
||
619 | */ |
||
620 | 2 | View Code Duplication | public function rightTrim(string $mask = null): self |
627 | |||
628 | /** |
||
629 | * Trim the left side of the string |
||
630 | * |
||
631 | * @param string $mask |
||
632 | * |
||
633 | * @return self |
||
634 | */ |
||
635 | 2 | View Code Duplication | public function leftTrim(string $mask = null): self |
642 | |||
643 | /** |
||
644 | * Check if the given string is present in the current one |
||
645 | * |
||
646 | * @param string $value |
||
647 | * |
||
648 | * @return bool |
||
649 | */ |
||
650 | 4 | public function contains(string $value): bool |
|
660 | |||
661 | /** |
||
662 | * Quote regular expression characters |
||
663 | * |
||
664 | * @param string $delimiter |
||
665 | * |
||
666 | * @return self |
||
667 | */ |
||
668 | 2 | public function pregQuote(string $delimiter = ''): self |
|
672 | |||
673 | /** |
||
674 | * Pad the string |
||
675 | * |
||
676 | * @param int $length |
||
677 | * @param string $character |
||
678 | * @param int $direction |
||
679 | * |
||
680 | * @return self |
||
681 | */ |
||
682 | 2 | private function pad( |
|
694 | |||
695 | 4 | private function usesDefaultEncoding(): bool |
|
699 | } |
||
700 |
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.