CheckTranslations::handle()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 11
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 15
rs 9.9
1
<?php
2
3
namespace App\Console\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Facades\File;
7
use Illuminate\Support\Arr;
8
9
class CheckTranslations extends Command
10
{
11
    protected $signature = 'translation-check {language}';
12
    protected $description = 'Check if translation files are up to date';
13
14
    public function handle()
15
    {
16
        $defaultLanguage = 'en';
17
        $languageCode = $this->argument('language');
18
        $defaultTranslations = include base_path("resources/lang/{$defaultLanguage}/messages.php");
19
        $translations = include base_path("resources/lang/{$languageCode}/messages.php");
20
21
        $missingKeys = $this->getMissingKeys($defaultTranslations, $translations);
22
23
        if (count($missingKeys) > 0) {
24
            $this->output->error("{$languageCode} translation file is out of date!");
25
            $this->output->table(['Missing Keys'], $missingKeys);
26
            $this->output->error('Number of missing keys: ' . count($missingKeys));
27
        } else {
28
            $this->output->success("{$languageCode} translation file is up to date!");
29
        }
30
    }
31
32
    protected function getMissingKeys(array $default, array $translations)
33
    {
34
        $missingKeys = [];
35
36
        foreach (Arr::dot($default) as $key => $value) {
37
            if (!Arr::has($translations, $key)) {
38
                $missingKeys[] = [$key];
39
            }
40
        }
41
42
        return $missingKeys;
43
    }
44
}
45