Conditions | 17 |
Paths | 128 |
Total Lines | 73 |
Code Lines | 34 |
Lines | 0 |
Ratio | 0 % |
Tests | 25 |
CRAP Score | 20.023 |
Changes | 9 | ||
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 |
||
11490 | 5 | public static function substr_count( |
|
11491 | string $haystack, |
||
11492 | string $needle, |
||
11493 | int $offset = 0, |
||
11494 | int $length = null, |
||
11495 | string $encoding = 'UTF-8', |
||
11496 | bool $clean_utf8 = false |
||
11497 | ) { |
||
11498 | 5 | if ($needle === '') { |
|
11499 | 2 | return false; |
|
11500 | } |
||
11501 | |||
11502 | 5 | if ($haystack === '') { |
|
11503 | 2 | if (\PHP_VERSION_ID >= 80000) { |
|
11504 | 2 | return 0; |
|
11505 | } |
||
11506 | |||
11507 | return 0; |
||
11508 | } |
||
11509 | |||
11510 | 5 | if ($length === 0) { |
|
11511 | 2 | return 0; |
|
11512 | } |
||
11513 | |||
11514 | 5 | if ($encoding !== 'UTF-8' && $encoding !== 'CP850') { |
|
11515 | 2 | $encoding = self::normalize_encoding($encoding, 'UTF-8'); |
|
11516 | } |
||
11517 | |||
11518 | 5 | if ($clean_utf8) { |
|
11519 | // "mb_strpos()" and "iconv_strpos()" returns wrong position, |
||
11520 | // if invalid characters are found in $haystack before $needle |
||
11521 | $needle = self::clean($needle); |
||
11522 | $haystack = self::clean($haystack); |
||
11523 | } |
||
11524 | |||
11525 | 5 | if ($offset || $length > 0) { |
|
11526 | 2 | if ($length === null) { |
|
11527 | 2 | $length_tmp = self::strlen($haystack, $encoding); |
|
11528 | 2 | if ($length_tmp === false) { |
|
11529 | return false; |
||
11530 | } |
||
11531 | 2 | $length = $length_tmp; |
|
11532 | } |
||
11533 | |||
11534 | 2 | if ($encoding === 'UTF-8') { |
|
11535 | 2 | $haystack = (string) \mb_substr($haystack, $offset, $length); |
|
11536 | } else { |
||
11537 | 2 | $haystack = (string) \mb_substr($haystack, $offset, $length, $encoding); |
|
11538 | } |
||
11539 | } |
||
11540 | |||
11541 | if ( |
||
11542 | 5 | $encoding !== 'UTF-8' |
|
11543 | && |
||
11544 | 5 | self::$SUPPORT['mbstring'] === false |
|
11545 | ) { |
||
11546 | /** |
||
11547 | * @psalm-suppress ImpureFunctionCall - is is only a warning |
||
11548 | */ |
||
11549 | \trigger_error('UTF8::substr_count() without mbstring cannot handle "' . $encoding . '" encoding', \E_USER_WARNING); |
||
11550 | } |
||
11551 | |||
11552 | 5 | if (self::$SUPPORT['mbstring'] === true) { |
|
11553 | 5 | if ($encoding === 'UTF-8') { |
|
11554 | 5 | return \mb_substr_count($haystack, $needle); |
|
11555 | } |
||
11556 | |||
11557 | 2 | return \mb_substr_count($haystack, $needle, $encoding); |
|
11558 | } |
||
11559 | |||
11560 | \preg_match_all('/' . \preg_quote($needle, '/') . '/us', $haystack, $matches, \PREG_SET_ORDER); |
||
11561 | |||
11562 | return \count($matches); |
||
11563 | } |
||
13665 |