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 |
||
3865 | 21 | public static function is_utf16($str, bool $check_if_string_is_binary = true) |
|
3866 | { |
||
3867 | // init |
||
3868 | 21 | $str = (string) $str; |
|
3869 | 21 | $str_chars = []; |
|
3870 | |||
3871 | // fix for the "binary"-check |
||
3872 | 21 | if ($check_if_string_is_binary !== false && self::string_has_bom($str)) { |
|
3873 | 2 | $check_if_string_is_binary = false; |
|
3874 | } |
||
3875 | |||
3876 | if ( |
||
3877 | 21 | $check_if_string_is_binary |
|
3878 | && |
||
3879 | 21 | !self::is_binary($str, true) |
|
3880 | ) { |
||
3881 | 2 | return false; |
|
3882 | } |
||
3883 | |||
3884 | 21 | if (self::$SUPPORT['mbstring'] === false) { |
|
3885 | /** |
||
3886 | * @psalm-suppress ImpureFunctionCall - is is only a warning |
||
3887 | */ |
||
3888 | 3 | \trigger_error('UTF8::is_utf16() without mbstring may did not work correctly', \E_USER_WARNING); |
|
3889 | } |
||
3890 | |||
3891 | 21 | $str = self::remove_bom($str); |
|
3892 | |||
3893 | 21 | $maybe_utf16le = 0; |
|
3894 | 21 | $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-16LE'); |
|
3895 | 21 | if ($test) { |
|
3896 | 15 | $test2 = \mb_convert_encoding($test, 'UTF-16LE', 'UTF-8'); |
|
3897 | 15 | $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-16LE'); |
|
3898 | 15 | if ($test3 === $test) { |
|
3899 | /** |
||
3900 | * @psalm-suppress RedundantCondition |
||
3901 | */ |
||
3902 | 15 | if ($str_chars === []) { |
|
3903 | 15 | $str_chars = self::count_chars($str, true, false); |
|
3904 | } |
||
3905 | 15 | foreach (self::count_chars($test3) as $test3char => &$test3charEmpty) { |
|
3906 | 15 | if (\in_array($test3char, $str_chars, true)) { |
|
3907 | 15 | ++$maybe_utf16le; |
|
3908 | } |
||
3909 | } |
||
3910 | 15 | unset($test3charEmpty); |
|
3911 | } |
||
3912 | } |
||
3913 | |||
3914 | 21 | $maybe_utf16be = 0; |
|
3915 | 21 | $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-16BE'); |
|
3916 | 21 | if ($test) { |
|
3917 | 15 | $test2 = \mb_convert_encoding($test, 'UTF-16BE', 'UTF-8'); |
|
3918 | 15 | $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-16BE'); |
|
3919 | 15 | if ($test3 === $test) { |
|
3920 | 15 | if ($str_chars === []) { |
|
3921 | 7 | $str_chars = self::count_chars($str, true, false); |
|
3922 | } |
||
3923 | 15 | foreach (self::count_chars($test3) as $test3char => &$test3charEmpty) { |
|
3924 | 15 | if (\in_array($test3char, $str_chars, true)) { |
|
3925 | 15 | ++$maybe_utf16be; |
|
3926 | } |
||
3927 | } |
||
3928 | 15 | unset($test3charEmpty); |
|
3929 | } |
||
3930 | } |
||
3931 | |||
3932 | 21 | if ($maybe_utf16be !== $maybe_utf16le) { |
|
3933 | 7 | if ($maybe_utf16le > $maybe_utf16be) { |
|
3934 | 5 | return 1; |
|
3935 | } |
||
3936 | |||
3937 | 6 | return 2; |
|
3938 | } |
||
3939 | |||
3940 | 17 | return false; |
|
3941 | } |
||
13693 |