Conditions | 11 |
Paths | 10 |
Total Lines | 51 |
Code Lines | 25 |
Lines | 0 |
Ratio | 0 % |
Tests | 21 |
CRAP Score | 11.2363 |
Changes | 3 | ||
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 |
||
6677 | public static function str_limit_after_word( |
||
6678 | string $str, |
||
6679 | int $length = 100, |
||
6680 | string $str_add_on = '…', |
||
6681 | string $encoding = 'UTF-8' |
||
6682 | ): string { |
||
6683 | 6 | if ($str === '' || $length <= 0) { |
|
6684 | 2 | return ''; |
|
6685 | } |
||
6686 | |||
6687 | 6 | if ($encoding === 'UTF-8') { |
|
6688 | 2 | if ((int) \mb_strlen($str) <= $length) { |
|
6689 | 2 | return $str; |
|
6690 | } |
||
6691 | |||
6692 | 2 | if (\mb_substr($str, $length - 1, 1) === ' ') { |
|
6693 | 2 | return ((string) \mb_substr($str, 0, $length - 1)) . $str_add_on; |
|
6694 | } |
||
6695 | |||
6696 | 2 | $str = \mb_substr($str, 0, $length); |
|
6697 | |||
6698 | 2 | $array = \explode(' ', $str, -1); |
|
6699 | 2 | $new_str = \implode(' ', $array); |
|
6700 | |||
6701 | 2 | if ($new_str === '') { |
|
6702 | 2 | return ((string) \mb_substr($str, 0, $length - 1)) . $str_add_on; |
|
6703 | } |
||
6704 | } else { |
||
6705 | 4 | if ((int) self::strlen($str, $encoding) <= $length) { |
|
6706 | return $str; |
||
6707 | } |
||
6708 | |||
6709 | 4 | if (self::substr($str, $length - 1, 1, $encoding) === ' ') { |
|
6710 | 3 | return ((string) self::substr($str, 0, $length - 1, $encoding)) . $str_add_on; |
|
6711 | } |
||
6712 | |||
6713 | /** @noinspection CallableParameterUseCaseInTypeContextInspection - FP */ |
||
6714 | 1 | $str = self::substr($str, 0, $length, $encoding); |
|
6715 | 1 | if ($str === false) { |
|
6716 | return '' . $str_add_on; |
||
6717 | } |
||
6718 | |||
6719 | 1 | $array = \explode(' ', $str, -1); |
|
6720 | 1 | $new_str = \implode(' ', $array); |
|
6721 | |||
6722 | 1 | if ($new_str === '') { |
|
6723 | return ((string) self::substr($str, 0, $length - 1, $encoding)) . $str_add_on; |
||
6724 | } |
||
6725 | } |
||
6726 | |||
6727 | 3 | return $new_str . $str_add_on; |
|
6728 | } |
||
13723 |