Conditions | 20 |
Paths | 77 |
Total Lines | 104 |
Code Lines | 44 |
Lines | 0 |
Ratio | 0 % |
Tests | 11 |
CRAP Score | 167.9966 |
Changes | 4 | ||
Bugs | 0 | 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 |
||
10099 | public static function strrchr( |
||
10100 | string $haystack, |
||
10101 | string $needle, |
||
10102 | bool $before_needle = false, |
||
10103 | string $encoding = 'UTF-8', |
||
10104 | bool $clean_utf8 = false |
||
10105 | ) { |
||
10106 | 2 | if ($haystack === '' || $needle === '') { |
|
10107 | 2 | return false; |
|
10108 | } |
||
10109 | |||
10110 | 2 | if ($encoding !== 'UTF-8' && $encoding !== 'CP850') { |
|
10111 | 2 | $encoding = self::normalize_encoding($encoding, 'UTF-8'); |
|
10112 | } |
||
10113 | |||
10114 | 2 | if ($clean_utf8) { |
|
10115 | // "mb_strpos()" and "iconv_strpos()" returns wrong position, |
||
10116 | // if invalid characters are found in $haystack before $needle |
||
10117 | 2 | $needle = self::clean($needle); |
|
10118 | 2 | $haystack = self::clean($haystack); |
|
10119 | } |
||
10120 | |||
10121 | // |
||
10122 | // fallback via mbstring |
||
10123 | // |
||
10124 | |||
10125 | 2 | if (self::$SUPPORT['mbstring'] === true) { |
|
10126 | 2 | if ($encoding === 'UTF-8') { |
|
10127 | 2 | return \mb_strrchr($haystack, $needle, $before_needle); |
|
10128 | } |
||
10129 | |||
10130 | 2 | return \mb_strrchr($haystack, $needle, $before_needle, $encoding); |
|
10131 | } |
||
10132 | |||
10133 | // |
||
10134 | // fallback for binary || ascii only |
||
10135 | // |
||
10136 | |||
10137 | if ( |
||
10138 | !$before_needle |
||
10139 | && |
||
10140 | ( |
||
10141 | $encoding === 'CP850' |
||
10142 | || |
||
10143 | $encoding === 'ASCII' |
||
10144 | ) |
||
10145 | ) { |
||
10146 | return \strrchr($haystack, $needle); |
||
10147 | } |
||
10148 | |||
10149 | if ( |
||
10150 | $encoding !== 'UTF-8' |
||
10151 | && |
||
10152 | self::$SUPPORT['mbstring'] === false |
||
10153 | ) { |
||
10154 | /** |
||
10155 | * @psalm-suppress ImpureFunctionCall - is is only a warning |
||
10156 | */ |
||
10157 | \trigger_error('UTF8::strrchr() without mbstring cannot handle "' . $encoding . '" encoding', \E_USER_WARNING); |
||
10158 | } |
||
10159 | |||
10160 | // |
||
10161 | // fallback via iconv |
||
10162 | // |
||
10163 | |||
10164 | if (self::$SUPPORT['iconv'] === true) { |
||
10165 | $needle_tmp = self::substr($needle, 0, 1, $encoding); |
||
10166 | if ($needle_tmp === false) { |
||
10167 | return false; |
||
10168 | } |
||
10169 | $needle = $needle_tmp; |
||
10170 | |||
10171 | $pos = \iconv_strrpos($haystack, $needle, $encoding); |
||
10172 | if ($pos === false) { |
||
10173 | return false; |
||
10174 | } |
||
10175 | |||
10176 | if ($before_needle) { |
||
10177 | return self::substr($haystack, 0, $pos, $encoding); |
||
10178 | } |
||
10179 | |||
10180 | return self::substr($haystack, $pos, null, $encoding); |
||
10181 | } |
||
10182 | |||
10183 | // |
||
10184 | // fallback via vanilla php |
||
10185 | // |
||
10186 | |||
10187 | $needle_tmp = self::substr($needle, 0, 1, $encoding); |
||
10188 | if ($needle_tmp === false) { |
||
10189 | return false; |
||
10190 | } |
||
10191 | $needle = $needle_tmp; |
||
10192 | |||
10193 | $pos = self::strrpos($haystack, $needle, 0, $encoding); |
||
10194 | if ($pos === false) { |
||
10195 | return false; |
||
10196 | } |
||
10197 | |||
10198 | if ($before_needle) { |
||
10199 | return self::substr($haystack, 0, $pos, $encoding); |
||
10200 | } |
||
10201 | |||
10202 | return self::substr($haystack, $pos, null, $encoding); |
||
10203 | } |
||
13693 |