Total Complexity | 42 |
Total Lines | 222 |
Duplicated Lines | 0 % |
Changes | 4 | ||
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 |
||
15 | class Lezer extends \i18n |
||
16 | { |
||
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 one_language() |
||
38 | } |
||
39 | |||
40 | public function detect_language_files() |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * getUserLangs() |
||
57 | * Returns the user languages |
||
58 | * Normally it returns an array like this: |
||
59 | * 1. Forced language |
||
60 | * 2. Language in $_GET['lang'] |
||
61 | * 3. Language in $_SESSION['lang'] |
||
62 | * 4. COOKIE |
||
63 | * 5. Fallback language |
||
64 | * Note: duplicate values are deleted. |
||
65 | * |
||
66 | * @return array with the user languages sorted by priority. |
||
67 | */ |
||
68 | public function detect_language_env() |
||
69 | { |
||
70 | $userLangs = array(); |
||
71 | |||
72 | // 1. forced language |
||
73 | if ($this->forcedLang != null) { |
||
74 | $userLangs['forced'] = $this->forcedLang; |
||
75 | } |
||
76 | |||
77 | // 2. GET parameter 'lang' |
||
78 | if (isset($_GET['lang']) && is_string($_GET['lang'])) { |
||
79 | $userLangs['get'] = $_GET['lang']; |
||
80 | } |
||
81 | |||
82 | // 3. SESSION parameter 'lang' |
||
83 | if (isset($_SESSION['lang']) && is_string($_SESSION['lang'])) { |
||
84 | $userLangs['session'] = $_SESSION['lang']; |
||
85 | } |
||
86 | |||
87 | // 4. COOKIES |
||
88 | if (isset($_COOKIE['lang']) && is_string($_COOKIE['lang'])) { |
||
89 | $userLangs['cookie'] = $_COOKIE['lang']; |
||
90 | } |
||
91 | |||
92 | // Lowest priority: fallback |
||
93 | $userLangs['fallback'] = $this->fallbackLang; |
||
94 | // remove duplicate elements |
||
95 | $userLangs = array_unique($userLangs); |
||
96 | |||
97 | // remove illegal userLangs |
||
98 | foreach ($userLangs as $key => $value) { |
||
99 | // only allow a-z, A-Z and 0-9 and _ and - |
||
100 | if (preg_match('/^[a-zA-Z0-9_-]*$/', $value) === 1) { |
||
101 | $this->detected_language_env[$key] = $value; |
||
102 | } |
||
103 | } |
||
104 | |||
105 | return $this->detected_language_env; |
||
106 | } |
||
107 | |||
108 | |||
109 | public function compileFunction() |
||
118 | } |
||
119 | |||
120 | public function l($message, $context=[]) : string |
||
121 | { |
||
122 | return call_user_func($this->prefix, $message, $context); |
||
123 | } |
||
124 | |||
125 | public static function model_type_to_label($form_model) |
||
126 | { |
||
127 | return $this->l(sprintf('MODEL_%s_INSTANCE', get_class($form_model)::model_type())); |
||
|
|||
128 | } |
||
129 | public static function field_name_to_label($form_model, $field_name) |
||
130 | { |
||
131 | return $this->l(sprintf('MODEL_%s_FIELD_%s', (get_class($form_model))::model_type(), $field_name)); |
||
132 | } |
||
133 | |||
134 | // options['decimals'] = int |
||
135 | // options['abbrev'] = mixed: key needs to be set |
||
136 | public static function when($event, $options = []) |
||
188 | } |
||
189 | |||
190 | public static function time($time_string, $short = true) |
||
191 | { |
||
192 | if ($short === true) { |
||
193 | $time_string = substr($time_string, 0, 5); |
||
194 | } |
||
195 | return $time_string; |
||
196 | } |
||
197 | |||
198 | public static function human_date($date_string, $short = true) |
||
216 | } |
||
217 | } |
||
218 | |||
219 | public static function human_month($date_string) |
||
220 | { |
||
221 | return $this->l('DATETIME_CALENDAR_MONTH_' . Dato::format($date_string, 'm')); |
||
222 | } |
||
223 | |||
224 | public static function human_day($date_string) |
||
227 | } |
||
228 | |||
229 | public static function human_seconds($seconds) |
||
230 | { |
||
231 | $hours = floor($seconds / 3600); |
||
232 | $mins = floor(($seconds - $hours * 3600) / 60); |
||
239 |