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