Total Complexity | 40 |
Total Lines | 212 |
Duplicated Lines | 0 % |
Changes | 6 | ||
Bugs | 0 | Features | 1 |
Complex classes like Lezer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Lezer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class Lezer extends \i18n |
||
17 | { |
||
18 | private $detected_language_files = []; |
||
19 | private $detected_language_env = []; |
||
20 | |||
21 | // protected $basePath = 'locale'; |
||
22 | // protected $filePath = 'locale/{LANGUAGE}/user_interface.ini'; // uses gettext hierarchy |
||
23 | // protected $cachePath = 'locale/cache/'; |
||
24 | // protected $fallbackLang = 'fra'; // uses ISO-639-3 |
||
25 | protected $currentLang = null; |
||
26 | |||
27 | public function availableLanguage() |
||
28 | { |
||
29 | $the_one_language = current(array_intersect($this->detectLanguageFiles(), $this->detectLanguageEnv())); |
||
30 | |||
31 | if ($the_one_language) { |
||
32 | $this->setForcedLang($the_one_language); |
||
33 | } |
||
34 | |||
35 | return $the_one_language; |
||
36 | } |
||
37 | |||
38 | private function detectLanguageFiles() |
||
39 | { |
||
40 | $files = FileSystem::preg_scandir(dirname($this->filePath), '/.json$/'); |
||
41 | if (empty($files)) { |
||
42 | return []; |
||
43 | } |
||
44 | |||
45 | $files = implode('', $files); |
||
46 | $res = preg_match_all('/([a-z]{3})\.json/', $files, $m); |
||
47 | if ($res) { // false or 0 is none found |
||
48 | $this->detected_language_files = $m[1]; |
||
49 | } |
||
50 | return $this->detected_language_files; |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * getUserLangs() |
||
55 | * Returns the user languages |
||
56 | * Normally it returns an array like this: |
||
57 | * 1. Forced language |
||
58 | * 2. Language in $_GET['lang'] |
||
59 | * 3. Language in $_SESSION['lang'] |
||
60 | * 4. COOKIE |
||
61 | * 5. Fallback language |
||
62 | * Note: duplicate values are deleted. |
||
63 | * |
||
64 | * @return array with the user languages sorted by priority. |
||
65 | */ |
||
66 | private function detectLanguageEnv() |
||
67 | { |
||
68 | $userLangs = array(); |
||
69 | |||
70 | // 1. forced language |
||
71 | if ($this->forcedLang != null) { |
||
72 | $userLangs['forced'] = $this->forcedLang; |
||
73 | } |
||
74 | |||
75 | // 2. GET parameter 'lang' |
||
76 | if (isset($_GET['lang']) && is_string($_GET['lang'])) { |
||
77 | $userLangs['get'] = $_GET['lang']; |
||
78 | } |
||
79 | |||
80 | // 3. SESSION parameter 'lang' |
||
81 | if (isset($_SESSION['lang']) && is_string($_SESSION['lang'])) { |
||
82 | $userLangs['session'] = $_SESSION['lang']; |
||
83 | } |
||
84 | |||
85 | // 4. COOKIES |
||
86 | if (isset($_COOKIE['lang']) && is_string($_COOKIE['lang'])) { |
||
87 | $userLangs['cookie'] = $_COOKIE['lang']; |
||
88 | } |
||
89 | |||
90 | // Lowest priority: fallback |
||
91 | $userLangs['fallback'] = $this->fallbackLang; |
||
92 | // remove duplicate elements |
||
93 | $userLangs = array_unique($userLangs); |
||
94 | |||
95 | // remove illegal userLangs |
||
96 | foreach ($userLangs as $key => $value) { |
||
97 | // only allow a-z, A-Z and 0-9 and _ and - |
||
98 | if (preg_match('/^[a-zA-Z0-9_-]*$/', $value) === 1) { |
||
99 | $this->detected_language_env[$key] = $value; |
||
100 | } |
||
101 | } |
||
102 | |||
103 | return $this->detected_language_env; |
||
104 | } |
||
105 | |||
106 | |||
107 | public function compileFunction() |
||
116 | } |
||
117 | |||
118 | public function l($message, $context = []): string |
||
119 | { |
||
120 | return call_user_func($this->prefix, $message, $context); |
||
121 | } |
||
122 | |||
123 | |||
124 | // options['decimals'] = int |
||
125 | // options['abbrev'] = mixed: key needs to be set |
||
126 | public function when($event, $options = []) |
||
179 | } |
||
180 | |||
181 | public function time($time_string, $short = true) |
||
187 | } |
||
188 | |||
189 | public function date($date_string, $short = true) |
||
190 | { |
||
191 | if ($date_string === '0000-00-00' || empty($date_string)) { |
||
192 | return $this->l('MODEL_common_VALUE_EMPTY'); |
||
193 | } |
||
194 | |||
195 | if (preg_match('/^[0-9]{4}$/', $date_string) === 1) { |
||
196 | return intval($date_string); |
||
197 | } |
||
198 | |||
199 | list($year, $month, $day) = explode('-', $date_string); |
||
200 | |||
201 | $ret = intval($day) . ' ' . $this->l("DATETIME_CALENDAR_MONTH_$month"); |
||
202 | |||
203 | if ($short === true && Dato::format(null, 'Y') === $year) { |
||
204 | return $ret; |
||
205 | } else { |
||
206 | return "$ret $year"; |
||
207 | } |
||
208 | } |
||
209 | |||
210 | public function month($date_string) |
||
211 | { |
||
212 | return $this->l('DATETIME_CALENDAR_MONTH_' . Dato::format($date_string, 'm')); |
||
213 | } |
||
214 | |||
215 | public function day($date_string) |
||
218 | } |
||
219 | |||
220 | public function seconds($seconds) |
||
228 | } |
||
229 | } |
||
230 |