Conditions | 18 |
Paths | 194 |
Total Lines | 76 |
Code Lines | 41 |
Lines | 0 |
Ratio | 0 % |
Tests | 40 |
CRAP Score | 18 |
Changes | 8 | ||
Bugs | 5 | 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 |
||
3881 | 21 | public static function is_utf16($str, bool $check_if_string_is_binary = true) |
|
3882 | { |
||
3883 | // init |
||
3884 | 21 | $str = (string) $str; |
|
3885 | 21 | $str_chars = []; |
|
3886 | |||
3887 | // fix for the "binary"-check |
||
3888 | 21 | if ($check_if_string_is_binary !== false && self::string_has_bom($str)) { |
|
3889 | 2 | $check_if_string_is_binary = false; |
|
3890 | } |
||
3891 | |||
3892 | if ( |
||
3893 | 21 | $check_if_string_is_binary |
|
3894 | && |
||
3895 | 21 | !self::is_binary($str, true) |
|
3896 | ) { |
||
3897 | 2 | return false; |
|
3898 | } |
||
3899 | |||
3900 | 21 | if (self::$SUPPORT['mbstring'] === false) { |
|
3901 | /** |
||
3902 | * @psalm-suppress ImpureFunctionCall - is is only a warning |
||
3903 | */ |
||
3904 | 3 | \trigger_error('UTF8::is_utf16() without mbstring may did not work correctly', \E_USER_WARNING); |
|
3905 | } |
||
3906 | |||
3907 | 21 | $str = self::remove_bom($str); |
|
3908 | |||
3909 | 21 | $maybe_utf16le = 0; |
|
3910 | 21 | $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-16LE'); |
|
3911 | 21 | if ($test) { |
|
3912 | 21 | $test2 = \mb_convert_encoding($test, 'UTF-16LE', 'UTF-8'); |
|
3913 | 21 | $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-16LE'); |
|
3914 | 21 | if ($test3 === $test) { |
|
3915 | /** |
||
3916 | * @psalm-suppress RedundantCondition |
||
3917 | */ |
||
3918 | 21 | if ($str_chars === []) { |
|
3919 | 21 | $str_chars = self::count_chars($str, true, false); |
|
3920 | } |
||
3921 | 21 | foreach (self::count_chars($test3) as $test3char => &$test3charEmpty) { |
|
3922 | 21 | if (\in_array($test3char, $str_chars, true)) { |
|
3923 | 5 | ++$maybe_utf16le; |
|
3924 | } |
||
3925 | } |
||
3926 | 21 | unset($test3charEmpty); |
|
3927 | } |
||
3928 | } |
||
3929 | |||
3930 | 21 | $maybe_utf16be = 0; |
|
3931 | 21 | $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-16BE'); |
|
3932 | 21 | if ($test) { |
|
3933 | 21 | $test2 = \mb_convert_encoding($test, 'UTF-16BE', 'UTF-8'); |
|
3934 | 21 | $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-16BE'); |
|
3935 | 21 | if ($test3 === $test) { |
|
3936 | 21 | if ($str_chars === []) { |
|
3937 | 11 | $str_chars = self::count_chars($str, true, false); |
|
3938 | } |
||
3939 | 21 | foreach (self::count_chars($test3) as $test3char => &$test3charEmpty) { |
|
3940 | 21 | if (\in_array($test3char, $str_chars, true)) { |
|
3941 | 6 | ++$maybe_utf16be; |
|
3942 | } |
||
3943 | } |
||
3944 | 21 | unset($test3charEmpty); |
|
3945 | } |
||
3946 | } |
||
3947 | |||
3948 | 21 | if ($maybe_utf16be !== $maybe_utf16le) { |
|
3949 | 7 | if ($maybe_utf16le > $maybe_utf16be) { |
|
3950 | 5 | return 1; |
|
3951 | } |
||
3952 | |||
3953 | 6 | return 2; |
|
3954 | } |
||
3955 | |||
3956 | 17 | return false; |
|
3957 | } |
||
13722 |