FindCommand::handle()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 4
nop 0
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
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 FindCommand extends Command
10
{
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'transcribe:find {keyword} {--package : Vendor Package name to search within.}';
17
18
    /**
19
     * The name and signature of the console command.
20
     *
21
     * @var string
22
     */
23
    protected $description = 'Find key with values matching the keyword.';
24
25
    /**
26
     * The Languages manager instance.
27
     *
28
     * @var \NicolasBeauvais\Transcribe\Manager
29
     */
30
    private $manager;
31
32
    /**
33
     * Array of files grouped by filename.
34
     *
35
     * @var array
36
     */
37
    protected $files;
38
39
    /**
40
     * ListCommand constructor.
41
     *
42
     * @param \NicolasBeauvais\Transcribe\Manager $manager
43
     *
44
     * @return void
45
     */
46
    public function __construct(Manager $manager)
47
    {
48
        parent::__construct();
49
50
        $this->manager = $manager;
51
    }
52
53
    /**
54
     * Execute the console command.
55
     *
56
     * @return mixed
57
     */
58
    public function handle()
59
    {
60
        if ($package = $this->option('package')) {
61
            $this->manager->setPathToVendorPackage($package);
62
        }
63
64
        $this->files = $this->manager->files();
65
66
        if (empty($this->files)) {
67
            $this->warn('No language files were found!');
68
        }
69
70
        $languages = $this->manager->languages();
71
72
        $this->table(
73
            array_merge(['key'], $languages),
74
            $this->tableRows()
75
        );
76
    }
77
78
    /**
79
     * The output of the table rows.
80
     *
81
     * @return array
82
     */
83
    private function tableRows()
84
    {
85
        $allLanguages = $this->manager->languages();
86
87
        $filesContent = [];
88
89
        $output = [];
90
91
        foreach ($this->files as $fileName => $fileLanguages) {
92
            foreach ($fileLanguages as $languageKey => $filePath) {
93
                $lines = $filesContent[$fileName][$languageKey] = Arr::dot($this->manager->getFileContent($filePath));
94
95
                foreach ($lines as $key => $line) {
96
                    if (!is_array($line) && stripos($line, $this->argument('keyword')) !== false) {
0 ignored issues
show
Bug introduced by
It seems like $this->argument('keyword') can also be of type array; however, parameter $needle of stripos() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

96
                    if (!is_array($line) && stripos($line, /** @scrutinizer ignore-type */ $this->argument('keyword')) !== false) {
Loading history...
97
                        $output[$fileName.'.'.$key][$languageKey] = "<bg=yellow;fg=black>{$line}</>";
98
                    }
99
                }
100
            }
101
        }
102
103
        // Now that we collected all values that matches the keyword argument
104
        // in a close match, we collect the values for the rest of the
105
        // languages for the found keys to complete the table view.
106
        foreach ($output as $fullKey => $values) {
107
            list($fileName, $key) = explode('.', $fullKey, 2);
108
109
            $original = [];
110
111
            foreach ($allLanguages as $languageKey) {
112
                $original[$languageKey] =
113
                    isset($values[$languageKey])
114
                        ? $values[$languageKey]
115
                        : isset($filesContent[$fileName][$languageKey][$key]) ? $filesContent[$fileName][$languageKey][$key] : '';
116
            }
117
118
            // Sort the language values based on language name
119
            ksort($original);
120
121
            $output[$fullKey] = array_merge(['key' => "<fg=yellow>$fullKey</>"], $original);
122
        }
123
124
        return array_values($output);
125
    }
126
}
127