Conditions | 9 |
Paths | 11 |
Total Lines | 51 |
Code Lines | 24 |
Lines | 0 |
Ratio | 0 % |
Tests | 19 |
CRAP Score | 9.0101 |
Changes | 6 | ||
Bugs | 1 | 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 |
||
12668 | 9 | public static function ucwords( |
|
12669 | string $str, |
||
12670 | array $exceptions = [], |
||
12671 | string $char_list = '', |
||
12672 | string $encoding = 'UTF-8', |
||
12673 | bool $clean_utf8 = false |
||
12674 | ): string { |
||
12675 | 9 | if (!$str) { |
|
12676 | 2 | return ''; |
|
12677 | } |
||
12678 | |||
12679 | // INFO: mb_convert_case($str, MB_CASE_TITLE); |
||
12680 | // -> MB_CASE_TITLE didn't only uppercase the first letter, it also lowercase all other letters |
||
12681 | |||
12682 | 8 | if ($clean_utf8) { |
|
12683 | // "mb_strpos()" and "iconv_strpos()" returns wrong position, |
||
12684 | // if invalid characters are found in $haystack before $needle |
||
12685 | 1 | $str = self::clean($str); |
|
12686 | } |
||
12687 | |||
12688 | 8 | $use_php_default_functions = !(bool) ($char_list . \implode('', $exceptions)); |
|
12689 | |||
12690 | if ( |
||
12691 | 8 | $use_php_default_functions |
|
12692 | && |
||
12693 | 8 | ASCII::is_ascii($str) |
|
12694 | ) { |
||
12695 | return \ucwords($str); |
||
12696 | } |
||
12697 | |||
12698 | 8 | $words = self::str_to_words($str, $char_list); |
|
12699 | 8 | $use_exceptions = $exceptions !== []; |
|
12700 | |||
12701 | 8 | $words_str = ''; |
|
12702 | 8 | foreach ($words as &$word) { |
|
12703 | 8 | if (!$word) { |
|
12704 | 8 | continue; |
|
12705 | } |
||
12706 | |||
12707 | if ( |
||
12708 | 8 | !$use_exceptions |
|
12709 | || |
||
12710 | 8 | !\in_array($word, $exceptions, true) |
|
12711 | ) { |
||
12712 | 8 | $words_str .= self::ucfirst($word, $encoding); |
|
12713 | } else { |
||
12714 | 1 | $words_str .= $word; |
|
12715 | } |
||
12716 | } |
||
12717 | |||
12718 | 8 | return $words_str; |
|
12719 | } |
||
13665 |