Passed
Pull Request — main (#123)
by Andrey
29:01 queued 13:53
created

Ask::selectLocales()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Helldar\LaravelLangPublisher\Concerns;
6
7
use Helldar\LaravelLangPublisher\Facades\Helpers\Locales;
8
use Helldar\Support\Facades\Helpers\Arr;
9
use Helldar\Support\Facades\Helpers\Str;
10
11
trait Ask
12
{
13
    protected function getLocales(): array
14
    {
15
        if ($locales = $this->argument('locales')) {
0 ignored issues
show
Bug introduced by
It seems like argument() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

15
        if ($locales = $this->/** @scrutinizer ignore-call */ argument('locales')) {
Loading history...
16
            return $this->resolveSelectedLocales($locales);
17
        }
18
19
        return $this->askLocales($this->getMethod());
20
    }
21
22
    protected function askLocales(string $method): array
23
    {
24
        $locales = $this->confirm("Do you want to $method all localizations?") ? $this->getAllLocales() : $this->selectLocales($method);
0 ignored issues
show
Bug introduced by
It seems like confirm() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

24
        $locales = $this->/** @scrutinizer ignore-call */ confirm("Do you want to $method all localizations?") ? $this->getAllLocales() : $this->selectLocales($method);
Loading history...
25
26
        return $this->resolveSelectedLocales($locales);
27
    }
28
29
    protected function getAllLocales(): array
30
    {
31
        return Locales::available();
32
    }
33
34
    protected function selectLocales(string $method)
35
    {
36
        return $this->choice("Select localizations to $method (specify the necessary localizations separated by commas):", $this->getAllLocales(), null, null, true);
0 ignored issues
show
Bug introduced by
It seems like choice() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

36
        return $this->/** @scrutinizer ignore-call */ choice("Select localizations to $method (specify the necessary localizations separated by commas):", $this->getAllLocales(), null, null, true);
Loading history...
37
    }
38
39
    protected function resolveSelectedLocales($locales): array
40
    {
41
        $locales = Arr::wrap($locales);
42
43
        return $this->validatedLocales($locales);
44
    }
45
46
    protected function getMethod(): string
47
    {
48
        $name = class_basename(static::class);
49
50
        return Str::lower($name);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Helldar\Support\F...lpers\Str::lower($name) could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
51
    }
52
53
    protected function validatedLocales(array $locales): array
54
    {
55
        foreach ($locales as $locale) {
56
            Locales::validate($locale);
57
        }
58
59
        return $locales;
60
    }
61
}
62