Passed
Push — 9.x ( b7dd27...898b15 )
by Andrey
12:15
created

BaseCommand::validatePackage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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
36
    abstract protected function processor(): Processor;
37
38
    public function handle()
39
    {
40
        $this->setLogger();
41
        $this->start();
42
        $this->clean();
43
        $this->ran();
44
        $this->end();
45
    }
46
47
    protected function ran(): void
48
    {
49
        foreach ($this->packages() as $package) {
50
            $this->log('Packages handling: ' . $package);
51
52
            $this->validatePackage($package);
53
54
            foreach ($this->locales() as $locale) {
55
                $this->log('Localization handling: ' . $locale);
56
57
                $this->validateLocale($locale);
58
59
                foreach ($this->files($package) as $filename) {
60
                    $this->log('Processing the localization file: ' . $filename);
61
62
                    $status = $this->process($package, $locale, $filename);
63
64
                    $this->processed($locale, $filename, $status, $package);
65
                }
66
            }
67
        }
68
    }
69
70
    protected function process(string $package, string $locale, string $filename): string
71
    {
72
        $this->log('Launching the processor for localization: ' . $locale . ', ' . $filename);
73
74
        return $this->processor()
75
            ->force($this->hasForce())
76
            ->package($package)
77
            ->locale($locale)
78
            ->filename($filename, $this->hasInline())
79
            ->run();
80
    }
81
82
    protected function locales(): array
83
    {
84
        $this->log('Getting a list of localizations...');
85
86
        if (! empty($this->locales)) {
87
            return $this->locales;
88
        }
89
90
        return $this->locales = LocalesSupport::make($this->input, $this->output, $this->action(), $this->targetLocales())->get();
91
    }
92
93
    protected function targetLocales(): array
94
    {
95
        $this->log('Getting a list of installed localizations...');
96
97
        return Locales::installed();
98
    }
99
100
    protected function packages(): array
101
    {
102
        return Config::packages();
103
    }
104
105
    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
        $this->log('Getting a list of files...');
108
109
        if (! empty($this->files)) {
110
            return $this->files;
111
        }
112
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
            return ! Str::contains($filename, 'inline');
115
        });
116
    }
117
118
    protected function start(): void
119
    {
120
        $action = $this->action()->present(true);
121
122
        $this->info($action . ' localizations...');
123
    }
124
125
    protected function end(): void
126
    {
127
        $action = $this->action()->past();
128
129
        $this->info('Localizations have ben successfully ' . $action . '.');
130
    }
131
132
    protected function processed(string $locale, string $filename, string $status, string $package = null): void
133
    {
134
        $message = Message::same()
135
            ->length($this->localesLength(), $this->filesLength())
136
            ->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
            ->status($status)
140
            ->get();
141
142
        $this->line($message);
143
    }
144
145
    protected function localesLength(): int
146
    {
147
        $this->log('Getting the maximum length of a localization string...');
148
149
        if ($this->locales_length > 0) {
150
            return $this->locales_length;
151
        }
152
153
        $this->log('Calculating the maximum length of a localization string...');
154
155
        return $this->locales_length = Arr::longestStringLength($this->locales());
156
    }
157
158
    protected function filesLength(): int
159
    {
160
        $this->log('Getting the maximum length of a filenames...');
161
162
        if ($this->files_length > 0) {
163
            return $this->files_length;
164
        }
165
166
        $this->log('Calculating the maximum length of a filenames...');
167
168
        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
    protected function hasInline(): bool
172
    {
173
        $this->log('Getting a use case for a validation file.');
174
175
        return Config::hasInline();
176
    }
177
178
    protected function action(): Actionable
179
    {
180
        $this->log('Getting the action...');
181
182
        return $this->container($this->action);
183
    }
184
185
    protected function hasForce(): bool
186
    {
187
        return $this->boolOption('force');
188
    }
189
190
    protected function hasFull(): bool
191
    {
192
        return $this->boolOption('full');
193
    }
194
195
    protected function boolOption(string $key): bool
196
    {
197
        return $this->hasOption($key) && $this->option($key);
198
    }
199
200
    protected function validateLocale(string $locale): void
201
    {
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