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 |
||
6648 | 6 | public static function str_limit_after_word( |
|
6649 | string $str, |
||
6650 | int $length = 100, |
||
6651 | string $str_add_on = '…', |
||
6652 | string $encoding = 'UTF-8' |
||
6653 | ): string { |
||
6654 | 6 | if ($str === '' || $length <= 0) { |
|
6655 | 2 | return ''; |
|
6656 | } |
||
6657 | |||
6658 | 6 | if ($encoding === 'UTF-8') { |
|
6659 | 2 | if ((int) \mb_strlen($str) <= $length) { |
|
6660 | 2 | return $str; |
|
6661 | } |
||
6662 | |||
6663 | 2 | if (\mb_substr($str, $length - 1, 1) === ' ') { |
|
6664 | 2 | return ((string) \mb_substr($str, 0, $length - 1)) . $str_add_on; |
|
6665 | } |
||
6666 | |||
6667 | 2 | $str = \mb_substr($str, 0, $length); |
|
6668 | |||
6669 | 2 | $array = \explode(' ', $str, -1); |
|
6670 | 2 | $new_str = \implode(' ', $array); |
|
6671 | |||
6672 | 2 | if ($new_str === '') { |
|
6673 | 2 | return ((string) \mb_substr($str, 0, $length - 1)) . $str_add_on; |
|
6674 | } |
||
6675 | } else { |
||
6676 | 4 | if ((int) self::strlen($str, $encoding) <= $length) { |
|
6677 | return $str; |
||
6678 | } |
||
6679 | |||
6680 | 4 | if (self::substr($str, $length - 1, 1, $encoding) === ' ') { |
|
6681 | 3 | return ((string) self::substr($str, 0, $length - 1, $encoding)) . $str_add_on; |
|
6682 | } |
||
6683 | |||
6684 | /** @noinspection CallableParameterUseCaseInTypeContextInspection - FP */ |
||
6685 | 1 | $str = self::substr($str, 0, $length, $encoding); |
|
6686 | 1 | if ($str === false) { |
|
6687 | return '' . $str_add_on; |
||
6688 | } |
||
6689 | |||
6690 | 1 | $array = \explode(' ', $str, -1); |
|
6691 | 1 | $new_str = \implode(' ', $array); |
|
6692 | |||
6693 | 1 | if ($new_str === '') { |
|
6694 | return ((string) self::substr($str, 0, $length - 1, $encoding)) . $str_add_on; |
||
6695 | } |
||
6696 | } |
||
6697 | |||
6698 | 3 | return $new_str . $str_add_on; |
|
6699 | } |
||
13694 |