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