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 |
||
| 46 | class Str |
||
| 47 | { |
||
| 48 | // Only alpha numeric characters [a-zA-Z0-9] |
||
| 49 | public const RANDOM_ALNUM = 0; |
||
| 50 | // Only alphabetical characters [azAZ] |
||
| 51 | public const RANDOM_ALPHA = 1; |
||
| 52 | // Only alpha numeric uppercase characters exclude similar |
||
| 53 | // characters [2345679ACDEFHJKLMNPRSTUVWXYZ] |
||
| 54 | public const RANDOM_DISTINCT = 5; |
||
| 55 | // Only hexadecimal characters [0-9a-f] |
||
| 56 | public const RANDOM_HEXDEC = 2; |
||
| 57 | // Only numbers without 0 [1-9] |
||
| 58 | public const RANDOM_NOZERO = 4; |
||
| 59 | // Only numbers [0-9] |
||
| 60 | public const RANDOM_NUMERIC = 3; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Concatenates strings using the separator only once without duplication in |
||
| 64 | * places concatenation |
||
| 65 | * |
||
| 66 | * ```php |
||
| 67 | * $str = Phalcon\Helper\Str::concat( |
||
| 68 | * "/", |
||
| 69 | * "/tmp/", |
||
| 70 | * "/folder_1/", |
||
| 71 | * "/folder_2", |
||
| 72 | * "folder_3/" |
||
| 73 | * ); |
||
| 74 | * |
||
| 75 | * echo $str; // /tmp/folder_1/folder_2/folder_3/ |
||
| 76 | * ``` |
||
| 77 | * |
||
| 78 | * @param string separator |
||
| 79 | * @param string a |
||
| 80 | * @param string b |
||
| 81 | * @param string ...N |
||
| 82 | * |
||
| 83 | * @return string |
||
| 84 | * @throws Exception |
||
| 85 | */ |
||
| 86 | 2 | final public static function concat(): string |
|
| 118 | |||
| 119 | /** |
||
| 120 | * Returns number of vowels in provided string. Uses a regular expression |
||
| 121 | * to count the number of vowels (A, E, I, O, U) in a string. |
||
| 122 | * |
||
| 123 | * @param string $text |
||
| 124 | * |
||
| 125 | * @return int |
||
| 126 | */ |
||
| 127 | 1 | final public static function countVowels(string $text): int |
|
| 133 | |||
| 134 | /** |
||
| 135 | * Decapitalizes the first letter of the string and then adds it with rest |
||
| 136 | * of the string. Omit the upperRest parameter to keep the rest of the |
||
| 137 | * string intact, or set it to true to convert to uppercase. |
||
| 138 | * |
||
| 139 | * @param string $text |
||
| 140 | * @param bool $upperRest |
||
| 141 | * @param string $encoding |
||
| 142 | * |
||
| 143 | * @return string |
||
| 144 | */ |
||
| 145 | 1 | final public static function decapitalize( |
|
| 155 | |||
| 156 | /** |
||
| 157 | * Removes a number from a string or decrements that number if it is already |
||
| 158 | * defined |
||
| 159 | * |
||
| 160 | * ```php |
||
| 161 | * use Phalcon\Helper\Str; |
||
| 162 | * |
||
| 163 | * echo Str::decrement("a_1"); // "a" |
||
| 164 | * echo Str::decrement("a_2"); // "a_1" |
||
| 165 | * ``` |
||
| 166 | * |
||
| 167 | * @param string $text |
||
| 168 | * @param string $separator |
||
| 169 | * |
||
| 170 | * @return string |
||
| 171 | */ |
||
| 172 | 1 | final public static function decrement( |
|
| 189 | |||
| 190 | /** |
||
| 191 | * Accepts a file name (without extension) and returns a calculated |
||
| 192 | * directory structure with the filename in the end |
||
| 193 | * |
||
| 194 | * @param string $file |
||
| 195 | * |
||
| 196 | * @return string |
||
| 197 | */ |
||
| 198 | 28 | final public static function dirFromFile(string $file): string |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Accepts a directory name and ensures that it ends with |
||
| 212 | * DIRECTORY_SEPARATOR |
||
| 213 | * |
||
| 214 | * @param string $directory |
||
| 215 | * |
||
| 216 | * @return string |
||
| 217 | */ |
||
| 218 | 40 | final public static function dirSeparator(string $directory): string |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Check if a string ends with a given string |
||
| 225 | * |
||
| 226 | * @param string $haystack |
||
| 227 | * @param string $needle |
||
| 228 | * @param bool $ignoreCase |
||
| 229 | * |
||
| 230 | * @return bool |
||
| 231 | */ |
||
| 232 | 6 | final public static function endsWith( |
|
| 249 | |||
| 250 | /** |
||
| 251 | * Returns the first string there is between the strings from the |
||
| 252 | * parameter start and end. |
||
| 253 | * |
||
| 254 | * @param string $text |
||
| 255 | * @param string $start |
||
| 256 | * @param string $end |
||
| 257 | * |
||
| 258 | * @return string |
||
| 259 | */ |
||
| 260 | 1 | final public static function firstBetween( |
|
| 272 | |||
| 273 | /** |
||
| 274 | * Changes a text to a URL friendly one |
||
| 275 | * |
||
| 276 | * @param string $text |
||
| 277 | * @param string $separator |
||
| 278 | * @param bool $lowercase |
||
| 279 | * @param mixed|null $replace |
||
| 280 | * |
||
| 281 | * @return string |
||
| 282 | * @throws Exception |
||
| 283 | */ |
||
| 284 | 2 | public function friendly( |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Makes an underscored or dashed phrase human-readable |
||
| 323 | * |
||
| 324 | * @param string $text |
||
| 325 | * |
||
| 326 | * @return string |
||
| 327 | */ |
||
| 328 | 1 | final public static function humanize(string $text): string |
|
| 334 | |||
| 335 | /** |
||
| 336 | * Lets you determine whether or not a string includes another string. |
||
| 337 | * |
||
| 338 | * @param string $haystack |
||
| 339 | * @param string $needle |
||
| 340 | * |
||
| 341 | * @return bool |
||
| 342 | */ |
||
| 343 | 25 | final public static function includes( |
|
| 349 | |||
| 350 | /** |
||
| 351 | * Adds a number to a string or increment that number if it already is |
||
| 352 | * defined |
||
| 353 | * |
||
| 354 | * @param string $text |
||
| 355 | * @param string $separator |
||
| 356 | * |
||
| 357 | * @return string |
||
| 358 | */ |
||
| 359 | 1 | final public static function increment( |
|
| 372 | |||
| 373 | /** |
||
| 374 | * Compare two strings and returns true if both strings are anagram, |
||
| 375 | * false otherwise. |
||
| 376 | * |
||
| 377 | * @param string $first |
||
| 378 | * @param string $second |
||
| 379 | * |
||
| 380 | * @return bool |
||
| 381 | */ |
||
| 382 | 1 | final public static function isAnagram(string $first, string $second): bool |
|
| 386 | |||
| 387 | /** |
||
| 388 | * Returns true if the given string is lower case, false otherwise. |
||
| 389 | * |
||
| 390 | * @param string $text |
||
| 391 | * @param string $encoding |
||
| 392 | * |
||
| 393 | * @return bool |
||
| 394 | */ |
||
| 395 | 1 | final public static function isLower( |
|
| 401 | |||
| 402 | /** |
||
| 403 | * Returns true if the given string is a palindrome, false otherwise. |
||
| 404 | * |
||
| 405 | * @param string $text |
||
| 406 | * |
||
| 407 | * @return bool |
||
| 408 | */ |
||
| 409 | 1 | final public static function isPalindrome(string $text): bool |
|
| 413 | |||
| 414 | /** |
||
| 415 | * Returns true if the given string is upper case, false otherwise. |
||
| 416 | * |
||
| 417 | * @param string $text |
||
| 418 | * @param string $encoding |
||
| 419 | * |
||
| 420 | * @return bool |
||
| 421 | */ |
||
| 422 | 1 | final public static function isUpper( |
|
| 428 | |||
| 429 | /** |
||
| 430 | * Calculates the length of the string. Uses mbstring if present |
||
| 431 | * |
||
| 432 | * @param string $text |
||
| 433 | * @param string $encoding |
||
| 434 | * |
||
| 435 | * @return int |
||
| 436 | */ |
||
| 437 | 4 | final public static function len( |
|
| 443 | |||
| 444 | /** |
||
| 445 | * Lowercases a string, this function makes use of the mbstring extension if |
||
| 446 | * available |
||
| 447 | * |
||
| 448 | * @param string $text |
||
| 449 | * @param string $encoding |
||
| 450 | * |
||
| 451 | * @return string |
||
| 452 | */ |
||
| 453 | 8 | final public static function lower( |
|
| 459 | |||
| 460 | /** |
||
| 461 | * Generates a random string based on the given type. Type is one of the |
||
| 462 | * RANDOM_* constants |
||
| 463 | * |
||
| 464 | * @param int $type |
||
| 465 | * @param int $length |
||
| 466 | * |
||
| 467 | * @return string |
||
| 468 | */ |
||
| 469 | 6 | final public static function random( |
|
| 502 | |||
| 503 | /** |
||
| 504 | * Reduces multiple slashes in a string to single slashes |
||
| 505 | * |
||
| 506 | * @param string $text |
||
| 507 | * |
||
| 508 | * @return string |
||
| 509 | */ |
||
| 510 | 1 | final public static function reduceSlashes(string $text): string |
|
| 516 | |||
| 517 | /** |
||
| 518 | * Check if a string starts with a given string |
||
| 519 | * |
||
| 520 | * @param string $haystack |
||
| 521 | * @param string $needle |
||
| 522 | * @param bool $ignoreCase |
||
| 523 | * |
||
| 524 | * @return bool |
||
| 525 | */ |
||
| 526 | 69 | final public static function startsWith( |
|
| 543 | |||
| 544 | /** |
||
| 545 | * Makes a phrase underscored instead of spaced |
||
| 546 | * |
||
| 547 | * @param string $text |
||
| 548 | * @param string $encoding |
||
| 549 | * |
||
| 550 | * @return string |
||
| 551 | */ |
||
| 552 | 1 | final public static function ucwords( |
|
| 558 | |||
| 559 | /** |
||
| 560 | * Makes a phrase underscored instead of spaced |
||
| 561 | * |
||
| 562 | * @param string $text |
||
| 563 | * |
||
| 564 | * @return string |
||
| 565 | */ |
||
| 566 | 1 | final public static function underscore(string $text): string |
|
| 572 | |||
| 573 | /** |
||
| 574 | * Uppercases a string, this function makes use of the mbstring extension if |
||
| 575 | * available |
||
| 576 | * |
||
| 577 | * @param string $text |
||
| 578 | * @param string $encoding |
||
| 579 | * |
||
| 580 | * @return string |
||
| 581 | */ |
||
| 582 | 6 | final public static function upper( |
|
| 588 | } |
||
| 589 |