Completed
Push — master ( fb36fd...a359f8 )
by Nicolas
05:26
created

UnusedCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 61
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 5 1
B reportUnused() 0 12 5
A __construct() 0 4 1
1
<?php
2
3
namespace NicolasBeauvais\Transcribe\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Arr;
7
use NicolasBeauvais\Transcribe\Manager;
8
9
class UnusedCommand extends Command
10
{
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'langman:unused';
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
     * The Languages manager instance.
25
     *
26
     * @var \Themsaid\LangMan\Manager
0 ignored issues
show
Bug introduced by
The type Themsaid\LangMan\Manager was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
     */
28
    private $manager;
29
    /**
30
     * Command constructor.
31
     *
32
     * @param \Themsaid\LangMan\Manager $manager
33
     * @return void
34
     */
35
    public function __construct(Manager $manager)
36
    {
37
        parent::__construct();
38
        $this->manager = $manager;
0 ignored issues
show
Documentation Bug introduced by
It seems like $manager of type NicolasBeauvais\Transcribe\Manager is incompatible with the declared type Themsaid\LangMan\Manager of property $manager.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
39
    }
40
    /**
41
     * Execute the console command.
42
     *
43
     * @return mixed
44
     */
45
    public function handle()
46
    {
47
        $translationFiles = $this->manager->files();
48
        $this->reportUnused($translationFiles);
49
        $this->info('Done!');
50
    }
51
    /**
52
     * Report unused keys in translation files
53
     *
54
     * @param $translationFiles
55
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
56
     * @return void
57
     */
58
    private function reportUnused($translationFiles)
59
    {
60
        $this->info('Finding unused keys...');
61
        // An array of all translation keys as found in project files.
62
        $allKeysInFiles = $this->manager->collectFromFiles();
63
        foreach ($translationFiles as $fileName => $languages) {
64
            foreach ($languages as $languageKey => $path) {
65
                $fileContent = $this->manager->getFileContent($path);
66
                if (isset($allKeysInFiles[$fileName])) {
67
                    $missingKeys = array_diff(array_keys(array_dot($fileContent)), $allKeysInFiles[$fileName]);
68
                    foreach ($missingKeys as $i => $missingKey) {
69
                        $this->output->writeln("\"<fg=red>{$languageKey}.{$fileName}.{$missingKey}</>\" never used.");
70
                    }
71
                }
72
            }
73
        }
74
    }
75
}
76