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 | 14 | 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 | 12 | 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 |
|
| 140 | |||
| 141 | /** |
||
| 142 | * Returns the string following the given delimiter |
||
| 143 | * |
||
| 144 | * @param string $delimiter |
||
| 145 | * |
||
| 146 | * @throws SubstringException If the string is not found |
||
| 147 | * |
||
| 148 | * @return self |
||
| 149 | */ |
||
| 150 | 6 | public function str(string $delimiter): self |
|
| 163 | |||
| 164 | /** |
||
| 165 | * Return the string in upper case |
||
| 166 | * |
||
| 167 | * @return self |
||
| 168 | */ |
||
| 169 | 6 | public function toUpper(): self |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Return the string in lower case |
||
| 176 | * |
||
| 177 | * @return self |
||
| 178 | */ |
||
| 179 | 4 | public function toLower(): self |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Return the string length |
||
| 186 | * |
||
| 187 | * @return int |
||
| 188 | */ |
||
| 189 | 28 | public function length(): int |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Reverse the string |
||
| 196 | * |
||
| 197 | * @return self |
||
| 198 | */ |
||
| 199 | 2 | public function reverse(): self |
|
| 206 | |||
| 207 | /** |
||
| 208 | * Pad to the right |
||
| 209 | * |
||
| 210 | * @param int $length |
||
| 211 | * @param string $character |
||
| 212 | * |
||
| 213 | * @return self |
||
| 214 | */ |
||
| 215 | 2 | public function rightPad(int $length, string $character = ' '): self |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Pad to the left |
||
| 222 | * |
||
| 223 | * @param int $length |
||
| 224 | * @param string $character |
||
| 225 | * |
||
| 226 | * @return self |
||
| 227 | */ |
||
| 228 | 2 | public function leftPad(int $length, string $character = ' '): self |
|
| 232 | |||
| 233 | /** |
||
| 234 | * Pad both sides |
||
| 235 | * |
||
| 236 | * @param int $length |
||
| 237 | * @param string $character |
||
| 238 | * |
||
| 239 | * @return self |
||
| 240 | */ |
||
| 241 | 2 | public function uniPad(int $length, string $character = ' '): self |
|
| 245 | |||
| 246 | /** |
||
| 247 | * Find length of initial segment not matching mask |
||
| 248 | * |
||
| 249 | * @param string $mask |
||
| 250 | * @param int $start |
||
| 251 | * @param int $length |
||
| 252 | * |
||
| 253 | * @return int |
||
| 254 | */ |
||
| 255 | 2 | public function cspn(string $mask, int $start = 0, int $length = null): int |
|
| 270 | |||
| 271 | /** |
||
| 272 | * Repeat the string n times |
||
| 273 | * |
||
| 274 | * @param int $repeat |
||
| 275 | * |
||
| 276 | * @return self |
||
| 277 | */ |
||
| 278 | 2 | public function repeat(int $repeat): self |
|
| 282 | |||
| 283 | /** |
||
| 284 | * Shuffle the string |
||
| 285 | * |
||
| 286 | * @return self |
||
| 287 | */ |
||
| 288 | 4 | public function shuffle(): self |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Strip slashes |
||
| 298 | * |
||
| 299 | * @return self |
||
| 300 | */ |
||
| 301 | 2 | public function stripSlashes(): self |
|
| 305 | |||
| 306 | /** |
||
| 307 | * Strip C-like slashes |
||
| 308 | * |
||
| 309 | * @return self |
||
| 310 | */ |
||
| 311 | 2 | public function stripCSlashes(): self |
|
| 315 | |||
| 316 | /** |
||
| 317 | * Return the word count |
||
| 318 | * |
||
| 319 | * @param string $charlist |
||
| 320 | * |
||
| 321 | * @return int |
||
| 322 | */ |
||
| 323 | 2 | public function wordCount(string $charlist = ''): int |
|
| 331 | |||
| 332 | /** |
||
| 333 | * Return the collection of words |
||
| 334 | * |
||
| 335 | * @param string $charlist |
||
| 336 | * |
||
| 337 | * @return MapInterface<int, self> |
||
| 338 | */ |
||
| 339 | 2 | View Code Duplication | public function words(string $charlist = ''): MapInterface |
| 350 | |||
| 351 | /** |
||
| 352 | * Split the string using a regular expression |
||
| 353 | * |
||
| 354 | * @param string $regex |
||
| 355 | * @param int $limit |
||
| 356 | * |
||
| 357 | * @return StreamInterface<self> |
||
| 358 | */ |
||
| 359 | 4 | View Code Duplication | public function pregSplit(string $regex, int $limit = -1): StreamInterface |
| 370 | |||
| 371 | /** |
||
| 372 | * Check if the string match the given regular expression |
||
| 373 | * |
||
| 374 | * @param string $regex |
||
| 375 | * @param int $offset |
||
| 376 | * |
||
| 377 | * @throws Exception If the regex failed |
||
| 378 | * |
||
| 379 | * @return bool |
||
| 380 | */ |
||
| 381 | 4 | public function matches(string $regex, int $offset = 0): bool |
|
| 392 | |||
| 393 | /** |
||
| 394 | * Return a collection of the elements matching the regex |
||
| 395 | * |
||
| 396 | * @deprecated replaced by self::capture, to be removed in 3.0 |
||
| 397 | * |
||
| 398 | * @param string $regex |
||
| 399 | * @param int $offset |
||
| 400 | * @param int $flags |
||
| 401 | * |
||
| 402 | * @throws Exception If the regex failed |
||
| 403 | * |
||
| 404 | * @return MapInterface<scalar, self> |
||
| 405 | */ |
||
| 406 | 2 | public function getMatches( |
|
| 413 | |||
| 414 | /** |
||
| 415 | * Return a collection of the elements matching the regex |
||
| 416 | * |
||
| 417 | * @param string $regex |
||
| 418 | * @param int $offset |
||
| 419 | * @param int $flags |
||
| 420 | * |
||
| 421 | * @throws Exception If the regex failed |
||
| 422 | * |
||
| 423 | * @return MapInterface<scalar, self> |
||
| 424 | */ |
||
| 425 | 6 | public function capture( |
|
| 450 | |||
| 451 | /** |
||
| 452 | * Replace part of the string by using a regular expression |
||
| 453 | * |
||
| 454 | * @param string $regex |
||
| 455 | * @param string $replacement |
||
| 456 | * @param int $limit |
||
| 457 | * |
||
| 458 | * @throws Exception If the regex failed |
||
| 459 | * |
||
| 460 | * @return self |
||
| 461 | */ |
||
| 462 | 2 | public function pregReplace( |
|
| 480 | |||
| 481 | /** |
||
| 482 | * Return part of the string |
||
| 483 | * |
||
| 484 | * @param int $start |
||
| 485 | * @param int $length |
||
| 486 | * |
||
| 487 | * @return self |
||
| 488 | */ |
||
| 489 | 24 | public function substring(int $start, int $length = null): self |
|
| 499 | |||
| 500 | /** |
||
| 501 | * Return a formatted string |
||
| 502 | * |
||
| 503 | * @return self |
||
| 504 | */ |
||
| 505 | 2 | public function sprintf(...$values): self |
|
| 509 | |||
| 510 | /** |
||
| 511 | * Return the string with the first letter as uppercase |
||
| 512 | * |
||
| 513 | * @return self |
||
| 514 | */ |
||
| 515 | 4 | public function ucfirst(): self |
|
| 522 | |||
| 523 | /** |
||
| 524 | * Return the string with the first letter as lowercase |
||
| 525 | * |
||
| 526 | * @return self |
||
| 527 | */ |
||
| 528 | 2 | public function lcfirst(): self |
|
| 535 | |||
| 536 | /** |
||
| 537 | * Return a CamelCase representation of the string |
||
| 538 | * |
||
| 539 | * @return self |
||
| 540 | */ |
||
| 541 | 2 | public function camelize(): self |
|
| 551 | |||
| 552 | /** |
||
| 553 | * Append a string at the end of the current one |
||
| 554 | * |
||
| 555 | * @param string $string |
||
| 556 | * |
||
| 557 | * @return self |
||
| 558 | */ |
||
| 559 | 8 | public function append(string $string): self |
|
| 563 | |||
| 564 | /** |
||
| 565 | * Prepend a string at the beginning of the current one |
||
| 566 | * |
||
| 567 | * @param string $string |
||
| 568 | * |
||
| 569 | * @return self |
||
| 570 | */ |
||
| 571 | 2 | public function prepend(string $string): self |
|
| 575 | |||
| 576 | /** |
||
| 577 | * Check if the 2 strings are equal |
||
| 578 | * |
||
| 579 | * @param self $string |
||
| 580 | * |
||
| 581 | * @return bool |
||
| 582 | */ |
||
| 583 | 56 | public function equals(self $string): bool |
|
| 587 | |||
| 588 | /** |
||
| 589 | * Trim the string |
||
| 590 | * |
||
| 591 | * @param string $mask |
||
| 592 | * |
||
| 593 | * @return self |
||
| 594 | */ |
||
| 595 | 2 | View Code Duplication | public function trim(string $mask = null): self |
| 602 | |||
| 603 | /** |
||
| 604 | * Trim the right side of the string |
||
| 605 | * |
||
| 606 | * @param string $mask |
||
| 607 | * |
||
| 608 | * @return self |
||
| 609 | */ |
||
| 610 | 2 | View Code Duplication | public function rightTrim(string $mask = null): self |
| 617 | |||
| 618 | /** |
||
| 619 | * Trim the left side of the string |
||
| 620 | * |
||
| 621 | * @param string $mask |
||
| 622 | * |
||
| 623 | * @return self |
||
| 624 | */ |
||
| 625 | 2 | View Code Duplication | public function leftTrim(string $mask = null): self |
| 632 | |||
| 633 | /** |
||
| 634 | * Check if the given string is present in the current one |
||
| 635 | * |
||
| 636 | * @param string $value |
||
| 637 | * |
||
| 638 | * @return bool |
||
| 639 | */ |
||
| 640 | 6 | public function contains(string $value): bool |
|
| 650 | |||
| 651 | /** |
||
| 652 | * Quote regular expression characters |
||
| 653 | * |
||
| 654 | * @param string $delimiter |
||
| 655 | * |
||
| 656 | * @return self |
||
| 657 | */ |
||
| 658 | 2 | public function pregQuote(string $delimiter = ''): self |
|
| 662 | |||
| 663 | /** |
||
| 664 | * Pad the string |
||
| 665 | * |
||
| 666 | * @param int $length |
||
| 667 | * @param string $character |
||
| 668 | * @param int $direction |
||
| 669 | * |
||
| 670 | * @return self |
||
| 671 | */ |
||
| 672 | 2 | private function pad( |
|
| 684 | } |
||
| 685 |
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.