Complex classes like AbstractString 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 AbstractString, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | abstract class AbstractString implements \Stringable, \JsonSerializable |
||
| 31 | { |
||
| 32 | public const PREG_PATTERN_ORDER = PREG_PATTERN_ORDER; |
||
| 33 | public const PREG_SET_ORDER = PREG_SET_ORDER; |
||
| 34 | public const PREG_OFFSET_CAPTURE = PREG_OFFSET_CAPTURE; |
||
| 35 | public const PREG_UNMATCHED_AS_NULL = PREG_UNMATCHED_AS_NULL; |
||
| 36 | |||
| 37 | public const PREG_SPLIT = 0; |
||
| 38 | public const PREG_SPLIT_NO_EMPTY = PREG_SPLIT_NO_EMPTY; |
||
| 39 | public const PREG_SPLIT_DELIM_CAPTURE = PREG_SPLIT_DELIM_CAPTURE; |
||
| 40 | public const PREG_SPLIT_OFFSET_CAPTURE = PREG_SPLIT_OFFSET_CAPTURE; |
||
| 41 | |||
| 42 | protected $string = ''; |
||
| 43 | protected $ignoreCase = false; |
||
| 44 | |||
| 45 | abstract public function __construct(string $string = ''); |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Unwraps instances of AbstractString back to strings. |
||
| 49 | * |
||
| 50 | * @return string[]|array |
||
| 51 | */ |
||
| 52 | public static function unwrap(array $values): array |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Wraps (and normalizes) strings in instances of AbstractString. |
||
| 67 | * |
||
| 68 | * @return static[]|array |
||
| 69 | */ |
||
| 70 | public static function wrap(array $values): array |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @param string|string[] $needle |
||
| 95 | * |
||
| 96 | * @return static |
||
| 97 | */ |
||
| 98 | public function after($needle, bool $includeNeedle = false, int $offset = 0): self |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @param string|string[] $needle |
||
| 127 | * |
||
| 128 | * @return static |
||
| 129 | */ |
||
| 130 | public function afterLast($needle, bool $includeNeedle = false, int $offset = 0): self |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @return static |
||
| 159 | */ |
||
| 160 | abstract public function append(string ...$suffix): self; |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @param string|string[] $needle |
||
| 164 | * |
||
| 165 | * @return static |
||
| 166 | */ |
||
| 167 | public function before($needle, bool $includeNeedle = false, int $offset = 0): self |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @param string|string[] $needle |
||
| 196 | * |
||
| 197 | * @return static |
||
| 198 | */ |
||
| 199 | public function beforeLast($needle, bool $includeNeedle = false, int $offset = 0): self |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @return int[] |
||
| 228 | */ |
||
| 229 | public function bytesAt(int $offset): array |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @return static |
||
| 238 | */ |
||
| 239 | abstract public function camel(): self; |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @return static[] |
||
| 243 | */ |
||
| 244 | abstract public function chunk(int $length = 1): array; |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @return static |
||
| 248 | */ |
||
| 249 | public function collapseWhitespace(): self |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @param string|string[] $needle |
||
| 259 | */ |
||
| 260 | public function containsAny($needle): bool |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @param string|string[] $suffix |
||
| 267 | */ |
||
| 268 | public function endsWith($suffix): bool |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @return static |
||
| 285 | */ |
||
| 286 | public function ensureEnd(string $suffix): self |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @return static |
||
| 300 | */ |
||
| 301 | public function ensureStart(string $prefix): self |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @param string|string[] $string |
||
| 322 | */ |
||
| 323 | public function equalsTo($string): bool |
||
| 337 | |||
| 338 | /** |
||
| 339 | * @return static |
||
| 340 | */ |
||
| 341 | abstract public function folded(): self; |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @return static |
||
| 345 | */ |
||
| 346 | public function ignoreCase(): self |
||
| 353 | |||
| 354 | /** |
||
| 355 | * @param string|string[] $needle |
||
| 356 | */ |
||
| 357 | public function indexOf($needle, int $offset = 0): ?int |
||
| 375 | |||
| 376 | /** |
||
| 377 | * @param string|string[] $needle |
||
| 378 | */ |
||
| 379 | public function indexOfLast($needle, int $offset = 0): ?int |
||
| 397 | |||
| 398 | public function isEmpty(): bool |
||
| 402 | |||
| 403 | /** |
||
| 404 | * @return static |
||
| 405 | */ |
||
| 406 | abstract public function join(array $strings, string $lastGlue = null): self; |
||
| 407 | |||
| 408 | public function jsonSerialize(): string |
||
| 412 | |||
| 413 | abstract public function length(): int; |
||
| 414 | |||
| 415 | /** |
||
| 416 | * @return static |
||
| 417 | */ |
||
| 418 | abstract public function lower(): self; |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Matches the string using a regular expression. |
||
| 422 | * |
||
| 423 | * Pass PREG_PATTERN_ORDER or PREG_SET_ORDER as $flags to get all occurrences matching the regular expression. |
||
| 424 | * |
||
| 425 | * @return array All matches in a multi-dimensional array ordered according to flags |
||
| 426 | */ |
||
| 427 | abstract public function match(string $regexp, int $flags = 0, int $offset = 0): array; |
||
| 428 | |||
| 429 | /** |
||
| 430 | * @return static |
||
| 431 | */ |
||
| 432 | abstract public function padBoth(int $length, string $padStr = ' '): self; |
||
| 433 | |||
| 434 | /** |
||
| 435 | * @return static |
||
| 436 | */ |
||
| 437 | abstract public function padEnd(int $length, string $padStr = ' '): self; |
||
| 438 | |||
| 439 | /** |
||
| 440 | * @return static |
||
| 441 | */ |
||
| 442 | abstract public function padStart(int $length, string $padStr = ' '): self; |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @return static |
||
| 446 | */ |
||
| 447 | abstract public function prepend(string ...$prefix): self; |
||
| 448 | |||
| 449 | /** |
||
| 450 | * @return static |
||
| 451 | */ |
||
| 452 | public function repeat(int $multiplier): self |
||
| 463 | |||
| 464 | /** |
||
| 465 | * @return static |
||
| 466 | */ |
||
| 467 | abstract public function replace(string $from, string $to): self; |
||
| 468 | |||
| 469 | /** |
||
| 470 | * @param string|callable $to |
||
| 471 | * |
||
| 472 | * @return static |
||
| 473 | */ |
||
| 474 | abstract public function replaceMatches(string $fromRegexp, $to): self; |
||
| 475 | |||
| 476 | /** |
||
| 477 | * @return static |
||
| 478 | */ |
||
| 479 | abstract public function reverse(): self; |
||
| 480 | |||
| 481 | /** |
||
| 482 | * @return static |
||
| 483 | */ |
||
| 484 | abstract public function slice(int $start = 0, int $length = null): self; |
||
| 485 | |||
| 486 | /** |
||
| 487 | * @return static |
||
| 488 | */ |
||
| 489 | abstract public function snake(): self; |
||
| 490 | |||
| 491 | /** |
||
| 492 | * @return static |
||
| 493 | */ |
||
| 494 | abstract public function splice(string $replacement, int $start = 0, int $length = null): self; |
||
| 495 | |||
| 496 | /** |
||
| 497 | * @return static[] |
||
| 498 | */ |
||
| 499 | public function split(string $delimiter, int $limit = null, int $flags = null): array |
||
| 543 | |||
| 544 | /** |
||
| 545 | * @param string|string[] $prefix |
||
| 546 | */ |
||
| 547 | public function startsWith($prefix): bool |
||
| 561 | |||
| 562 | /** |
||
| 563 | * @return static |
||
| 564 | */ |
||
| 565 | abstract public function title(bool $allWords = false): self; |
||
| 566 | |||
| 567 | public function toByteString(string $toEncoding = null): ByteString |
||
| 597 | |||
| 598 | public function toCodePointString(): CodePointString |
||
| 602 | |||
| 603 | public function toString(): string |
||
| 607 | |||
| 608 | public function toUnicodeString(): UnicodeString |
||
| 612 | |||
| 613 | /** |
||
| 614 | * @return static |
||
| 615 | */ |
||
| 616 | abstract public function trim(string $chars = " \t\n\r\0\x0B\x0C\u{A0}\u{FEFF}"): self; |
||
| 617 | |||
| 618 | /** |
||
| 619 | * @return static |
||
| 620 | */ |
||
| 621 | abstract public function trimEnd(string $chars = " \t\n\r\0\x0B\x0C\u{A0}\u{FEFF}"): self; |
||
| 622 | |||
| 623 | /** |
||
| 624 | * @return static |
||
| 625 | */ |
||
| 626 | abstract public function trimStart(string $chars = " \t\n\r\0\x0B\x0C\u{A0}\u{FEFF}"): self; |
||
| 627 | |||
| 628 | /** |
||
| 629 | * @return static |
||
| 630 | */ |
||
| 631 | public function truncate(int $length, string $ellipsis = '', bool $cut = true): self |
||
| 657 | |||
| 658 | /** |
||
| 659 | * @return static |
||
| 660 | */ |
||
| 661 | abstract public function upper(): self; |
||
| 662 | |||
| 663 | /** |
||
| 664 | * Returns the printable length on a terminal. |
||
| 665 | */ |
||
| 666 | abstract public function width(bool $ignoreAnsiDecoration = true): int; |
||
| 667 | |||
| 668 | /** |
||
| 669 | * @return static |
||
| 670 | */ |
||
| 671 | public function wordwrap(int $width = 75, string $break = "\n", bool $cut = false): self |
||
| 716 | |||
| 717 | public function __sleep(): array |
||
| 721 | |||
| 722 | public function __clone() |
||
| 726 | |||
| 727 | public function __toString(): string |
||
| 731 | } |
||
| 732 |
This check looks for access to properties that are not accessible from the current context.
If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.