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