Conditions | 17 |
Paths | 46 |
Total Lines | 79 |
Code Lines | 37 |
Lines | 0 |
Ratio | 0 % |
Tests | 16 |
CRAP Score | 59.8794 |
Changes | 9 | ||
Bugs | 3 | 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 |
||
9427 | 13 | public static function stristr( |
|
9428 | string $haystack, |
||
9429 | string $needle, |
||
9430 | bool $before_needle = false, |
||
9431 | string $encoding = 'UTF-8', |
||
9432 | bool $clean_utf8 = false |
||
9433 | ) { |
||
9434 | 13 | if ($haystack === '') { |
|
9435 | 3 | if (\PHP_VERSION_ID >= 80000 && $needle === '') { |
|
9436 | 2 | return ''; |
|
9437 | } |
||
9438 | |||
9439 | 2 | return false; |
|
9440 | } |
||
9441 | |||
9442 | 11 | if ($clean_utf8) { |
|
9443 | // "mb_strpos()" and "iconv_strpos()" returns wrong position, |
||
9444 | // if invalid characters are found in $haystack before $needle |
||
9445 | 1 | $needle = self::clean($needle); |
|
9446 | 1 | $haystack = self::clean($haystack); |
|
9447 | } |
||
9448 | |||
9449 | 11 | if ($needle === '') { |
|
9450 | 2 | if (\PHP_VERSION_ID >= 80000) { |
|
9451 | 2 | return $haystack; |
|
9452 | } |
||
9453 | |||
9454 | return false; |
||
9455 | } |
||
9456 | |||
9457 | 10 | if (self::$SUPPORT['mbstring'] === true) { |
|
9458 | 10 | if ($encoding === 'UTF-8') { |
|
9459 | 10 | return \mb_stristr($haystack, $needle, $before_needle); |
|
9460 | } |
||
9461 | |||
9462 | 1 | $encoding = self::normalize_encoding($encoding, 'UTF-8'); |
|
9463 | |||
9464 | 1 | return \mb_stristr($haystack, $needle, $before_needle, $encoding); |
|
9465 | } |
||
9466 | |||
9467 | $encoding = self::normalize_encoding($encoding, 'UTF-8'); |
||
9468 | |||
9469 | if ( |
||
9470 | $encoding !== 'UTF-8' |
||
9471 | && |
||
9472 | self::$SUPPORT['mbstring'] === false |
||
9473 | ) { |
||
9474 | /** |
||
9475 | * @psalm-suppress ImpureFunctionCall - is is only a warning |
||
9476 | */ |
||
9477 | \trigger_error('UTF8::stristr() without mbstring cannot handle "' . $encoding . '" encoding', \E_USER_WARNING); |
||
9478 | } |
||
9479 | |||
9480 | if ( |
||
9481 | $encoding === 'UTF-8' // INFO: "grapheme_stristr()" can't handle other encodings |
||
9482 | && |
||
9483 | self::$SUPPORT['intl'] === true |
||
9484 | ) { |
||
9485 | $return_tmp = \grapheme_stristr($haystack, $needle, $before_needle); |
||
9486 | if ($return_tmp !== false) { |
||
9487 | return $return_tmp; |
||
9488 | } |
||
9489 | } |
||
9490 | |||
9491 | if (ASCII::is_ascii($needle . $haystack)) { |
||
9492 | return \stristr($haystack, $needle, $before_needle); |
||
9493 | } |
||
9494 | |||
9495 | \preg_match('/^(.*?)' . \preg_quote($needle, '/') . '/usi', $haystack, $match); |
||
9496 | |||
9497 | if (!isset($match[1])) { |
||
9498 | return false; |
||
9499 | } |
||
9500 | |||
9501 | if ($before_needle) { |
||
9502 | return $match[1]; |
||
9503 | } |
||
9504 | |||
9505 | return self::substr($haystack, (int) self::strlen($match[1], $encoding), null, $encoding); |
||
9506 | } |
||
13694 |