Conditions | 20 |
Paths | 77 |
Total Lines | 104 |
Code Lines | 44 |
Lines | 0 |
Ratio | 0 % |
Tests | 12 |
CRAP Score | 157.2 |
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 |
||
10130 | 2 | public static function strrchr( |
|
10131 | string $haystack, |
||
10132 | string $needle, |
||
10133 | bool $before_needle = false, |
||
10134 | string $encoding = 'UTF-8', |
||
10135 | bool $clean_utf8 = false |
||
10136 | ) { |
||
10137 | 2 | if ($haystack === '' || $needle === '') { |
|
10138 | 2 | return false; |
|
10139 | } |
||
10140 | |||
10141 | 2 | if ($encoding !== 'UTF-8' && $encoding !== 'CP850') { |
|
10142 | 2 | $encoding = self::normalize_encoding($encoding, 'UTF-8'); |
|
10143 | } |
||
10144 | |||
10145 | 2 | if ($clean_utf8) { |
|
10146 | // "mb_strpos()" and "iconv_strpos()" returns wrong position, |
||
10147 | // if invalid characters are found in $haystack before $needle |
||
10148 | 2 | $needle = self::clean($needle); |
|
10149 | 2 | $haystack = self::clean($haystack); |
|
10150 | } |
||
10151 | |||
10152 | // |
||
10153 | // fallback via mbstring |
||
10154 | // |
||
10155 | |||
10156 | 2 | if (self::$SUPPORT['mbstring'] === true) { |
|
10157 | 2 | if ($encoding === 'UTF-8') { |
|
10158 | 2 | return \mb_strrchr($haystack, $needle, $before_needle); |
|
10159 | } |
||
10160 | |||
10161 | 2 | return \mb_strrchr($haystack, $needle, $before_needle, $encoding); |
|
10162 | } |
||
10163 | |||
10164 | // |
||
10165 | // fallback for binary || ascii only |
||
10166 | // |
||
10167 | |||
10168 | if ( |
||
10169 | !$before_needle |
||
10170 | && |
||
10171 | ( |
||
10172 | $encoding === 'CP850' |
||
10173 | || |
||
10174 | $encoding === 'ASCII' |
||
10175 | ) |
||
10176 | ) { |
||
10177 | return \strrchr($haystack, $needle); |
||
10178 | } |
||
10179 | |||
10180 | if ( |
||
10181 | $encoding !== 'UTF-8' |
||
10182 | && |
||
10183 | self::$SUPPORT['mbstring'] === false |
||
10184 | ) { |
||
10185 | /** |
||
10186 | * @psalm-suppress ImpureFunctionCall - is is only a warning |
||
10187 | */ |
||
10188 | \trigger_error('UTF8::strrchr() without mbstring cannot handle "' . $encoding . '" encoding', \E_USER_WARNING); |
||
10189 | } |
||
10190 | |||
10191 | // |
||
10192 | // fallback via iconv |
||
10193 | // |
||
10194 | |||
10195 | if (self::$SUPPORT['iconv'] === true) { |
||
10196 | $needle_tmp = self::substr($needle, 0, 1, $encoding); |
||
10197 | if ($needle_tmp === false) { |
||
10198 | return false; |
||
10199 | } |
||
10200 | $needle = $needle_tmp; |
||
10201 | |||
10202 | $pos = \iconv_strrpos($haystack, $needle, $encoding); |
||
10203 | if ($pos === false) { |
||
10204 | return false; |
||
10205 | } |
||
10206 | |||
10207 | if ($before_needle) { |
||
10208 | return self::substr($haystack, 0, $pos, $encoding); |
||
10209 | } |
||
10210 | |||
10211 | return self::substr($haystack, $pos, null, $encoding); |
||
10212 | } |
||
10213 | |||
10214 | // |
||
10215 | // fallback via vanilla php |
||
10216 | // |
||
10217 | |||
10218 | $needle_tmp = self::substr($needle, 0, 1, $encoding); |
||
10219 | if ($needle_tmp === false) { |
||
10220 | return false; |
||
10221 | } |
||
10222 | $needle = $needle_tmp; |
||
10223 | |||
10224 | $pos = self::strrpos($haystack, $needle, 0, $encoding); |
||
10225 | if ($pos === false) { |
||
10226 | return false; |
||
10227 | } |
||
10228 | |||
10229 | if ($before_needle) { |
||
10230 | return self::substr($haystack, 0, $pos, $encoding); |
||
10231 | } |
||
10232 | |||
10233 | return self::substr($haystack, $pos, null, $encoding); |
||
10234 | } |
||
13706 |