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 | 483 | public function __construct(string $value, string $encoding = null) |
|
| 28 | |||
| 29 | 6 | public static function of(string $value, string $encoding = null): self |
|
| 30 | { |
||
| 31 | 6 | return new self($value, $encoding); |
|
| 32 | } |
||
| 33 | |||
| 34 | /** |
||
| 35 | * {@inheritdoc} |
||
| 36 | */ |
||
| 37 | 3 | public function toPrimitive() |
|
| 38 | { |
||
| 39 | 3 | return $this->value; |
|
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * {@inheritdoc} |
||
| 44 | */ |
||
| 45 | 318 | public function __toString(): string |
|
| 46 | { |
||
| 47 | 318 | return $this->value; |
|
| 48 | } |
||
| 49 | |||
| 50 | 9 | public function encoding(): self |
|
| 51 | { |
||
| 52 | 9 | return new self($this->encoding); |
|
| 53 | } |
||
| 54 | |||
| 55 | 45 | public function toEncoding(string $encoding): self |
|
| 56 | { |
||
| 57 | 45 | return new self($this->value, $encoding); |
|
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Split the string into a collection of ones |
||
| 62 | * |
||
| 63 | * @param string $delimiter |
||
| 64 | * |
||
| 65 | * @return StreamInterface<self> |
||
|
|
|||
| 66 | */ |
||
| 67 | 21 | public function split(string $delimiter = null): StreamInterface |
|
| 68 | { |
||
| 69 | 21 | if (is_null($delimiter) || $delimiter === '') { |
|
| 70 | 9 | return $this->chunk(); |
|
| 71 | } |
||
| 72 | |||
| 73 | 15 | $parts = explode($delimiter, $this->value); |
|
| 74 | 15 | $stream = new Stream(self::class); |
|
| 75 | |||
| 76 | 15 | foreach ($parts as $part) { |
|
| 77 | 15 | $stream = $stream->add(new self($part, $this->encoding)); |
|
| 78 | } |
||
| 79 | |||
| 80 | 15 | return $stream; |
|
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Returns a collection of the string splitted by the given chunk size |
||
| 85 | * |
||
| 86 | * @param int $size |
||
| 87 | * |
||
| 88 | * @return StreamInterface<self> |
||
| 89 | */ |
||
| 90 | 21 | public function chunk(int $size = 1): StreamInterface |
|
| 91 | { |
||
| 92 | 21 | $stream = new Stream(self::class); |
|
| 93 | 21 | $string = $this; |
|
| 94 | |||
| 95 | 21 | while ($string->length() > 0) { |
|
| 96 | 21 | $stream = $stream->add($string->substring(0, $size)); |
|
| 97 | 21 | $string = $string->substring($size); |
|
| 98 | } |
||
| 99 | |||
| 100 | 21 | return $stream; |
|
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Returns the position of the first occurence of the string |
||
| 105 | * |
||
| 106 | * @param string $needle |
||
| 107 | * @param int $offset |
||
| 108 | * |
||
| 109 | * @throws SubstringException If the string is not found |
||
| 110 | * |
||
| 111 | * @return int |
||
| 112 | */ |
||
| 113 | 18 | public function position(string $needle, int $offset = 0): int |
|
| 114 | { |
||
| 115 | 18 | $position = mb_strpos($this->value, $needle, $offset, $this->encoding); |
|
| 116 | |||
| 117 | 18 | if ($position === false) { |
|
| 118 | 9 | throw new SubstringException(sprintf( |
|
| 119 | 9 | 'Substring "%s" not found', |
|
| 120 | 9 | $needle |
|
| 121 | )); |
||
| 122 | } |
||
| 123 | |||
| 124 | 15 | return (int) $position; |
|
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Replace all occurences of the search string with the replacement one |
||
| 129 | * |
||
| 130 | * @param string $search |
||
| 131 | * @param string $replacement |
||
| 132 | * |
||
| 133 | * @return self |
||
| 134 | */ |
||
| 135 | 6 | public function replace(string $search, string $replacement): self |
|
| 136 | { |
||
| 137 | 6 | if (!$this->contains($search)) { |
|
| 138 | 3 | return $this; |
|
| 139 | } |
||
| 140 | |||
| 141 | return $this |
||
| 142 | 6 | ->split($search) |
|
| 143 | 6 | ->join($replacement); |
|
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Returns the string following the given delimiter |
||
| 148 | * |
||
| 149 | * @param string $delimiter |
||
| 150 | * |
||
| 151 | * @throws SubstringException If the string is not found |
||
| 152 | * |
||
| 153 | * @return self |
||
| 154 | */ |
||
| 155 | 9 | public function str(string $delimiter): self |
|
| 156 | { |
||
| 157 | 9 | $sub = mb_strstr($this->value, $delimiter, false, $this->encoding); |
|
| 158 | |||
| 159 | 9 | if ($sub === false) { |
|
| 160 | 3 | throw new SubstringException(sprintf( |
|
| 161 | 3 | 'Substring "%s" not found', |
|
| 162 | 3 | $delimiter |
|
| 163 | )); |
||
| 164 | } |
||
| 165 | |||
| 166 | 6 | return new self($sub, $this->encoding); |
|
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Return the string in upper case |
||
| 171 | * |
||
| 172 | * @return self |
||
| 173 | */ |
||
| 174 | 9 | public function toUpper(): self |
|
| 175 | { |
||
| 176 | 9 | return new self(mb_strtoupper($this->value), $this->encoding); |
|
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Return the string in lower case |
||
| 181 | * |
||
| 182 | * @return self |
||
| 183 | */ |
||
| 184 | 6 | public function toLower(): self |
|
| 185 | { |
||
| 186 | 6 | return new self(mb_strtolower($this->value), $this->encoding); |
|
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Return the string length |
||
| 191 | * |
||
| 192 | * @return int |
||
| 193 | */ |
||
| 194 | 45 | public function length(): int |
|
| 195 | { |
||
| 196 | 45 | return mb_strlen($this->value, $this->encoding); |
|
| 197 | } |
||
| 198 | |||
| 199 | 3 | public function empty(): bool |
|
| 200 | { |
||
| 201 | 3 | return $this->value === ''; |
|
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Reverse the string |
||
| 206 | * |
||
| 207 | * @return self |
||
| 208 | */ |
||
| 209 | 3 | 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 | 3 | 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 | 3 | 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 | 3 | 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 | 3 | 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 | 3 | public function repeat(int $repeat): self |
|
| 292 | |||
| 293 | /** |
||
| 294 | * Shuffle the string |
||
| 295 | * |
||
| 296 | * @return self |
||
| 297 | */ |
||
| 298 | 6 | public function shuffle(): self |
|
| 305 | |||
| 306 | /** |
||
| 307 | * Strip slashes |
||
| 308 | * |
||
| 309 | * @return self |
||
| 310 | */ |
||
| 311 | 3 | public function stripSlashes(): self |
|
| 315 | |||
| 316 | /** |
||
| 317 | * Strip C-like slashes |
||
| 318 | * |
||
| 319 | * @return self |
||
| 320 | */ |
||
| 321 | 3 | public function stripCSlashes(): self |
|
| 325 | |||
| 326 | /** |
||
| 327 | * Return the word count |
||
| 328 | * |
||
| 329 | * @param string $charlist |
||
| 330 | * |
||
| 331 | * @return int |
||
| 332 | */ |
||
| 333 | 3 | 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 | 3 | 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 | 6 | 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 | 6 | 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 | 3 | 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 | 9 | 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 | 3 | 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 | 39 | public function substring(int $start, int $length = null): self |
|
| 509 | |||
| 510 | /** |
||
| 511 | * Return a formatted string |
||
| 512 | * |
||
| 513 | * @return self |
||
| 514 | */ |
||
| 515 | 3 | public function sprintf(...$values): self |
|
| 519 | |||
| 520 | /** |
||
| 521 | * Return the string with the first letter as uppercase |
||
| 522 | * |
||
| 523 | * @return self |
||
| 524 | */ |
||
| 525 | 6 | public function ucfirst(): self |
|
| 532 | |||
| 533 | /** |
||
| 534 | * Return the string with the first letter as lowercase |
||
| 535 | * |
||
| 536 | * @return self |
||
| 537 | */ |
||
| 538 | 3 | public function lcfirst(): self |
|
| 545 | |||
| 546 | /** |
||
| 547 | * Return a CamelCase representation of the string |
||
| 548 | * |
||
| 549 | * @return self |
||
| 550 | */ |
||
| 551 | 3 | 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 | 12 | 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 | 3 | 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 | 93 | public function equals(self $string): bool |
|
| 597 | |||
| 598 | /** |
||
| 599 | * Trim the string |
||
| 600 | * |
||
| 601 | * @param string $mask |
||
| 602 | * |
||
| 603 | * @return self |
||
| 604 | */ |
||
| 605 | 3 | 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 | 3 | 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 | 3 | 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 | 9 | public function contains(string $value): bool |
|
| 660 | |||
| 661 | /** |
||
| 662 | * Quote regular expression characters |
||
| 663 | * |
||
| 664 | * @param string $delimiter |
||
| 665 | * |
||
| 666 | * @return self |
||
| 667 | */ |
||
| 668 | 3 | 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 | 3 | private function pad( |
|
| 694 | } |
||
| 695 |
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.