Total Complexity | 54 |
Total Lines | 280 |
Duplicated Lines | 0 % |
Changes | 11 | ||
Bugs | 3 | 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() |
||
104 | } |
||
105 | |||
106 | |||
107 | |||
108 | |||
109 | |||
110 | // options['decimals'] = int |
||
111 | // options['abbrev'] = mixed: key needs to be set |
||
112 | public function when($event, $options = []) |
||
167 | } |
||
168 | |||
169 | public function time($time_string, $short = true) |
||
170 | { |
||
171 | if ($short === true) { |
||
172 | $time_string = substr($time_string, 0, 5); |
||
173 | } |
||
174 | return $time_string; |
||
175 | } |
||
176 | |||
177 | public function date($date_string, $short = true) |
||
178 | { |
||
179 | if ($date_string === '0000-00-00' || empty($date_string)) { |
||
180 | return $this->l('MODEL_common_VALUE_EMPTY'); |
||
181 | } |
||
182 | |||
183 | if (preg_match('/^[0-9]{4}$/', $date_string) === 1) { |
||
184 | return intval($date_string); |
||
185 | } |
||
186 | |||
187 | list($year, $month, $day) = explode('-', $date_string); |
||
188 | |||
189 | $ret = intval($day) . ' ' . $this->l("DATETIME_CALENDAR_MONTH_$month"); |
||
190 | |||
191 | if ($short === true && Dato::format(null, 'Y') === $year) { |
||
192 | return $ret; |
||
193 | } else { |
||
194 | return "$ret $year"; |
||
195 | } |
||
196 | } |
||
197 | |||
198 | public function month($date_string) |
||
199 | { |
||
200 | return $this->l('DATETIME_CALENDAR_MONTH_' . Dato::format($date_string, 'm')); |
||
201 | } |
||
202 | |||
203 | public function day($date_string) |
||
206 | } |
||
207 | |||
208 | public function seconds($seconds) |
||
216 | } |
||
217 | |||
218 | public function init() { |
||
219 | if ($this->isInitialized()) { |
||
220 | throw new \BadMethodCallException('This object from class ' . __CLASS__ . ' is already initialized. It is not possible to init one object twice!'); |
||
221 | } |
||
222 | |||
223 | $this->isInitialized = true; |
||
224 | |||
225 | $this->userLangs = $this->getUserLangs(); |
||
226 | |||
227 | // search for language file |
||
228 | $this->appliedLang = NULL; |
||
229 | foreach ($this->userLangs as $priority => $langcode) { |
||
230 | $this->langFilePath = $this->getConfigFilename($langcode); |
||
231 | if (file_exists($this->langFilePath)) { |
||
232 | $this->appliedLang = $langcode; |
||
233 | break; |
||
234 | } |
||
235 | } |
||
236 | if ($this->appliedLang == NULL) { |
||
237 | throw new \RuntimeException('No language file was found.'); |
||
238 | } |
||
239 | |||
240 | // search for cache file |
||
241 | $this->cacheFilePath = $this->cachePath . '/php_i18n_' . md5_file(__FILE__) . '_' . $this->prefix . '_' . $this->appliedLang . '.cache.php'; |
||
242 | |||
243 | // whether we need to create a new cache file |
||
244 | $outdated = !file_exists($this->cacheFilePath) || |
||
245 | filemtime($this->cacheFilePath) < filemtime($this->langFilePath) || // the language config was updated |
||
246 | ($this->mergeFallback && filemtime($this->cacheFilePath) < filemtime($this->getConfigFilename($this->fallbackLang))); // the fallback language config was updated |
||
247 | |||
248 | if ($outdated) { |
||
249 | $config = $this->load($this->langFilePath); |
||
250 | if ($this->mergeFallback) |
||
251 | $config = array_replace_recursive($this->load($this->getConfigFilename($this->fallbackLang)), $config); |
||
252 | |||
253 | $compiled = "<?php class " . $this->prefix . " {\n" |
||
254 | . $this->compile($config) |
||
255 | . 'public static function __callStatic($string, $args) {' . "\n" |
||
256 | . ' return vsprintf(constant("self::" . $string), $args);' |
||
257 | . "\n}\n}\n" |
||
258 | . $this->compileFunction(); |
||
259 | |||
260 | if( ! is_dir($this->cachePath)) |
||
261 | mkdir($this->cachePath, 0755, true); |
||
262 | |||
263 | if (file_put_contents($this->cacheFilePath, $compiled) === FALSE) { |
||
264 | throw new \Exception("Could not write cache file to path '" . $this->cacheFilePath . "'. Is it writable?"); |
||
265 | } |
||
266 | try{ |
||
267 | chmod($this->cacheFilePath, 0755); |
||
268 | } |
||
269 | catch(\Throwable $t){ |
||
270 | throw new \Exception("Could chmod cache file '" . $this->cacheFilePath . "'"); |
||
271 | } |
||
272 | |||
273 | |||
274 | } |
||
275 | |||
276 | require_once $this->cacheFilePath; |
||
277 | } |
||
278 | |||
279 | public function compileFunction() |
||
288 | } |
||
289 | |||
290 | public function l($message, $context = []): string |
||
296 | } |
||
297 | } |
||
298 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths