Conditions | 28 |
Paths | 4001 |
Total Lines | 120 |
Code Lines | 75 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 1 | Features | 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 |
||
249 | public function exportTranslations($module, $excel = true, $onlyLang = [], $untranslated = false, $translate = false) |
||
250 | { |
||
251 | $fullLangPath = $this->getLangPath($module); |
||
252 | |||
253 | $translationFiles = glob($fullLangPath . '/*.yml'); |
||
254 | if ($translationFiles === false) { |
||
255 | $this->message("No yml"); |
||
256 | return; |
||
257 | } |
||
258 | |||
259 | // Collect messages in all lang |
||
260 | $allMessages = []; |
||
261 | $headers = ['key']; |
||
262 | $default = []; |
||
263 | $defaultFile = null; |
||
264 | $defaultLang = substr(i18n::get_locale(), 0, 2); |
||
265 | foreach ($translationFiles as $translationFile) { |
||
266 | $lang = pathinfo($translationFile, PATHINFO_FILENAME); |
||
267 | if ($lang == $defaultLang) { |
||
268 | $defaultFile = $translationFile; |
||
269 | } |
||
270 | if (!empty($onlyLang) && !in_array($lang, $onlyLang)) { |
||
271 | continue; |
||
272 | } |
||
273 | $headers[] = $lang; |
||
274 | $default[] = ''; |
||
275 | } |
||
276 | |||
277 | |||
278 | $masterMessages = []; |
||
279 | if ($untranslated) { |
||
280 | $reader = new YamlReader; |
||
281 | $masterMessages = $reader->read($defaultLang, $defaultFile); |
||
282 | } |
||
283 | |||
284 | $i = 0; |
||
285 | foreach ($translationFiles as $translationFile) { |
||
286 | $lang = pathinfo($translationFile, PATHINFO_FILENAME); |
||
287 | if (!empty($onlyLang) && !in_array($lang, $onlyLang)) { |
||
288 | continue; |
||
289 | } |
||
290 | $reader = new YamlReader; |
||
291 | $messages = $reader->read($lang, $translationFile); |
||
292 | |||
293 | $translator = new OllamaTowerInstruct(); |
||
294 | |||
295 | foreach ($messages as $entityKey => $v) { |
||
296 | if ($untranslated) { |
||
297 | if (isset($masterMessages[$entityKey]) && $masterMessages[$entityKey] != $v) { |
||
298 | continue; |
||
299 | } |
||
300 | } |
||
301 | |||
302 | if (!isset($allMessages[$entityKey])) { |
||
303 | $allMessages[$entityKey] = $default; |
||
304 | } |
||
305 | // Plurals can be arrays and need to be converted |
||
306 | if (is_array($v)) { |
||
307 | $v = json_encode($v); |
||
308 | } |
||
309 | |||
310 | // Attempt auto translation / 200 |
||
311 | if ($translate && count($allMessages) < 200) { |
||
312 | $v = $translator->translate($v, $lang, $defaultLang); |
||
313 | } |
||
314 | |||
315 | $allMessages[$entityKey][$i] = $v; |
||
316 | } |
||
317 | $i++; |
||
318 | } |
||
319 | // don't sort this will be mess up git when merging later |
||
320 | // ksort($allMessages); |
||
321 | if ($this->debug) { |
||
322 | Debug::show($allMessages); |
||
323 | } |
||
324 | |||
325 | // Write them to a file |
||
326 | if ($excel && class_exists(ExcelImportExport::class)) { |
||
327 | $ext = 'xlsx'; |
||
328 | $destinationFilename = str_replace('/lang', '/lang.' . $ext, $fullLangPath); |
||
329 | if ($this->debug) { |
||
330 | Debug::show("Debug mode enabled : no output will be sent to $destinationFilename"); |
||
331 | return; |
||
332 | } |
||
333 | if (is_file($destinationFilename)) { |
||
334 | unlink($destinationFilename); |
||
335 | } |
||
336 | // First row contains headers |
||
337 | $data = [$headers]; |
||
338 | // Add a row per lang |
||
339 | foreach ($allMessages as $key => $translations) { |
||
340 | array_unshift($translations, $key); |
||
341 | $data[] = $translations; |
||
342 | } |
||
343 | ExcelImportExport::arrayToFile($data, $destinationFilename); |
||
344 | } else { |
||
345 | $ext = 'csv'; |
||
346 | $destinationFilename = str_replace('/lang', '/lang.' . $ext, $fullLangPath); |
||
347 | if ($this->debug) { |
||
348 | Debug::show("Debug mode enabled : no output will be sent to $destinationFilename"); |
||
349 | return; |
||
350 | } |
||
351 | if (is_file($destinationFilename)) { |
||
352 | unlink($destinationFilename); |
||
353 | } |
||
354 | $fp = fopen($destinationFilename, 'w'); |
||
355 | if ($fp === false) { |
||
356 | throw new Exception("Failed to open stream"); |
||
357 | } |
||
358 | // UTF 8 fix |
||
359 | fprintf($fp, "\xEF\xBB\xBF"); |
||
360 | fputcsv($fp, $headers); |
||
361 | foreach ($allMessages as $key => $translations) { |
||
362 | array_unshift($translations, $key); |
||
363 | fputcsv($fp, $translations); |
||
364 | } |
||
365 | fclose($fp); |
||
366 | } |
||
367 | |||
368 | $this->message("Translations written to $destinationFilename"); |
||
369 | } |
||
376 |
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