Conditions | 18 |
Paths | 13 |
Total Lines | 86 |
Code Lines | 42 |
Lines | 0 |
Ratio | 0 % |
Tests | 33 |
CRAP Score | 18.1872 |
Changes | 3 | ||
Bugs | 0 | Features | 1 |
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 |
||
8851 | 47 | public static function str_truncate_safe( |
|
8852 | string $str, |
||
8853 | int $length, |
||
8854 | string $substring = '', |
||
8855 | string $encoding = 'UTF-8', |
||
8856 | bool $ignore_do_not_split_words_for_one_word = false |
||
8857 | ): string { |
||
8858 | 47 | if ($str === '' || $length <= 0) { |
|
8859 | 1 | return $substring; |
|
8860 | } |
||
8861 | |||
8862 | 47 | if ($encoding === 'UTF-8') { |
|
8863 | 21 | if ($length >= (int) \mb_strlen($str)) { |
|
8864 | 5 | return $str; |
|
8865 | } |
||
8866 | |||
8867 | // need to further trim the string so we can append the substring |
||
8868 | 17 | $length -= (int) \mb_strlen($substring); |
|
8869 | 17 | if ($length <= 0) { |
|
8870 | 1 | return $substring; |
|
8871 | } |
||
8872 | |||
8873 | /** @var false|string $truncated - needed for PhpStan (stubs error) */ |
||
8874 | 17 | $truncated = \mb_substr($str, 0, $length); |
|
8875 | 17 | if ($truncated === false) { |
|
8876 | return ''; |
||
8877 | } |
||
8878 | |||
8879 | // if the last word was truncated |
||
8880 | 17 | $space_position = \mb_strpos($str, ' ', $length - 1); |
|
8881 | 17 | if ($space_position !== $length) { |
|
8882 | // find pos of the last occurrence of a space, get up to that |
||
8883 | 13 | $last_position = \mb_strrpos($truncated, ' ', 0); |
|
8884 | |||
8885 | if ( |
||
8886 | 13 | $last_position !== false |
|
8887 | || |
||
8888 | ( |
||
8889 | 3 | $space_position !== false |
|
8890 | && |
||
8891 | 13 | !$ignore_do_not_split_words_for_one_word |
|
8892 | ) |
||
8893 | ) { |
||
8894 | 17 | $truncated = (string) \mb_substr($truncated, 0, (int) $last_position); |
|
8895 | } |
||
8896 | } |
||
8897 | } else { |
||
8898 | 26 | $encoding = self::normalize_encoding($encoding, 'UTF-8'); |
|
8899 | |||
8900 | 26 | if ($length >= (int) self::strlen($str, $encoding)) { |
|
8901 | 4 | return $str; |
|
8902 | } |
||
8903 | |||
8904 | // need to further trim the string so we can append the substring |
||
8905 | 22 | $length -= (int) self::strlen($substring, $encoding); |
|
8906 | 22 | if ($length <= 0) { |
|
8907 | return $substring; |
||
8908 | } |
||
8909 | |||
8910 | 22 | $truncated = self::substr($str, 0, $length, $encoding); |
|
8911 | |||
8912 | 22 | if ($truncated === false) { |
|
8913 | return ''; |
||
8914 | } |
||
8915 | |||
8916 | // if the last word was truncated |
||
8917 | 22 | $space_position = self::strpos($str, ' ', $length - 1, $encoding); |
|
8918 | 22 | if ($space_position !== $length) { |
|
8919 | // find pos of the last occurrence of a space, get up to that |
||
8920 | 12 | $last_position = self::strrpos($truncated, ' ', 0, $encoding); |
|
8921 | |||
8922 | if ( |
||
8923 | 12 | $last_position !== false |
|
8924 | || |
||
8925 | ( |
||
8926 | 4 | $space_position !== false |
|
8927 | && |
||
8928 | 12 | !$ignore_do_not_split_words_for_one_word |
|
8929 | ) |
||
8930 | ) { |
||
8931 | 9 | $truncated = (string) self::substr($truncated, 0, (int) $last_position, $encoding); |
|
8932 | } |
||
8933 | } |
||
8934 | } |
||
8935 | |||
8936 | 39 | return $truncated . $substring; |
|
8937 | } |
||
13689 |