Conditions | 7 |
Paths | 7 |
Total Lines | 43 |
Code Lines | 20 |
Lines | 0 |
Ratio | 0 % |
Tests | 15 |
CRAP Score | 7.0119 |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
8838 | 22 | public static function str_truncate( |
|
8839 | string $str, |
||
8840 | int $length, |
||
8841 | string $substring = '', |
||
8842 | string $encoding = 'UTF-8' |
||
8843 | ): string { |
||
8844 | 22 | if ($str === '') { |
|
8845 | return ''; |
||
8846 | } |
||
8847 | |||
8848 | 22 | if ($encoding === 'UTF-8') { |
|
8849 | 10 | if ($length >= (int) \mb_strlen($str)) { |
|
8850 | 2 | return $str; |
|
8851 | } |
||
8852 | |||
8853 | 8 | if ($substring !== '') { |
|
8854 | 4 | $length -= (int) \mb_strlen($substring); |
|
8855 | |||
8856 | /** @noinspection UnnecessaryCastingInspection */ |
||
8857 | 4 | return (string) \mb_substr($str, 0, $length) . $substring; |
|
8858 | } |
||
8859 | |||
8860 | 4 | return (string) \mb_substr($str, 0, $length); |
|
8861 | } |
||
8862 | |||
8863 | 12 | $encoding = self::normalize_encoding($encoding, 'UTF-8'); |
|
8864 | |||
8865 | 12 | if ($length >= (int) self::strlen($str, $encoding)) { |
|
8866 | 2 | return $str; |
|
8867 | } |
||
8868 | |||
8869 | 10 | if ($substring !== '') { |
|
8870 | 6 | $length -= (int) self::strlen($substring, $encoding); |
|
8871 | } |
||
8872 | |||
8873 | return ( |
||
8874 | 10 | (string) self::substr( |
|
8875 | $str, |
||
8876 | 0, |
||
8877 | $length, |
||
8878 | $encoding |
||
8879 | ) |
||
8880 | ) . $substring; |
||
8881 | } |
||
13722 |