Conditions | 31 |
Paths | 219 |
Total Lines | 137 |
Code Lines | 53 |
Lines | 0 |
Ratio | 0 % |
Tests | 32 |
CRAP Score | 54.1721 |
Changes | 17 | ||
Bugs | 9 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
11295 | 172 | public static function substr( |
|
11296 | string $str, |
||
11297 | int $offset = 0, |
||
11298 | int $length = null, |
||
11299 | string $encoding = 'UTF-8', |
||
11300 | bool $clean_utf8 = false |
||
11301 | ) { |
||
11302 | // empty string |
||
11303 | 172 | if ($str === '' || $length === 0) { |
|
11304 | 8 | return ''; |
|
11305 | } |
||
11306 | |||
11307 | 168 | if ($clean_utf8) { |
|
11308 | // iconv and mbstring are not tolerant to invalid encoding |
||
11309 | // further, their behaviour is inconsistent with that of PHP's substr |
||
11310 | 2 | $str = self::clean($str); |
|
11311 | } |
||
11312 | |||
11313 | // whole string |
||
11314 | 168 | if (!$offset && $length === null) { |
|
11315 | 7 | return $str; |
|
11316 | } |
||
11317 | |||
11318 | 163 | if ($encoding !== 'UTF-8' && $encoding !== 'CP850') { |
|
11319 | 19 | $encoding = self::normalize_encoding($encoding, 'UTF-8'); |
|
11320 | } |
||
11321 | |||
11322 | // |
||
11323 | // fallback via mbstring |
||
11324 | // |
||
11325 | |||
11326 | 163 | if (self::$SUPPORT['mbstring'] === true && $encoding === 'UTF-8') { |
|
11327 | 161 | if ($length === null) { |
|
11328 | 64 | return \mb_substr($str, $offset); |
|
11329 | } |
||
11330 | |||
11331 | 102 | return \mb_substr($str, $offset, $length); |
|
11332 | } |
||
11333 | |||
11334 | // |
||
11335 | // fallback for binary || ascii only |
||
11336 | // |
||
11337 | |||
11338 | if ( |
||
11339 | 4 | $encoding === 'CP850' |
|
11340 | || |
||
11341 | 4 | $encoding === 'ASCII' |
|
11342 | ) { |
||
11343 | if ($length === null) { |
||
11344 | return \substr($str, $offset); |
||
11345 | } |
||
11346 | |||
11347 | return \substr($str, $offset, $length); |
||
11348 | } |
||
11349 | |||
11350 | // otherwise we need the string-length |
||
11351 | 4 | $str_length = 0; |
|
11352 | 4 | if ($offset || $length === null) { |
|
11353 | 4 | $str_length = self::strlen($str, $encoding); |
|
11354 | } |
||
11355 | |||
11356 | // e.g.: invalid chars + mbstring not installed |
||
11357 | 4 | if ($str_length === false) { |
|
11358 | return false; |
||
11359 | } |
||
11360 | |||
11361 | // empty string |
||
11362 | 4 | if ($offset === $str_length && !$length) { |
|
11363 | return ''; |
||
11364 | } |
||
11365 | |||
11366 | // impossible |
||
11367 | 4 | if ($offset && $offset > $str_length) { |
|
11368 | return ''; |
||
11369 | } |
||
11370 | |||
11371 | 4 | $length = $length ?? $str_length; |
|
11372 | |||
11373 | if ( |
||
11374 | 4 | $encoding !== 'UTF-8' |
|
11375 | && |
||
11376 | 4 | self::$SUPPORT['mbstring'] === false |
|
11377 | ) { |
||
11378 | /** |
||
11379 | * @psalm-suppress ImpureFunctionCall - is is only a warning |
||
11380 | */ |
||
11381 | 2 | \trigger_error('UTF8::substr() without mbstring cannot handle "' . $encoding . '" encoding', \E_USER_WARNING); |
|
11382 | } |
||
11383 | |||
11384 | // |
||
11385 | // fallback via intl |
||
11386 | // |
||
11387 | |||
11388 | if ( |
||
11389 | 4 | $encoding === 'UTF-8' // INFO: "grapheme_substr()" can't handle other encodings |
|
11390 | && |
||
11391 | 4 | $offset >= 0 // grapheme_substr() can't handle negative offset |
|
11392 | && |
||
11393 | 4 | self::$SUPPORT['intl'] === true |
|
11394 | ) { |
||
11395 | $return_tmp = \grapheme_substr($str, $offset, $length); |
||
11396 | if ($return_tmp !== false) { |
||
11397 | return $return_tmp; |
||
11398 | } |
||
11399 | } |
||
11400 | |||
11401 | // |
||
11402 | // fallback via iconv |
||
11403 | // |
||
11404 | |||
11405 | if ( |
||
11406 | 4 | $length >= 0 // "iconv_substr()" can't handle negative length |
|
11407 | && |
||
11408 | 4 | self::$SUPPORT['iconv'] === true |
|
11409 | ) { |
||
11410 | $return_tmp = \iconv_substr($str, $offset, $length); |
||
11411 | if ($return_tmp !== false) { |
||
11412 | return $return_tmp; |
||
11413 | } |
||
11414 | } |
||
11415 | |||
11416 | // |
||
11417 | // fallback for ascii only |
||
11418 | // |
||
11419 | |||
11420 | 4 | if (ASCII::is_ascii($str)) { |
|
11421 | return \substr($str, $offset, $length); |
||
11422 | } |
||
11423 | |||
11424 | // |
||
11425 | // fallback via vanilla php |
||
11426 | // |
||
11427 | |||
11428 | // split to array, and remove invalid characters |
||
11429 | // && |
||
11430 | // extract relevant part, and join to make sting again |
||
11431 | 4 | return \implode('', \array_slice(self::str_split($str), $offset, $length)); |
|
11432 | } |
||
13694 |