Passed
Pull Request — main (#96)
by Andrey
56:20 queued 41:27
created

BaseCommand::start()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Helldar\LaravelLangPublisher\Console;
4
5
use Helldar\LaravelLangPublisher\Concerns\Containable;
6
use Helldar\LaravelLangPublisher\Concerns\Logger;
7
use Helldar\LaravelLangPublisher\Constants\Locales as LocalesList;
8
use Helldar\LaravelLangPublisher\Contracts\Actionable;
9
use Helldar\LaravelLangPublisher\Contracts\Processor;
10
use Helldar\LaravelLangPublisher\Facades\Config;
11
use Helldar\LaravelLangPublisher\Facades\Locales;
12
use Helldar\LaravelLangPublisher\Facades\Message;
13
use Helldar\LaravelLangPublisher\Facades\Path;
14
use Helldar\LaravelLangPublisher\Facades\Validator;
15
use Helldar\LaravelLangPublisher\Services\Command\Locales as LocalesSupport;
16
use Helldar\Support\Facades\Helpers\Arr;
17
use Helldar\Support\Facades\Helpers\Filesystem\File;
18
use Helldar\Support\Facades\Helpers\Str;
19
use Illuminate\Console\Command;
20
21
abstract class BaseCommand extends Command
22
{
23
    use Containable;
24
    use Logger;
25
26
    protected $action;
27
28
    protected $locales_length = 0;
29
30
    protected $files_length = 0;
31
32
    protected $files;
33
34
    protected $locales;
35 13
36
    public function handle()
37 13
    {
38 13
        $this->setLogger();
39 13
        $this->start();
40 13
        $this->clean();
41 12
        $this->ran();
42 12
        $this->end();
43
    }
44
45
    abstract protected function processor(): Processor;
46 12
47
    protected function ran(): void
48 12
    {
49 12
        foreach ($this->packages() as $package) {
50
            $this->log('Packages handling: ' . $package);
51 12
52
            $this->validatePackage($package);
53 11
54 11
            foreach ($this->locales() as $locale) {
55
                $this->log('Localization handling: ' . $locale);
56 11
57
                $this->validateLocale($locale);
58 11
59
                foreach ($this->files($package) as $filename) {
60
                    $this->log('Processing the localization file: ' . $filename);
61 11
62
                    $status = $this->process($package, $locale, $filename);
63 11
64
                    $this->processed($locale, $filename, $status, $package);
65 11
                }
66
            }
67 11
        }
68 11
    }
69 11
70 11
    protected function process(string $package, string $locale, string $filename): string
71 11
    {
72
        $this->log('Launching the processor for localization: ' . $locale . ', ' . $filename);
73
74 12
        return $this->processor()
75
            ->force($this->hasForce())
76 12
            ->package($package)
77
            ->locale($locale)
78 12
            ->filename($filename, $this->hasInline())
79 11
            ->run();
80
    }
81
82 12
    protected function locales(): array
83
    {
84
        $this->log('Getting a list of localizations...');
85 7
86
        if (! empty($this->locales)) {
87 7
            return $this->locales;
88
        }
89 7
90
        return $this->locales = LocalesSupport::make($this->input, $this->output, $this->action(), $this->targetLocales())->get();
91
    }
92 12
93
    protected function targetLocales(): array
94 12
    {
95
        $this->log('Getting a list of installed localizations...');
96 12
97 11
        return Locales::installed();
98
    }
99
100 12
    protected function packages(): array
101 12
    {
102 12
        return Config::packages();
103
    }
104
105 13
    protected function files(string $package): array
0 ignored issues
show
Unused Code introduced by
The parameter $package is not used and could be removed. ( Ignorable by Annotation )

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

105
    protected function files(/** @scrutinizer ignore-unused */ string $package): array

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
106
    {
107 13
        $this->log('Getting a list of files...');
108
109 13
        if (! empty($this->files)) {
110 13
            return $this->files;
111
        }
112 12
113
        return $this->files = File::names(Path::source(LocalesList::ENGLISH), static function ($filename) {
0 ignored issues
show
Bug introduced by
The call to Helldar\LaravelLangPubli...\Facades\Path::source() has too few arguments starting with locale. ( Ignorable by Annotation )

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

113
        return $this->files = File::names(Path::/** @scrutinizer ignore-call */ source(LocalesList::ENGLISH), static function ($filename) {

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
114 12
            return ! Str::contains($filename, 'inline');
115
        });
116 12
    }
117 12
118
    protected function start(): void
119 12
    {
120
        $action = $this->action()->present(true);
121 12
122 12
        $this->info($action . ' localizations...');
123 12
    }
124 12
125 12
    protected function end(): void
126 12
    {
127
        $action = $this->action()->past();
128 12
129 12
        $this->info('Localizations have ben successfully ' . $action . '.');
130
    }
131 12
132
    protected function processed(string $locale, string $filename, string $status, string $package = null): void
133 12
    {
134
        $message = Message::same()
135 12
            ->length($this->localesLength(), $this->filesLength())
136 12
            ->package($package)
0 ignored issues
show
Bug introduced by
The method package() does not exist on Helldar\LaravelLangPublisher\Support\Message. ( Ignorable by Annotation )

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

136
            ->/** @scrutinizer ignore-call */ package($package)

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
137
            ->locale($locale)
138
            ->filename($filename)
139 12
            ->status($status)
140
            ->get();
141 12
142
        $this->line($message);
143
    }
144 12
145
    protected function localesLength(): int
146 12
    {
147
        $this->log('Getting the maximum length of a localization string...');
148 12
149 12
        if ($this->locales_length > 0) {
150
            return $this->locales_length;
151
        }
152 12
153
        $this->log('Calculating the maximum length of a localization string...');
154 12
155
        return $this->locales_length = Arr::longestStringLength($this->locales());
156
    }
157 11
158
    protected function filesLength(): int
159 11
    {
160
        $this->log('Getting the maximum length of a filenames...');
161 11
162
        if ($this->files_length > 0) {
163
            return $this->files_length;
164 13
        }
165
166 13
        $this->log('Calculating the maximum length of a filenames...');
167
168 13
        return $this->files_length = Arr::longestStringLength($this->files());
0 ignored issues
show
Bug introduced by
The call to Helldar\LaravelLangPubli...le\BaseCommand::files() has too few arguments starting with package. ( Ignorable by Annotation )

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

168
        return $this->files_length = Arr::longestStringLength($this->/** @scrutinizer ignore-call */ files());

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
169
    }
170
171 10
    protected function hasInline(): bool
172
    {
173 10
        $this->log('Getting a use case for a validation file.');
174
175
        return Config::hasInline();
176 2
    }
177
178 2
    protected function action(): Actionable
179
    {
180
        $this->log('Getting the action...');
181 10
182
        return $this->container($this->action);
183 10
    }
184
185
    protected function hasForce(): bool
186 13
    {
187
        return $this->boolOption('force');
188 13
    }
189 12
190
    protected function hasFull(): bool
191 4
    {
192
        return $this->boolOption('full');
193 4
    }
194
195
    protected function boolOption(string $key): bool
196 13
    {
197
        return $this->hasOption($key) && $this->option($key);
198 13
    }
199
200 13
    protected function validateLocale(string $locale): void
201 13
    {
202
        Validator::locale($locale);
203
    }
204
205
    protected function validatePackage(string $package): void
206
    {
207
        Validator::package($package);
208
    }
209
210
    protected function doesntProtect(string $locale): bool
211
    {
212
        return ! Locales::isProtected($locale);
213
    }
214
215
    protected function clean(): void
216
    {
217
        $this->log('Clear the variable from the saved localizations...');
218
219
        $this->locales = null;
220
    }
221
}
222