Conditions | 11 |
Paths | 10 |
Total Lines | 51 |
Code Lines | 25 |
Lines | 0 |
Ratio | 0 % |
Tests | 22 |
CRAP Score | 11.209 |
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 |
||
6694 | 6 | public static function str_limit_after_word( |
|
6695 | string $str, |
||
6696 | int $length = 100, |
||
6697 | string $str_add_on = '…', |
||
6698 | string $encoding = 'UTF-8' |
||
6699 | ): string { |
||
6700 | 6 | if ($str === '' || $length <= 0) { |
|
6701 | 2 | return ''; |
|
6702 | } |
||
6703 | |||
6704 | 6 | if ($encoding === 'UTF-8') { |
|
6705 | 2 | if ((int) \mb_strlen($str) <= $length) { |
|
6706 | 2 | return $str; |
|
6707 | } |
||
6708 | |||
6709 | 2 | if (\mb_substr($str, $length - 1, 1) === ' ') { |
|
6710 | 2 | return ((string) \mb_substr($str, 0, $length - 1)) . $str_add_on; |
|
6711 | } |
||
6712 | |||
6713 | 2 | $str = \mb_substr($str, 0, $length); |
|
6714 | |||
6715 | 2 | $array = \explode(' ', $str, -1); |
|
6716 | 2 | $new_str = \implode(' ', $array); |
|
6717 | |||
6718 | 2 | if ($new_str === '') { |
|
6719 | 2 | return ((string) \mb_substr($str, 0, $length - 1)) . $str_add_on; |
|
6720 | } |
||
6721 | } else { |
||
6722 | 4 | if ((int) self::strlen($str, $encoding) <= $length) { |
|
6723 | return $str; |
||
6724 | } |
||
6725 | |||
6726 | 4 | if (self::substr($str, $length - 1, 1, $encoding) === ' ') { |
|
6727 | 3 | return ((string) self::substr($str, 0, $length - 1, $encoding)) . $str_add_on; |
|
6728 | } |
||
6729 | |||
6730 | /** @noinspection CallableParameterUseCaseInTypeContextInspection - FP */ |
||
6731 | 1 | $str = self::substr($str, 0, $length, $encoding); |
|
6732 | 1 | if ($str === false) { |
|
6733 | return '' . $str_add_on; |
||
6734 | } |
||
6735 | |||
6736 | 1 | $array = \explode(' ', $str, -1); |
|
6737 | 1 | $new_str = \implode(' ', $array); |
|
6738 | |||
6739 | 1 | if ($new_str === '') { |
|
6740 | return ((string) self::substr($str, 0, $length - 1, $encoding)) . $str_add_on; |
||
6741 | } |
||
6742 | } |
||
6743 | |||
6744 | 3 | return $new_str . $str_add_on; |
|
6745 | } |
||
13722 |