Conditions | 14 |
Paths | 20 |
Total Lines | 111 |
Code Lines | 60 |
Lines | 0 |
Ratio | 0 % |
Tests | 15 |
CRAP Score | 18.5707 |
Changes | 9 | ||
Bugs | 3 | 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 |
||
5817 | public static function str_detect_encoding($str) |
||
5818 | 8 | { |
|
5819 | // init |
||
5820 | $str = (string) $str; |
||
5821 | |||
5822 | // |
||
5823 | // 1.) check binary strings (010001001...) like UTF-16 / UTF-32 / PDF / Images / ... |
||
5824 | // |
||
5825 | 27 | ||
5826 | 10 | if (self::is_binary($str, self::string_has_bom($str) ? false : true)) { |
|
5827 | $is_utf32 = self::is_utf32($str, false); |
||
5828 | if ($is_utf32 === 1) { |
||
5829 | return 'UTF-32LE'; |
||
5830 | } |
||
5831 | if ($is_utf32 === 2) { |
||
5832 | return 'UTF-32BE'; |
||
5833 | 27 | } |
|
5834 | 19 | ||
5835 | $is_utf16 = self::is_utf16($str, false); |
||
5836 | if ($is_utf16 === 1) { |
||
5837 | return 'UTF-16LE'; |
||
5838 | } |
||
5839 | if ($is_utf16 === 2) { |
||
5840 | return 'UTF-16BE'; |
||
5841 | } |
||
5842 | 16 | ||
5843 | // is binary but not "UTF-16" or "UTF-32" |
||
5844 | return false; |
||
5845 | } |
||
5846 | |||
5847 | // |
||
5848 | // 2.) simple check for ASCII chars |
||
5849 | // |
||
5850 | |||
5851 | if (ASCII::is_ascii($str)) { |
||
5852 | return 'ASCII'; |
||
5853 | } |
||
5854 | |||
5855 | // |
||
5856 | // 3.) simple check for UTF-8 chars |
||
5857 | // |
||
5858 | |||
5859 | if (self::is_utf8_string($str)) { |
||
5860 | return 'UTF-8'; |
||
5861 | } |
||
5862 | |||
5863 | // |
||
5864 | // 4.) check via "mb_detect_encoding()" |
||
5865 | // |
||
5866 | // INFO: UTF-16, UTF-32, UCS2 and UCS4, encoding detection will fail always with "mb_detect_encoding()" |
||
5867 | |||
5868 | $encoding_detecting_order = [ |
||
5869 | 'ISO-8859-1', |
||
5870 | 'ISO-8859-2', |
||
5871 | 'ISO-8859-3', |
||
5872 | 'ISO-8859-4', |
||
5873 | 'ISO-8859-5', |
||
5874 | 'ISO-8859-6', |
||
5875 | 'ISO-8859-7', |
||
5876 | 'ISO-8859-8', |
||
5877 | 16 | 'ISO-8859-9', |
|
5878 | 'ISO-8859-10', |
||
5879 | 16 | 'ISO-8859-13', |
|
5880 | 16 | 'ISO-8859-14', |
|
5881 | 16 | 'ISO-8859-15', |
|
5882 | 'ISO-8859-16', |
||
5883 | 'WINDOWS-1251', |
||
5884 | 'WINDOWS-1252', |
||
5885 | 'WINDOWS-1254', |
||
5886 | 'CP932', |
||
5887 | 'CP936', |
||
5888 | 'CP950', |
||
5889 | 'CP866', |
||
5890 | 'CP850', |
||
5891 | 'CP51932', |
||
5892 | 'CP50220', |
||
5893 | 'CP50221', |
||
5894 | 'CP50222', |
||
5895 | 'ISO-2022-JP', |
||
5896 | 'ISO-2022-KR', |
||
5897 | 'JIS', |
||
5898 | 'JIS-ms', |
||
5899 | 'EUC-CN', |
||
5900 | 'EUC-JP', |
||
5901 | ]; |
||
5902 | |||
5903 | if (self::$SUPPORT['mbstring'] === true) { |
||
5904 | // info: do not use the symfony polyfill here |
||
5905 | $encoding = \mb_detect_encoding($str, $encoding_detecting_order, true); |
||
5906 | if ($encoding) { |
||
5907 | return $encoding; |
||
5908 | } |
||
5909 | } |
||
5910 | |||
5911 | // |
||
5912 | // 5.) check via "iconv()" |
||
5913 | // |
||
5914 | |||
5915 | if (self::$ENCODINGS === null) { |
||
5916 | self::$ENCODINGS = self::getData('encodings'); |
||
5917 | } |
||
5918 | |||
5919 | 9 | foreach (self::$ENCODINGS as $encoding_tmp) { |
|
5920 | // INFO: //IGNORE but still throw notice |
||
5921 | 9 | /** @noinspection PhpUsageOfSilenceOperatorInspection */ |
|
5922 | 2 | if ((string) @\iconv($encoding_tmp, $encoding_tmp . '//IGNORE', $str) === $str) { |
|
5923 | return $encoding_tmp; |
||
5924 | } |
||
5925 | 9 | } |
|
5926 | 1 | ||
5927 | return false; |
||
5928 | } |
||
13757 |