| Total Complexity | 43 |
| Total Lines | 343 |
| 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 |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Get a string according to an integer value. |
||
| 88 | * |
||
| 89 | * @param float $number |
||
| 90 | * @param array $choice |
||
| 91 | * @param string|null $extra |
||
| 92 | * |
||
| 93 | * @return string |
||
| 94 | */ |
||
| 95 | public function choice(float $number, array $choice = [], string $extra = null): string |
||
| 96 | { |
||
| 97 | $number = (int) $number; |
||
| 98 | $mod = $number % 10; |
||
| 99 | |||
| 100 | switch (true) { |
||
| 101 | case $mod === 0: |
||
| 102 | case $mod >= 5 && $mod <= 9: |
||
| 103 | case ($number % 100 >= 11) && ($number % 100 <= 20): |
||
| 104 | $result = $choice[2] ?? ''; |
||
| 105 | break; |
||
| 106 | |||
| 107 | case $mod >= 2 && $mod <= 4: |
||
| 108 | $result = $choice[1] ?? ''; |
||
| 109 | break; |
||
| 110 | |||
| 111 | default: |
||
| 112 | $result = $choice[0] ?? ''; |
||
| 113 | } |
||
| 114 | |||
| 115 | if (empty($extra)) { |
||
| 116 | return trim($result); |
||
| 117 | } |
||
| 118 | |||
| 119 | return implode(' ', [trim($result), trim($extra)]); |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Begin a string with a single instance of a given value. |
||
| 124 | * |
||
| 125 | * @see https://github.com/illuminate/support/blob/master/Str.php |
||
| 126 | * |
||
| 127 | * @param string|null $value |
||
| 128 | * @param string $prefix |
||
| 129 | * |
||
| 130 | * @return string |
||
| 131 | */ |
||
| 132 | public function start(?string $value, string $prefix): string |
||
| 133 | { |
||
| 134 | $quoted = preg_quote($prefix, '/'); |
||
| 135 | |||
| 136 | return $prefix . preg_replace('/^(?:' . $quoted . ')+/u', '', $value); |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Cap a string with a single instance of a given value. |
||
| 141 | * |
||
| 142 | * @see https://github.com/illuminate/support/blob/master/Str.php |
||
| 143 | * |
||
| 144 | * @param string $value |
||
| 145 | * @param string $cap |
||
| 146 | * |
||
| 147 | * @return string |
||
| 148 | */ |
||
| 149 | public function finish(string $value, string $cap = '/'): string |
||
| 150 | { |
||
| 151 | $quoted = preg_quote($cap, '/'); |
||
| 152 | |||
| 153 | return preg_replace('/(?:' . $quoted . ')+$/u', '', $value) . $cap; |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Determine if a given string starts with a given substring. |
||
| 158 | * |
||
| 159 | * @see https://github.com/illuminate/support/blob/master/Str.php |
||
| 160 | * |
||
| 161 | * @param string $haystack |
||
| 162 | * @param string|string[] $needles |
||
| 163 | * |
||
| 164 | * @return bool |
||
| 165 | */ |
||
| 166 | public function startsWith(string $haystack, $needles): bool |
||
| 167 | { |
||
| 168 | foreach ((array) $needles as $needle) { |
||
| 169 | if ((string) $needle !== '' && strncmp($haystack, $needle, strlen($needle)) === 0) { |
||
| 170 | return true; |
||
| 171 | } |
||
| 172 | } |
||
| 173 | |||
| 174 | return false; |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Determine if a given string ends with a given substring. |
||
| 179 | * |
||
| 180 | * @see https://github.com/illuminate/support/blob/master/Str.php |
||
| 181 | * |
||
| 182 | * @param string $haystack |
||
| 183 | * @param string|string[] $needles |
||
| 184 | * |
||
| 185 | * @return bool |
||
| 186 | */ |
||
| 187 | public function endsWith(string $haystack, $needles): bool |
||
| 188 | { |
||
| 189 | foreach ((array) $needles as $needle) { |
||
| 190 | if ($needle !== '' && substr($haystack, -strlen($needle)) === (string) $needle) { |
||
| 191 | return true; |
||
| 192 | } |
||
| 193 | } |
||
| 194 | |||
| 195 | return false; |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Convert the given string to lower-case. |
||
| 200 | * |
||
| 201 | * @see https://github.com/illuminate/support/blob/master/Str.php |
||
| 202 | * |
||
| 203 | * @param string|null $value |
||
| 204 | * |
||
| 205 | * @return string |
||
| 206 | */ |
||
| 207 | public function lower(?string $value): string |
||
| 208 | { |
||
| 209 | return mb_strtolower($value, 'UTF-8'); |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Convert a value to studly caps case. |
||
| 214 | * |
||
| 215 | * @see https://github.com/illuminate/support/blob/master/Str.php |
||
| 216 | * |
||
| 217 | * @param string|null $value |
||
| 218 | * |
||
| 219 | * @return string|null |
||
| 220 | */ |
||
| 221 | public function studly(?string $value): ?string |
||
| 222 | { |
||
| 223 | if (empty($value)) { |
||
| 224 | return null; |
||
| 225 | } |
||
| 226 | |||
| 227 | $key = $value; |
||
| 228 | |||
| 229 | if (isset(self::$studlyCache[$key])) { |
||
| 230 | return self::$studlyCache[$key]; |
||
| 231 | } |
||
| 232 | |||
| 233 | $value = ucwords(str_replace(['-', '_'], ' ', $value)); |
||
| 234 | |||
| 235 | return self::$studlyCache[$key] = str_replace(' ', '', $value); |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Convert a value to camel case. |
||
| 240 | * |
||
| 241 | * @see https://github.com/illuminate/support/blob/master/Str.php |
||
| 242 | * |
||
| 243 | * @param string|null $value |
||
| 244 | * |
||
| 245 | * @return string|null |
||
| 246 | */ |
||
| 247 | public function camel(?string $value): ?string |
||
| 248 | { |
||
| 249 | if (empty($value)) { |
||
| 250 | return null; |
||
| 251 | } |
||
| 252 | |||
| 253 | if (isset(self::$camelCache[$value])) { |
||
| 254 | return self::$camelCache[$value]; |
||
| 255 | } |
||
| 256 | |||
| 257 | return self::$camelCache[$value] = lcfirst($this->studly($value)); |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Convert a string to snake case. |
||
| 262 | * |
||
| 263 | * @see https://github.com/illuminate/support/blob/master/Str.php |
||
| 264 | * |
||
| 265 | * @param string|null $value |
||
| 266 | * @param string $delimiter |
||
| 267 | * |
||
| 268 | * @return string|null |
||
| 269 | */ |
||
| 270 | public function snake(?string $value, string $delimiter = '_'): ?string |
||
| 271 | { |
||
| 272 | if (empty($value)) { |
||
| 273 | return null; |
||
| 274 | } |
||
| 275 | |||
| 276 | $key = $value; |
||
| 277 | |||
| 278 | if (isset(self::$snakeCache[$key][$delimiter])) { |
||
| 279 | return self::$snakeCache[$key][$delimiter]; |
||
| 280 | } |
||
| 281 | |||
| 282 | if (! ctype_lower($value)) { |
||
| 283 | $value = preg_replace('/\s+/u', '', ucwords($value)); |
||
| 284 | |||
| 285 | $value = $this->lower(preg_replace('/(.)(?=[A-Z])/u', '$1' . $delimiter, $value)); |
||
| 286 | } |
||
| 287 | |||
| 288 | return self::$snakeCache[$key][$delimiter] = $value; |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Return the length of the given string. |
||
| 293 | * |
||
| 294 | * @see https://github.com/illuminate/support/blob/master/Str.php |
||
| 295 | * |
||
| 296 | * @param string|null $value |
||
| 297 | * @param string|null $encoding |
||
| 298 | * |
||
| 299 | * @return int |
||
| 300 | */ |
||
| 301 | public function length(?string $value, string $encoding = null): int |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Returns the portion of string specified by the start and length parameters. |
||
| 310 | * |
||
| 311 | * @see https://github.com/illuminate/support/blob/master/Str.php |
||
| 312 | * |
||
| 313 | * @param string $string |
||
| 314 | * @param int $start |
||
| 315 | * @param int|null $length |
||
| 316 | * |
||
| 317 | * @return string|null |
||
| 318 | */ |
||
| 319 | public function substr(string $string, int $start, int $length = null): ?string |
||
| 320 | { |
||
| 321 | return mb_substr($string, $start, $length, 'UTF-8'); |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Get the portion of a string before the first occurrence of a given value. |
||
| 326 | * |
||
| 327 | * @see https://github.com/illuminate/support/blob/master/Str.php |
||
| 328 | * |
||
| 329 | * @param string $subject |
||
| 330 | * @param string $search |
||
| 331 | * |
||
| 332 | * @return string |
||
| 333 | */ |
||
| 334 | public function before(string $subject, string $search): ?string |
||
| 337 | } |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Return the remainder of a string after the first occurrence of a given value. |
||
| 341 | * |
||
| 342 | * @see https://github.com/illuminate/support/blob/master/Str.php |
||
| 343 | * |
||
| 344 | * @param string $subject |
||
| 345 | * @param string $search |
||
| 346 | * |
||
| 347 | * @return string |
||
| 348 | */ |
||
| 349 | public function after(string $subject, string $search): string |
||
| 352 | } |
||
| 353 | } |
||
| 354 |
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: