| Total Complexity | 43 |
| Total Lines | 334 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
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.
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 | final class Str |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * The cache of snake-cased words. |
||
| 13 | * |
||
| 14 | * @var array |
||
| 15 | */ |
||
| 16 | protected static $snakeCache = []; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * The cache of camel-cased words. |
||
| 20 | * |
||
| 21 | * @var array |
||
| 22 | */ |
||
| 23 | protected static $camelCache = []; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * The cache of studly-cased words. |
||
| 27 | * |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | protected static $studlyCache = []; |
||
| 31 | |||
| 32 | protected $escaping_methods = [ |
||
| 33 | DeferringDisplayableValue::class => 'resolveDisplayableValue', |
||
| 34 | Htmlable::class => 'toHtml', |
||
| 35 | ]; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Escape HTML special characters in a string. |
||
| 39 | * |
||
| 40 | * @param string|null $value |
||
| 41 | * @param bool $double |
||
| 42 | * |
||
| 43 | * @return string|null |
||
| 44 | */ |
||
| 45 | public function e(?string $value, bool $double = true): ?string |
||
| 46 | { |
||
| 47 | if (empty($value)) { |
||
| 48 | return null; |
||
| 49 | } |
||
| 50 | |||
| 51 | if ($escaped = Instance::callOf($this->escaping_methods, $value)) { |
||
| 52 | return $escaped; |
||
| 53 | } |
||
| 54 | |||
| 55 | return htmlspecialchars($value, ENT_QUOTES, 'UTF-8', $double); |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Convert special HTML entities back to characters. |
||
| 60 | * |
||
| 61 | * @param string|null $value |
||
| 62 | * |
||
| 63 | * @return string|null |
||
| 64 | */ |
||
| 65 | public function de(?string $value): ?string |
||
| 66 | { |
||
| 67 | if (empty($value)) { |
||
| 68 | return null; |
||
| 69 | } |
||
| 70 | |||
| 71 | return htmlspecialchars_decode($value, ENT_QUOTES); |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Replacing multiple spaces with a single space. |
||
| 76 | * |
||
| 77 | * @param string|null $value |
||
| 78 | * |
||
| 79 | * @return string|null |
||
| 80 | */ |
||
| 81 | public function removeSpaces(?string $value): ?string |
||
| 82 | { |
||
| 83 | return preg_replace('!\s+!', ' ', $value); |
||
| 84 | } |
||
| 85 | |||
| 86 | public function choice(float $number, array $choice = [], string $extra = null): string |
||
| 87 | { |
||
| 88 | $number = (int) $number; |
||
| 89 | $mod = $number % 10; |
||
| 90 | |||
| 91 | switch (true) { |
||
| 92 | case $mod === 0: |
||
| 93 | case $mod >= 5 && $mod <= 9: |
||
| 94 | case ($number % 100 >= 11) && ($number % 100 <= 20): |
||
| 95 | $result = $choice[2] ?? ''; |
||
| 96 | break; |
||
| 97 | |||
| 98 | case $mod >= 2 && $mod <= 4: |
||
| 99 | $result = $choice[1] ?? ''; |
||
| 100 | break; |
||
| 101 | |||
| 102 | default: |
||
| 103 | $result = $choice[0] ?? ''; |
||
| 104 | } |
||
| 105 | |||
| 106 | if (empty($extra)) { |
||
| 107 | return trim($result); |
||
| 108 | } |
||
| 109 | |||
| 110 | return implode(' ', [trim($result), trim($extra)]); |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Begin a string with a single instance of a given value. |
||
| 115 | * |
||
| 116 | * @see https://github.com/illuminate/support/blob/master/Str.php |
||
| 117 | * |
||
| 118 | * @param string|null $value |
||
| 119 | * @param string $prefix |
||
| 120 | * |
||
| 121 | * @return string |
||
| 122 | */ |
||
| 123 | public function start(?string $value, string $prefix): string |
||
| 124 | { |
||
| 125 | $quoted = preg_quote($prefix, '/'); |
||
| 126 | |||
| 127 | return $prefix . preg_replace('/^(?:' . $quoted . ')+/u', '', $value); |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Cap a string with a single instance of a given value. |
||
| 132 | * |
||
| 133 | * @see https://github.com/illuminate/support/blob/master/Str.php |
||
| 134 | * |
||
| 135 | * @param string $value |
||
| 136 | * @param string $cap |
||
| 137 | * |
||
| 138 | * @return string |
||
| 139 | */ |
||
| 140 | public function finish(string $value, string $cap = '/'): string |
||
| 141 | { |
||
| 142 | $quoted = preg_quote($cap, '/'); |
||
| 143 | |||
| 144 | return preg_replace('/(?:' . $quoted . ')+$/u', '', $value) . $cap; |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Determine if a given string starts with a given substring. |
||
| 149 | * |
||
| 150 | * @see https://github.com/illuminate/support/blob/master/Str.php |
||
| 151 | * |
||
| 152 | * @param string $haystack |
||
| 153 | * @param string|string[] $needles |
||
| 154 | * |
||
| 155 | * @return bool |
||
| 156 | */ |
||
| 157 | public function startsWith(string $haystack, $needles): bool |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Determine if a given string ends with a given substring. |
||
| 170 | * |
||
| 171 | * @see https://github.com/illuminate/support/blob/master/Str.php |
||
| 172 | * |
||
| 173 | * @param string $haystack |
||
| 174 | * @param string|string[] $needles |
||
| 175 | * |
||
| 176 | * @return bool |
||
| 177 | */ |
||
| 178 | public function endsWith(string $haystack, $needles): bool |
||
| 179 | { |
||
| 180 | foreach ((array) $needles as $needle) { |
||
| 181 | if ($needle !== '' && substr($haystack, -strlen($needle)) === (string) $needle) { |
||
| 182 | return true; |
||
| 183 | } |
||
| 184 | } |
||
| 185 | |||
| 186 | return false; |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Convert the given string to lower-case. |
||
| 191 | * |
||
| 192 | * @see https://github.com/illuminate/support/blob/master/Str.php |
||
| 193 | * |
||
| 194 | * @param string|null $value |
||
| 195 | * |
||
| 196 | * @return string |
||
| 197 | */ |
||
| 198 | public function lower(?string $value): string |
||
| 199 | { |
||
| 200 | return mb_strtolower($value, 'UTF-8'); |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Convert a value to studly caps case. |
||
| 205 | * |
||
| 206 | * @see https://github.com/illuminate/support/blob/master/Str.php |
||
| 207 | * |
||
| 208 | * @param string|null $value |
||
| 209 | * |
||
| 210 | * @return string|null |
||
| 211 | */ |
||
| 212 | public function studly(?string $value): ?string |
||
| 213 | { |
||
| 214 | if (empty($value)) { |
||
| 215 | return null; |
||
| 216 | } |
||
| 217 | |||
| 218 | $key = $value; |
||
| 219 | |||
| 220 | if (isset(self::$studlyCache[$key])) { |
||
| 221 | return self::$studlyCache[$key]; |
||
| 222 | } |
||
| 223 | |||
| 224 | $value = ucwords(str_replace(['-', '_'], ' ', $value)); |
||
| 225 | |||
| 226 | return self::$studlyCache[$key] = str_replace(' ', '', $value); |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Convert a value to camel case. |
||
| 231 | * |
||
| 232 | * @see https://github.com/illuminate/support/blob/master/Str.php |
||
| 233 | * |
||
| 234 | * @param string|null $value |
||
| 235 | * |
||
| 236 | * @return string|null |
||
| 237 | */ |
||
| 238 | public function camel(?string $value): ?string |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Convert a string to snake case. |
||
| 253 | * |
||
| 254 | * @see https://github.com/illuminate/support/blob/master/Str.php |
||
| 255 | * |
||
| 256 | * @param string|null $value |
||
| 257 | * @param string $delimiter |
||
| 258 | * |
||
| 259 | * @return string|null |
||
| 260 | */ |
||
| 261 | public function snake(?string $value, string $delimiter = '_'): ?string |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Return the length of the given string. |
||
| 284 | * |
||
| 285 | * @see https://github.com/illuminate/support/blob/master/Str.php |
||
| 286 | * |
||
| 287 | * @param string|null $value |
||
| 288 | * @param string|null $encoding |
||
| 289 | * |
||
| 290 | * @return int |
||
| 291 | */ |
||
| 292 | public function length(?string $value, string $encoding = null): int |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Returns the portion of string specified by the start and length parameters. |
||
| 301 | * |
||
| 302 | * @see https://github.com/illuminate/support/blob/master/Str.php |
||
| 303 | * |
||
| 304 | * @param string $string |
||
| 305 | * @param int $start |
||
| 306 | * @param int|null $length |
||
| 307 | * |
||
| 308 | * @return string|null |
||
| 309 | */ |
||
| 310 | public function substr(string $string, int $start, int $length = null): ?string |
||
| 311 | { |
||
| 312 | return mb_substr($string, $start, $length, 'UTF-8'); |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Get the portion of a string before the first occurrence of a given value. |
||
| 317 | * |
||
| 318 | * @see https://github.com/illuminate/support/blob/master/Str.php |
||
| 319 | * |
||
| 320 | * @param string $subject |
||
| 321 | * @param string $search |
||
| 322 | * |
||
| 323 | * @return string |
||
| 324 | */ |
||
| 325 | public function before(string $subject, string $search): ?string |
||
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Return the remainder of a string after the first occurrence of a given value. |
||
| 332 | * |
||
| 333 | * @see https://github.com/illuminate/support/blob/master/Str.php |
||
| 334 | * |
||
| 335 | * @param string $subject |
||
| 336 | * @param string $search |
||
| 337 | * |
||
| 338 | * @return string |
||
| 339 | */ |
||
| 340 | public function after(string $subject, string $search): string |
||
| 343 | } |
||
| 344 | } |
||
| 345 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: