Conditions | 19 |
Paths | 133 |
Total Lines | 104 |
Code Lines | 40 |
Lines | 0 |
Ratio | 0 % |
Tests | 26 |
CRAP Score | 23.7029 |
Changes | 15 | ||
Bugs | 8 | Features | 1 |
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 |
||
9503 | 174 | public static function strlen( |
|
9504 | string $str, |
||
9505 | string $encoding = 'UTF-8', |
||
9506 | bool $clean_utf8 = false |
||
9507 | ) { |
||
9508 | 174 | if ($str === '') { |
|
9509 | 25 | return 0; |
|
9510 | } |
||
9511 | |||
9512 | 172 | if ($encoding !== 'UTF-8' && $encoding !== 'CP850') { |
|
9513 | 12 | $encoding = self::normalize_encoding($encoding, 'UTF-8'); |
|
9514 | } |
||
9515 | |||
9516 | 172 | if ($clean_utf8) { |
|
9517 | // "mb_strlen" and "\iconv_strlen" returns wrong length, |
||
9518 | // if invalid characters are found in $str |
||
9519 | 5 | $str = self::clean($str); |
|
9520 | } |
||
9521 | |||
9522 | // |
||
9523 | // fallback via mbstring |
||
9524 | // |
||
9525 | |||
9526 | 172 | if (self::$SUPPORT['mbstring'] === true) { |
|
9527 | 166 | if ($encoding === 'UTF-8') { |
|
9528 | /** @noinspection PhpUsageOfSilenceOperatorInspection - ignore warnings, it's working anyway */ |
||
9529 | 166 | return @\mb_strlen($str); |
|
9530 | } |
||
9531 | |||
9532 | /** @noinspection PhpUsageOfSilenceOperatorInspection - ignore warnings, it's working anyway */ |
||
9533 | 4 | return @\mb_strlen($str, $encoding); |
|
9534 | } |
||
9535 | |||
9536 | // |
||
9537 | // fallback for binary || ascii only |
||
9538 | // |
||
9539 | |||
9540 | if ( |
||
9541 | 8 | $encoding === 'CP850' |
|
9542 | || |
||
9543 | 8 | $encoding === 'ASCII' |
|
9544 | ) { |
||
9545 | return \strlen($str); |
||
9546 | } |
||
9547 | |||
9548 | if ( |
||
9549 | 8 | $encoding !== 'UTF-8' |
|
9550 | && |
||
9551 | 8 | self::$SUPPORT['mbstring'] === false |
|
9552 | && |
||
9553 | 8 | self::$SUPPORT['iconv'] === false |
|
9554 | ) { |
||
9555 | /** |
||
9556 | * @psalm-suppress ImpureFunctionCall - is is only a warning |
||
9557 | */ |
||
9558 | 2 | \trigger_error('UTF8::strlen() without mbstring / iconv cannot handle "' . $encoding . '" encoding', \E_USER_WARNING); |
|
9559 | } |
||
9560 | |||
9561 | // |
||
9562 | // fallback via iconv |
||
9563 | // |
||
9564 | |||
9565 | 8 | if (self::$SUPPORT['iconv'] === true) { |
|
9566 | $return_tmp = \iconv_strlen($str, $encoding); |
||
9567 | if ($return_tmp !== false) { |
||
9568 | return $return_tmp; |
||
9569 | } |
||
9570 | } |
||
9571 | |||
9572 | // |
||
9573 | // fallback via intl |
||
9574 | // |
||
9575 | |||
9576 | if ( |
||
9577 | 8 | $encoding === 'UTF-8' // INFO: "grapheme_strlen()" can't handle other encodings |
|
9578 | && |
||
9579 | 8 | self::$SUPPORT['intl'] === true |
|
9580 | ) { |
||
9581 | $return_tmp = \grapheme_strlen($str); |
||
9582 | if ($return_tmp !== null) { |
||
9583 | return $return_tmp; |
||
9584 | } |
||
9585 | } |
||
9586 | |||
9587 | // |
||
9588 | // fallback for ascii only |
||
9589 | // |
||
9590 | |||
9591 | 8 | if (ASCII::is_ascii($str)) { |
|
9592 | 4 | return \strlen($str); |
|
9593 | } |
||
9594 | |||
9595 | // |
||
9596 | // fallback via vanilla php |
||
9597 | // |
||
9598 | |||
9599 | 8 | \preg_match_all('/./us', $str, $parts); |
|
9600 | |||
9601 | 8 | $return_tmp = \count($parts[0]); |
|
9602 | 8 | if ($return_tmp === 0) { |
|
9603 | return false; |
||
9604 | } |
||
9605 | |||
9606 | 8 | return $return_tmp; |
|
9607 | } |
||
13666 |