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 { |
||
| 10 | /** |
||
| 11 | * The cache of snake-cased words. |
||
| 12 | * |
||
| 13 | * @var array |
||
| 14 | */ |
||
| 15 | protected static $snakeCache = []; |
||
| 16 | /** |
||
| 17 | * The cache of camel-cased words. |
||
| 18 | * |
||
| 19 | * @var array |
||
| 20 | */ |
||
| 21 | protected static $camelCache = []; |
||
| 22 | /** |
||
| 23 | * The cache of studly-cased words. |
||
| 24 | * |
||
| 25 | * @var array |
||
| 26 | */ |
||
| 27 | protected static $studlyCache = []; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * 判断一个字符串是否以给定字符串开始 |
||
| 31 | * |
||
| 32 | * @param string $haystack |
||
| 33 | * @param string|array $needles |
||
| 34 | * @return bool |
||
| 35 | */ |
||
| 36 | 3 | public static function startsWith($haystack, $needles) |
|
| 45 | |||
| 46 | /** |
||
| 47 | * 判断一个字符串是否以给定字符串结尾 |
||
| 48 | * |
||
| 49 | * @param string $haystack |
||
| 50 | * @param string|array $needles |
||
| 51 | * @return bool |
||
| 52 | */ |
||
| 53 | 1 | public static function endsWith($haystack, $needles) |
|
| 62 | |||
| 63 | /** |
||
| 64 | * Convert a value to camel case. |
||
| 65 | * |
||
| 66 | * @param string $value |
||
| 67 | * @return string |
||
| 68 | */ |
||
| 69 | public static function camel($value) { |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Convert a value to studly caps case. |
||
| 79 | * |
||
| 80 | * @param string $value |
||
| 81 | * @return string |
||
| 82 | */ |
||
| 83 | 1 | public static function studly($value) { |
|
| 94 | |||
| 95 | /** |
||
| 96 | * Cap a string with a single instance of a given value. |
||
| 97 | * |
||
| 98 | * @param string $value |
||
| 99 | * @param string $cap |
||
| 100 | * @return string |
||
| 101 | */ |
||
| 102 | public static function finish($value, $cap) { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Determine if a given string matches a given pattern. |
||
| 110 | * |
||
| 111 | * @param string $pattern |
||
| 112 | * @param string $value |
||
| 113 | * @return bool |
||
| 114 | */ |
||
| 115 | public static function is($pattern, $value) { |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Convert a string to kebab case. |
||
| 132 | * |
||
| 133 | * @param string $value |
||
| 134 | * @return string |
||
| 135 | */ |
||
| 136 | public static function kebab($value) { |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Convert a string to snake case. |
||
| 142 | * |
||
| 143 | * @param string $value |
||
| 144 | * @param string $delimiter |
||
| 145 | * @return string |
||
| 146 | */ |
||
| 147 | public static function snake($value, $delimiter = '_') { |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Convert the given string to lower-case. |
||
| 165 | * |
||
| 166 | * @param string $value |
||
| 167 | * @return string |
||
| 168 | */ |
||
| 169 | public static function lower($value) { |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Limit the number of characters in a string. |
||
| 175 | * |
||
| 176 | * @param string $value |
||
| 177 | * @param int $limit |
||
| 178 | * @param string $end |
||
| 179 | * @return string |
||
| 180 | */ |
||
| 181 | public static function limit($value, $limit = 100, $end = '...') { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Limit the number of words in a string. |
||
| 191 | * |
||
| 192 | * @param string $value |
||
| 193 | * @param int $words |
||
| 194 | * @param string $end |
||
| 195 | * @return string |
||
| 196 | */ |
||
| 197 | public static function words($value, $words = 100, $end = '...') { |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Return the length of the given string. |
||
| 209 | * |
||
| 210 | * @param string $value |
||
| 211 | * @return int |
||
| 212 | */ |
||
| 213 | public static function length($value) { |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Parse a Class@method style callback into class and method. |
||
| 219 | * |
||
| 220 | * @param string $callback |
||
| 221 | * @param string|null $default |
||
| 222 | * @return array |
||
| 223 | */ |
||
| 224 | public static function parseCallback($callback, $default = null) { |
||
| 227 | |||
| 228 | /** |
||
| 229 | * 判断一个字符串是否包含于另一个字符中 |
||
| 230 | * |
||
| 231 | * @param string $haystack |
||
| 232 | * @param string|array $needles |
||
| 233 | * @return bool |
||
| 234 | */ |
||
| 235 | 1 | public static function contains($haystack, $needles) { |
|
| 243 | |||
| 244 | /** |
||
| 245 | * Get the plural form of an English word. |
||
| 246 | * |
||
| 247 | * @param string $value |
||
| 248 | * @param int $count |
||
| 249 | * @return string |
||
| 250 | */ |
||
| 251 | public static function plural($value, $count = 2) { |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Generate a "random" alpha-numeric string. |
||
| 257 | * |
||
| 258 | * Should not be considered sufficient for cryptography, etc. |
||
| 259 | * |
||
| 260 | * @deprecated since version 5.3. Use the "random" method directly. |
||
| 261 | * |
||
| 262 | * @param int $length |
||
| 263 | * @return string |
||
| 264 | */ |
||
| 265 | public static function quickRandom($length = 16) { |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Generate a more truly "random" alpha-numeric string. |
||
| 277 | * |
||
| 278 | * @param int $length |
||
| 279 | * @return string |
||
| 280 | */ |
||
| 281 | public static function random($length = 16) { |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Replace a given value in the string sequentially with an array. |
||
| 297 | * |
||
| 298 | * @param string $search |
||
| 299 | * @param array $replace |
||
| 300 | * @param string $subject |
||
| 301 | * @return string |
||
| 302 | */ |
||
| 303 | public static function replaceArray($search, array $replace, $subject) { |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Replace the first occurrence of a given value in the string. |
||
| 313 | * |
||
| 314 | * @param string $search |
||
| 315 | * @param string $replace |
||
| 316 | * @param string $subject |
||
| 317 | * @return string |
||
| 318 | */ |
||
| 319 | public static function replaceFirst($search, $replace, $subject) { |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Replace the last occurrence of a given value in the string. |
||
| 331 | * |
||
| 332 | * @param string $search |
||
| 333 | * @param string $replace |
||
| 334 | * @param string $subject |
||
| 335 | * @return string |
||
| 336 | */ |
||
| 337 | public static function replaceLast($search, $replace, $subject) { |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Convert the given string to title case. |
||
| 349 | * |
||
| 350 | * @param string $value |
||
| 351 | * @return string |
||
| 352 | */ |
||
| 353 | public static function title($value) { |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Get the singular form of an English word. |
||
| 359 | * |
||
| 360 | * @param string $value |
||
| 361 | * @return string |
||
| 362 | */ |
||
| 363 | public static function singular($value) { |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Generate a URL friendly "slug" from a given string. |
||
| 369 | * |
||
| 370 | * @param string $title |
||
| 371 | * @param string $separator |
||
| 372 | * @return string |
||
| 373 | */ |
||
| 374 | public static function slug($title, $separator = '-') { |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Transliterate a UTF-8 value to ASCII. |
||
| 393 | * |
||
| 394 | * @param string $value |
||
| 395 | * @return string |
||
| 396 | */ |
||
| 397 | public static function ascii($value) { |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Returns the replacements for the ascii method. |
||
| 407 | * |
||
| 408 | * Note: Adapted from Stringy\Stringy. |
||
| 409 | * |
||
| 410 | * @see https://github.com/danielstjules/Stringy/blob/2.3.1/LICENSE.txt |
||
| 411 | * |
||
| 412 | * @return array |
||
| 413 | */ |
||
| 414 | protected static function charsArray() { |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Make a string's first character uppercase. |
||
| 540 | * |
||
| 541 | * @param string $string |
||
| 542 | * @return string |
||
| 543 | */ |
||
| 544 | public static function ucfirst($string) { |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Convert the given string to upper-case. |
||
| 550 | * |
||
| 551 | * @param string $value |
||
| 552 | * @return string |
||
| 553 | */ |
||
| 554 | public static function upper($value) { |
||
| 557 | |||
| 558 | /** |
||
| 559 | * Returns the portion of string specified by the start and length parameters. |
||
| 560 | * |
||
| 561 | * @param string $string |
||
| 562 | * @param int $start |
||
| 563 | * @param int|null $length |
||
| 564 | * @return string |
||
| 565 | */ |
||
| 566 | public static function substr($string, $start, $length = null) { |
||
| 569 | } |