Conditions | 31 |
Paths | 1886 |
Total Lines | 136 |
Code Lines | 62 |
Lines | 0 |
Ratio | 0 % |
Tests | 21 |
CRAP Score | 258.0441 |
Changes | 10 | ||
Bugs | 5 | 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 |
||
10564 | 35 | public static function strrpos( |
|
10565 | string $haystack, |
||
10566 | $needle, |
||
10567 | int $offset = 0, |
||
10568 | string $encoding = 'UTF-8', |
||
10569 | bool $clean_utf8 = false |
||
10570 | ) { |
||
10571 | 35 | if ($haystack === '') { |
|
10572 | 4 | if (\PHP_VERSION_ID >= 80000) { |
|
10573 | 4 | if ($needle === '') { |
|
10574 | 4 | return 0; |
|
10575 | } |
||
10576 | } else { |
||
10577 | return false; |
||
10578 | } |
||
10579 | } |
||
10580 | |||
10581 | // iconv and mbstring do not support integer $needle |
||
10582 | 35 | if ((int) $needle === $needle && $needle >= 0) { |
|
10583 | 1 | $needle = (string) self::chr($needle); |
|
10584 | } |
||
10585 | 35 | $needle = (string) $needle; |
|
10586 | |||
10587 | 35 | if ($haystack === '') { |
|
10588 | 2 | if (\PHP_VERSION_ID >= 80000 && $needle === '') { |
|
10589 | return 0; |
||
10590 | } |
||
10591 | |||
10592 | 2 | return false; |
|
10593 | } |
||
10594 | |||
10595 | 34 | if ($needle === '' && \PHP_VERSION_ID < 80000) { |
|
10596 | return false; |
||
10597 | } |
||
10598 | |||
10599 | 34 | if ($clean_utf8) { |
|
10600 | // mb_strrpos && iconv_strrpos is not tolerant to invalid characters |
||
10601 | 4 | $needle = self::clean($needle); |
|
10602 | 4 | $haystack = self::clean($haystack); |
|
10603 | } |
||
10604 | |||
10605 | 34 | if ($encoding !== 'UTF-8' && $encoding !== 'CP850') { |
|
10606 | 8 | $encoding = self::normalize_encoding($encoding, 'UTF-8'); |
|
10607 | } |
||
10608 | |||
10609 | // |
||
10610 | // fallback via mbstring |
||
10611 | // |
||
10612 | |||
10613 | 34 | if (self::$SUPPORT['mbstring'] === true) { |
|
10614 | 34 | if ($encoding === 'UTF-8') { |
|
10615 | 34 | return \mb_strrpos($haystack, $needle, $offset); |
|
10616 | } |
||
10617 | |||
10618 | 2 | return \mb_strrpos($haystack, $needle, $offset, $encoding); |
|
10619 | } |
||
10620 | |||
10621 | // |
||
10622 | // fallback for binary || ascii only |
||
10623 | // |
||
10624 | |||
10625 | if ( |
||
10626 | $encoding === 'CP850' |
||
10627 | || |
||
10628 | $encoding === 'ASCII' |
||
10629 | ) { |
||
10630 | return \strrpos($haystack, $needle, $offset); |
||
10631 | } |
||
10632 | |||
10633 | if ( |
||
10634 | $encoding !== 'UTF-8' |
||
10635 | && |
||
10636 | self::$SUPPORT['mbstring'] === false |
||
10637 | ) { |
||
10638 | /** |
||
10639 | * @psalm-suppress ImpureFunctionCall - is is only a warning |
||
10640 | */ |
||
10641 | \trigger_error('UTF8::strrpos() without mbstring cannot handle "' . $encoding . '" encoding', \E_USER_WARNING); |
||
10642 | } |
||
10643 | |||
10644 | // |
||
10645 | // fallback via intl |
||
10646 | // |
||
10647 | |||
10648 | if ( |
||
10649 | $offset >= 0 // grapheme_strrpos() can't handle negative offset |
||
10650 | && |
||
10651 | $encoding === 'UTF-8' // INFO: "grapheme_strrpos()" can't handle other encodings |
||
10652 | && |
||
10653 | self::$SUPPORT['intl'] === true |
||
10654 | ) { |
||
10655 | $return_tmp = \grapheme_strrpos($haystack, $needle, $offset); |
||
10656 | if ($return_tmp !== false) { |
||
10657 | return $return_tmp; |
||
10658 | } |
||
10659 | } |
||
10660 | |||
10661 | // |
||
10662 | // fallback for ascii only |
||
10663 | // |
||
10664 | |||
10665 | if (ASCII::is_ascii($haystack . $needle)) { |
||
10666 | return \strrpos($haystack, $needle, $offset); |
||
10667 | } |
||
10668 | |||
10669 | // |
||
10670 | // fallback via vanilla php |
||
10671 | // |
||
10672 | |||
10673 | $haystack_tmp = null; |
||
10674 | if ($offset > 0) { |
||
10675 | $haystack_tmp = self::substr($haystack, $offset); |
||
10676 | } elseif ($offset < 0) { |
||
10677 | $haystack_tmp = self::substr($haystack, 0, $offset); |
||
10678 | $offset = 0; |
||
10679 | } |
||
10680 | |||
10681 | if ($haystack_tmp !== null) { |
||
10682 | if ($haystack_tmp === false) { |
||
10683 | $haystack_tmp = ''; |
||
10684 | } |
||
10685 | $haystack = (string) $haystack_tmp; |
||
10686 | } |
||
10687 | |||
10688 | $pos = \strrpos($haystack, $needle); |
||
10689 | if ($pos === false) { |
||
10690 | return false; |
||
10691 | } |
||
10692 | |||
10693 | /** @var false|string $str_tmp - needed for PhpStan (stubs error) */ |
||
10694 | $str_tmp = \substr($haystack, 0, $pos); |
||
10695 | if ($str_tmp === false) { |
||
10696 | return false; |
||
10697 | } |
||
10698 | |||
10699 | return $offset + (int) self::strlen($str_tmp); |
||
10700 | } |
||
13706 |