Conditions | 7 |
Paths | 7 |
Total Lines | 43 |
Code Lines | 20 |
Lines | 0 |
Ratio | 0 % |
Tests | 20 |
CRAP Score | 7.0052 |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
8792 | 22 | public static function str_truncate( |
|
8793 | string $str, |
||
8794 | int $length, |
||
8795 | string $substring = '', |
||
8796 | string $encoding = 'UTF-8' |
||
8797 | ): string { |
||
8798 | 22 | if ($str === '') { |
|
8799 | return ''; |
||
8800 | } |
||
8801 | |||
8802 | 22 | if ($encoding === 'UTF-8') { |
|
8803 | 10 | if ($length >= (int) \mb_strlen($str)) { |
|
8804 | 2 | return $str; |
|
8805 | } |
||
8806 | |||
8807 | 8 | if ($substring !== '') { |
|
8808 | 4 | $length -= (int) \mb_strlen($substring); |
|
8809 | |||
8810 | /** @noinspection UnnecessaryCastingInspection */ |
||
8811 | 4 | return (string) \mb_substr($str, 0, $length) . $substring; |
|
8812 | } |
||
8813 | |||
8814 | 4 | return (string) \mb_substr($str, 0, $length); |
|
8815 | } |
||
8816 | |||
8817 | 12 | $encoding = self::normalize_encoding($encoding, 'UTF-8'); |
|
8818 | |||
8819 | 12 | if ($length >= (int) self::strlen($str, $encoding)) { |
|
8820 | 2 | return $str; |
|
8821 | } |
||
8822 | |||
8823 | 10 | if ($substring !== '') { |
|
8824 | 6 | $length -= (int) self::strlen($substring, $encoding); |
|
8825 | } |
||
8826 | |||
8827 | return ( |
||
8828 | 10 | (string) self::substr( |
|
8829 | 10 | $str, |
|
8830 | 10 | 0, |
|
8831 | 10 | $length, |
|
8832 | 10 | $encoding |
|
8833 | ) |
||
8834 | 10 | ) . $substring; |
|
8835 | } |
||
13694 |