Conditions | 16 |
Paths | 13 |
Total Lines | 147 |
Code Lines | 107 |
Lines | 0 |
Ratio | 0 % |
Tests | 32 |
CRAP Score | 16.052 |
Changes | 8 | ||
Bugs | 4 | 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 |
||
4809 | public static function normalize_encoding($encoding, $fallback = '') |
||
4810 | { |
||
4811 | /** |
||
4812 | * @psalm-suppress ImpureStaticVariable |
||
4813 | * |
||
4814 | * @var array<string,string> |
||
4815 | */ |
||
4816 | 339 | static $STATIC_NORMALIZE_ENCODING_CACHE = []; |
|
4817 | |||
4818 | // init |
||
4819 | 339 | $encoding = (string) $encoding; |
|
4820 | |||
4821 | 339 | if (!$encoding) { |
|
4822 | 290 | return $fallback; |
|
4823 | } |
||
4824 | |||
4825 | if ( |
||
4826 | 53 | $encoding === 'UTF-8' |
|
4827 | || |
||
4828 | 53 | $encoding === 'UTF8' |
|
4829 | ) { |
||
4830 | 29 | return 'UTF-8'; |
|
4831 | } |
||
4832 | |||
4833 | if ( |
||
4834 | 44 | $encoding === '8BIT' |
|
4835 | || |
||
4836 | 44 | $encoding === 'BINARY' |
|
4837 | ) { |
||
4838 | return 'CP850'; |
||
4839 | } |
||
4840 | |||
4841 | if ( |
||
4842 | 44 | $encoding === 'HTML' |
|
4843 | || |
||
4844 | 44 | $encoding === 'HTML-ENTITIES' |
|
4845 | ) { |
||
4846 | 2 | return 'HTML-ENTITIES'; |
|
4847 | } |
||
4848 | |||
4849 | if ( |
||
4850 | 44 | $encoding === 'ISO' |
|
4851 | || |
||
4852 | 44 | $encoding === 'ISO-8859-1' |
|
4853 | ) { |
||
4854 | 41 | return 'ISO-8859-1'; |
|
4855 | } |
||
4856 | |||
4857 | if ( |
||
4858 | 11 | $encoding === '1' // only a fallback, for non "strict_types" usage ... |
|
4859 | || |
||
4860 | 11 | $encoding === '0' // only a fallback, for non "strict_types" usage ... |
|
4861 | ) { |
||
4862 | return $fallback; |
||
4863 | } |
||
4864 | |||
4865 | 11 | if (isset($STATIC_NORMALIZE_ENCODING_CACHE[$encoding])) { |
|
4866 | 8 | return $STATIC_NORMALIZE_ENCODING_CACHE[$encoding]; |
|
4867 | } |
||
4868 | |||
4869 | 5 | if (self::$ENCODINGS === null) { |
|
4870 | 1 | self::$ENCODINGS = self::getData('encodings'); |
|
4871 | } |
||
4872 | |||
4873 | 5 | if (\in_array($encoding, self::$ENCODINGS, true)) { |
|
4874 | 3 | $STATIC_NORMALIZE_ENCODING_CACHE[$encoding] = $encoding; |
|
4875 | |||
4876 | 3 | return $encoding; |
|
4877 | } |
||
4878 | |||
4879 | 4 | $encoding_original = $encoding; |
|
4880 | 4 | $encoding = \strtoupper($encoding); |
|
4881 | 4 | $encoding_upper_helper = (string) \preg_replace('/[^a-zA-Z0-9]/u', '', $encoding); |
|
4882 | |||
4883 | $equivalences = [ |
||
4884 | 4 | 'ISO8859' => 'ISO-8859-1', |
|
4885 | 'ISO88591' => 'ISO-8859-1', |
||
4886 | 'ISO' => 'ISO-8859-1', |
||
4887 | 'LATIN' => 'ISO-8859-1', |
||
4888 | 'LATIN1' => 'ISO-8859-1', // Western European |
||
4889 | 'ISO88592' => 'ISO-8859-2', |
||
4890 | 'LATIN2' => 'ISO-8859-2', // Central European |
||
4891 | 'ISO88593' => 'ISO-8859-3', |
||
4892 | 'LATIN3' => 'ISO-8859-3', // Southern European |
||
4893 | 'ISO88594' => 'ISO-8859-4', |
||
4894 | 'LATIN4' => 'ISO-8859-4', // Northern European |
||
4895 | 'ISO88595' => 'ISO-8859-5', |
||
4896 | 'ISO88596' => 'ISO-8859-6', // Greek |
||
4897 | 'ISO88597' => 'ISO-8859-7', |
||
4898 | 'ISO88598' => 'ISO-8859-8', // Hebrew |
||
4899 | 'ISO88599' => 'ISO-8859-9', |
||
4900 | 'LATIN5' => 'ISO-8859-9', // Turkish |
||
4901 | 'ISO885911' => 'ISO-8859-11', |
||
4902 | 'TIS620' => 'ISO-8859-11', // Thai |
||
4903 | 'ISO885910' => 'ISO-8859-10', |
||
4904 | 'LATIN6' => 'ISO-8859-10', // Nordic |
||
4905 | 'ISO885913' => 'ISO-8859-13', |
||
4906 | 'LATIN7' => 'ISO-8859-13', // Baltic |
||
4907 | 'ISO885914' => 'ISO-8859-14', |
||
4908 | 'LATIN8' => 'ISO-8859-14', // Celtic |
||
4909 | 'ISO885915' => 'ISO-8859-15', |
||
4910 | 'LATIN9' => 'ISO-8859-15', // Western European (with some extra chars e.g. €) |
||
4911 | 'ISO885916' => 'ISO-8859-16', |
||
4912 | 'LATIN10' => 'ISO-8859-16', // Southeast European |
||
4913 | 'CP1250' => 'WINDOWS-1250', |
||
4914 | 'WIN1250' => 'WINDOWS-1250', |
||
4915 | 'WINDOWS1250' => 'WINDOWS-1250', |
||
4916 | 'CP1251' => 'WINDOWS-1251', |
||
4917 | 'WIN1251' => 'WINDOWS-1251', |
||
4918 | 'WINDOWS1251' => 'WINDOWS-1251', |
||
4919 | 'CP1252' => 'WINDOWS-1252', |
||
4920 | 'WIN1252' => 'WINDOWS-1252', |
||
4921 | 'WINDOWS1252' => 'WINDOWS-1252', |
||
4922 | 'CP1253' => 'WINDOWS-1253', |
||
4923 | 'WIN1253' => 'WINDOWS-1253', |
||
4924 | 'WINDOWS1253' => 'WINDOWS-1253', |
||
4925 | 'CP1254' => 'WINDOWS-1254', |
||
4926 | 'WIN1254' => 'WINDOWS-1254', |
||
4927 | 'WINDOWS1254' => 'WINDOWS-1254', |
||
4928 | 'CP1255' => 'WINDOWS-1255', |
||
4929 | 'WIN1255' => 'WINDOWS-1255', |
||
4930 | 'WINDOWS1255' => 'WINDOWS-1255', |
||
4931 | 'CP1256' => 'WINDOWS-1256', |
||
4932 | 'WIN1256' => 'WINDOWS-1256', |
||
4933 | 'WINDOWS1256' => 'WINDOWS-1256', |
||
4934 | 'CP1257' => 'WINDOWS-1257', |
||
4935 | 'WIN1257' => 'WINDOWS-1257', |
||
4936 | 'WINDOWS1257' => 'WINDOWS-1257', |
||
4937 | 'CP1258' => 'WINDOWS-1258', |
||
4938 | 'WIN1258' => 'WINDOWS-1258', |
||
4939 | 'WINDOWS1258' => 'WINDOWS-1258', |
||
4940 | 'UTF16' => 'UTF-16', |
||
4941 | 'UTF32' => 'UTF-32', |
||
4942 | 'UTF8' => 'UTF-8', |
||
4943 | 'UTF' => 'UTF-8', |
||
4944 | 'UTF7' => 'UTF-7', |
||
4945 | '8BIT' => 'CP850', |
||
4946 | 'BINARY' => 'CP850', |
||
4947 | ]; |
||
4948 | |||
4949 | 4 | if (!empty($equivalences[$encoding_upper_helper])) { |
|
4950 | 3 | $encoding = $equivalences[$encoding_upper_helper]; |
|
4951 | } |
||
4952 | |||
4953 | 4 | $STATIC_NORMALIZE_ENCODING_CACHE[$encoding_original] = $encoding; |
|
4954 | |||
4955 | 4 | return $encoding; |
|
4956 | } |
||
14812 |