| Conditions | 12 |
| Paths | 160 |
| Total Lines | 58 |
| Code Lines | 36 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 94 | private function setLang($hardSetLang = 0) |
||
| 95 | { |
||
| 96 | // $langConverted will contain candidates for the language setting in the order |
||
| 97 | // of prefference |
||
| 98 | $langConverted = []; |
||
| 99 | if ($hardSetLang !== 0) { |
||
| 100 | $langConverted[] = $hardSetLang; |
||
| 101 | } |
||
| 102 | if (!empty($_REQUEST['lang'])) { |
||
| 103 | $recoverLang = filter_input(INPUT_GET, 'lang', FILTER_SANITIZE_STRING) ?? filter_input(INPUT_POST, 'lang', FILTER_SANITIZE_STRING); |
||
| 104 | $langConverted[] = $recoverLang; |
||
| 105 | } |
||
| 106 | \core\CAT::sessionStart(); |
||
| 107 | if (!empty($_SESSION['language'])) { |
||
| 108 | $langConverted[] = $_SESSION['language']; |
||
| 109 | } |
||
| 110 | if (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { |
||
| 111 | $langs = explode(",", filter_input(INPUT_SERVER, "HTTP_ACCEPT_LANGUAGE", FILTER_SANITIZE_STRING)); |
||
| 112 | foreach ($langs as $lang) { |
||
| 113 | $result = []; |
||
| 114 | preg_match("/(.*);+.*/", $lang, $result); |
||
| 115 | $langConverted[] = (empty($result[1]) ? $lang : $result[1]); |
||
| 116 | } |
||
| 117 | } |
||
| 118 | $langIndex = \config\Master::APPEARANCE['defaultlocale']; |
||
| 119 | $theLocale = \config\Master::LANGUAGES[$langIndex]['locale']; |
||
| 120 | // always add configured default language as the last resort |
||
| 121 | $langConverted[] = $langIndex; |
||
| 122 | setlocale(LC_ALL, 0); |
||
| 123 | foreach ($langConverted as $tryLang) { |
||
| 124 | // madness! setlocale is completely unflexible. If $tryLang is "en" |
||
| 125 | // it will fail, because it only knows en_US, en_GB a.s.o. |
||
| 126 | // we need to map stuff manually |
||
| 127 | $localeTmp = FALSE; |
||
| 128 | |||
| 129 | // check if this language is supported by the CAT config |
||
| 130 | foreach (\config\Master::LANGUAGES as $language => $value) { |
||
| 131 | if (preg_match("/^" . $language . ".*/", $tryLang)) { |
||
| 132 | $localeTmp = $value['locale']; |
||
| 133 | $langIndex = $language; // ??? |
||
| 134 | break; |
||
| 135 | } |
||
| 136 | } |
||
| 137 | // make sure that the selected locale is actually instlled on this system |
||
| 138 | // normally this should not be needed, but it is a safeguard agains misconfiguration |
||
| 139 | if ($localeTmp) { |
||
| 140 | if (setlocale(LC_ALL, $localeTmp)) { |
||
| 141 | $theLocale = $localeTmp; |
||
| 142 | break; |
||
| 143 | } |
||
| 144 | } |
||
| 145 | } |
||
| 146 | putenv("LC_ALL=" . $theLocale); |
||
| 147 | $_SESSION['language'] = $langIndex; |
||
| 148 | $loggerInstance = new \core\common\Logging(); |
||
| 149 | $loggerInstance->debug(4, "selected lang:$langIndex:$theLocale\n"); |
||
| 150 | $loggerInstance->debug(4, print_r($langConverted, true)); |
||
| 151 | return([$langIndex, $theLocale]); |
||
| 152 | } |
||
| 202 | } |