Conditions | 31 |
Paths | 219 |
Total Lines | 137 |
Code Lines | 53 |
Lines | 0 |
Ratio | 0 % |
Tests | 32 |
CRAP Score | 54.1721 |
Changes | 16 | ||
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 |
||
11341 | 172 | public static function substr( |
|
11342 | string $str, |
||
11343 | int $offset = 0, |
||
11344 | int $length = null, |
||
11345 | string $encoding = 'UTF-8', |
||
11346 | bool $clean_utf8 = false |
||
11347 | ) { |
||
11348 | // empty string |
||
11349 | 172 | if ($str === '' || $length === 0) { |
|
11350 | 8 | return ''; |
|
11351 | } |
||
11352 | |||
11353 | 168 | if ($clean_utf8) { |
|
11354 | // iconv and mbstring are not tolerant to invalid encoding |
||
11355 | // further, their behaviour is inconsistent with that of PHP's substr |
||
11356 | 2 | $str = self::clean($str); |
|
11357 | } |
||
11358 | |||
11359 | // whole string |
||
11360 | 168 | if (!$offset && $length === null) { |
|
11361 | 7 | return $str; |
|
11362 | } |
||
11363 | |||
11364 | 163 | if ($encoding !== 'UTF-8' && $encoding !== 'CP850') { |
|
11365 | 19 | $encoding = self::normalize_encoding($encoding, 'UTF-8'); |
|
11366 | } |
||
11367 | |||
11368 | // |
||
11369 | // fallback via mbstring |
||
11370 | // |
||
11371 | |||
11372 | 163 | if (self::$SUPPORT['mbstring'] === true && $encoding === 'UTF-8') { |
|
11373 | 161 | if ($length === null) { |
|
11374 | 64 | return \mb_substr($str, $offset); |
|
11375 | } |
||
11376 | |||
11377 | 102 | return \mb_substr($str, $offset, $length); |
|
11378 | } |
||
11379 | |||
11380 | // |
||
11381 | // fallback for binary || ascii only |
||
11382 | // |
||
11383 | |||
11384 | if ( |
||
11385 | 4 | $encoding === 'CP850' |
|
11386 | || |
||
11387 | 4 | $encoding === 'ASCII' |
|
11388 | ) { |
||
11389 | if ($length === null) { |
||
11390 | return \substr($str, $offset); |
||
11391 | } |
||
11392 | |||
11393 | return \substr($str, $offset, $length); |
||
11394 | } |
||
11395 | |||
11396 | // otherwise we need the string-length |
||
11397 | 4 | $str_length = 0; |
|
11398 | 4 | if ($offset || $length === null) { |
|
11399 | 4 | $str_length = self::strlen($str, $encoding); |
|
11400 | } |
||
11401 | |||
11402 | // e.g.: invalid chars + mbstring not installed |
||
11403 | 4 | if ($str_length === false) { |
|
11404 | return false; |
||
11405 | } |
||
11406 | |||
11407 | // empty string |
||
11408 | 4 | if ($offset === $str_length && !$length) { |
|
11409 | return ''; |
||
11410 | } |
||
11411 | |||
11412 | // impossible |
||
11413 | 4 | if ($offset && $offset > $str_length) { |
|
11414 | return ''; |
||
11415 | } |
||
11416 | |||
11417 | 4 | $length = $length ?? $str_length; |
|
11418 | |||
11419 | if ( |
||
11420 | 4 | $encoding !== 'UTF-8' |
|
11421 | && |
||
11422 | 4 | self::$SUPPORT['mbstring'] === false |
|
11423 | ) { |
||
11424 | /** |
||
11425 | * @psalm-suppress ImpureFunctionCall - is is only a warning |
||
11426 | */ |
||
11427 | 2 | \trigger_error('UTF8::substr() without mbstring cannot handle "' . $encoding . '" encoding', \E_USER_WARNING); |
|
11428 | } |
||
11429 | |||
11430 | // |
||
11431 | // fallback via intl |
||
11432 | // |
||
11433 | |||
11434 | if ( |
||
11435 | 4 | $encoding === 'UTF-8' // INFO: "grapheme_substr()" can't handle other encodings |
|
11436 | && |
||
11437 | 4 | $offset >= 0 // grapheme_substr() can't handle negative offset |
|
11438 | && |
||
11439 | 4 | self::$SUPPORT['intl'] === true |
|
11440 | ) { |
||
11441 | $return_tmp = \grapheme_substr($str, $offset, $length); |
||
11442 | if ($return_tmp !== false) { |
||
11443 | return $return_tmp; |
||
11444 | } |
||
11445 | } |
||
11446 | |||
11447 | // |
||
11448 | // fallback via iconv |
||
11449 | // |
||
11450 | |||
11451 | if ( |
||
11452 | 4 | $length >= 0 // "iconv_substr()" can't handle negative length |
|
11453 | && |
||
11454 | 4 | self::$SUPPORT['iconv'] === true |
|
11455 | ) { |
||
11456 | $return_tmp = \iconv_substr($str, $offset, $length); |
||
11457 | if ($return_tmp !== false) { |
||
11458 | return $return_tmp; |
||
11459 | } |
||
11460 | } |
||
11461 | |||
11462 | // |
||
11463 | // fallback for ascii only |
||
11464 | // |
||
11465 | |||
11466 | 4 | if (ASCII::is_ascii($str)) { |
|
11467 | return \substr($str, $offset, $length); |
||
11468 | } |
||
11469 | |||
11470 | // |
||
11471 | // fallback via vanilla php |
||
11472 | // |
||
11473 | |||
11474 | // split to array, and remove invalid characters |
||
11475 | // && |
||
11476 | // extract relevant part, and join to make sting again |
||
11477 | 4 | return \implode('', \array_slice(self::str_split($str), $offset, $length)); |
|
11478 | } |
||
13722 |