Test Failed
Push — master ( c9c10d...19d548 )
by Nicolas
05:23
created

UnusedCommand   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 73
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 5 1
A __construct() 0 4 1
A reportUnused() 0 16 4
1
<?php
2
3
namespace NicolasBeauvais\Transcribe\Commands;
4
5
use Illuminate\Console\Command;
6
use NicolasBeauvais\Transcribe\Manager;
7
8
class UnusedCommand extends Command
9
{
10
    /**
11
     * The name and signature of the console command.
12
     *
13
     * @var string
14
     */
15
    protected $signature = 'transcribe:unused';
16
17
    /**
18
     * The description of the console command.
19
     *
20
     * @var string
21
     */
22
    protected $description = 'Look for translations in views and update missing key in language files.';
23
24
    /**
25
     * The Languages manager instance.
26
     *
27
     * @var \NicolasBeauvais\Transcribe\Manager
28
     */
29
    private $manager;
30
31
    /**
32
     * Command constructor.
33
     *
34
     * @param \NicolasBeauvais\Transcribe\Manager $manager
35
     *
36
     * @return void
37
     */
38
    public function __construct(Manager $manager)
39
    {
40
        parent::__construct();
41
        $this->manager = $manager;
42
    }
43
44
    /**
45
     * Execute the console command.
46
     *
47
     * @return mixed
48
     */
49
    public function handle()
50
    {
51
        $translationFiles = $this->manager->files();
52
        $this->reportUnused($translationFiles);
53
        $this->info('Done!');
54
    }
55
56
    /**
57
     * Report unused keys in translation files.
58
     *
59
     * @param $translationFiles
60
     *
61
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
62
     *
63
     * @return void
64
     */
65
    private function reportUnused($translationFiles)
66
    {
67
        $this->info('Finding unused keys...');
68
69
        // An array of all translation keys as found in project files.
70
        $allKeysInFiles = $this->manager->collectFromFiles();
71
72
        foreach ($translationFiles as $fileName => $languages) {
73
            $languageKey = config('app.fallback_locale');
74
            $path = $languages[$languageKey];
75
76
            $fileContent = $this->manager->getFileContent($path);
77
            if (isset($allKeysInFiles[$fileName])) {
78
                $missingKeys = array_diff(array_keys(array_dot($fileContent)), $allKeysInFiles[$fileName]);
79
                foreach ($missingKeys as $i => $missingKey) {
80
                    $this->output->writeln("\"<fg=red>{$fileName}.{$missingKey}</>\" never used.");
81
                }
82
            }
83
        }
84
    }
85
}
86