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