Total Complexity | 40 |
Total Lines | 214 |
Duplicated Lines | 0 % |
Changes | 5 | ||
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 |
||
123 | } |
||
124 | |||
125 | |||
126 | // options['decimals'] = int |
||
127 | // options['abbrev'] = mixed: key needs to be set |
||
128 | public static function when($event, $options = []) |
||
129 | { |
||
130 | try { |
||
131 | $amount_of_days = DatoTempo::days_diff(new \DateTime($event), new \DateTime()); |
||
132 | } catch (\Exception $e) { |
||
133 | return __FUNCTION__ . ': error'; |
||
134 | } |
||
135 | |||
136 | if ($amount_of_days === -1) { |
||
137 | return $this->l('DATETIME_RANGE_YESTERDAY'); |
||
|
|||
138 | } elseif ($amount_of_days === 0) { |
||
139 | return $this->l('DATETIME_RANGE_TODAY'); |
||
140 | } elseif ($amount_of_days === 1) { |
||
141 | return $this->l('DATETIME_RANGE_TOMORROW'); |
||
142 | } |
||
143 | |||
144 | |||
145 | $datetime_parts = [ |
||
146 | 'y' => 'DATETIME_UNIT_YEAR', |
||
147 | 'm' => 'DATETIME_UNIT_MONTH', |
||
148 | 'w' => 'DATETIME_UNIT_WEEK', |
||
149 | 'd' => 'DATETIME_UNIT_DAY', |
||
150 | 'h' => 'DATETIME_UNIT_HOUR', |
||
151 | 'i' => 'DATETIME_UNIT_MINUTE', |
||
152 | 's' => 'DATETIME_UNIT_SECOND' |
||
153 | ]; |
||
154 | |||
155 | $date_diff = DatoTempo::days_diff_in_parts(abs($amount_of_days)); |
||
156 | $ordering = []; |
||
157 | foreach ($datetime_parts as $unit => $label) { |
||
158 | if (!isset($date_diff[$unit])) { |
||
159 | continue; |
||
160 | } |
||
161 | |||
162 | $qty = (int)$date_diff[$unit]; |
||
163 | |||
164 | if ($qty === 0) { |
||
165 | continue; |
||
166 | } |
||
167 | |||
168 | if (isset($options['abbrev'])) { |
||
169 | $label .= '_ABBREV'; |
||
170 | } elseif ($qty > 1) { |
||
171 | $label .= '_PLURAL'; |
||
172 | } |
||
173 | |||
174 | $ordering[$unit] = $qty . ' ' . L($label) . '.'; |
||
175 | } |
||
176 | $ret = (isset($amount_of_days) && $amount_of_days >= 0) ? L('DATETIME_RANGE_PREFIX_FUTURE') : L('DATETIME_RANGE_PREFIX_PAST'); |
||
177 | $ret .= ' ' . implode(' & ', array_slice($ordering, 0, 2)); |
||
178 | |||
179 | return $ret; |
||
180 | } |
||
181 | |||
182 | public static function time($time_string, $short = true) |
||
183 | { |
||
184 | if ($short === true) { |
||
185 | $time_string = substr($time_string, 0, 5); |
||
186 | } |
||
187 | return $time_string; |
||
188 | } |
||
189 | |||
190 | public static function human_date($date_string, $short = true) |
||
208 | } |
||
209 | } |
||
210 | |||
211 | public static function human_month($date_string) |
||
212 | { |
||
213 | return $this->l('DATETIME_CALENDAR_MONTH_' . Dato::format($date_string, 'm')); |
||
214 | } |
||
215 | |||
216 | public static function human_day($date_string) |
||
219 | } |
||
220 | |||
221 | public static function human_seconds($seconds) |
||
231 |