Passed
Pull Request — main (#123)
by Andrey
42:12 queued 26:54
created

Ask   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
dl 0
loc 53
rs 10
c 1
b 0
f 0
wmc 11

7 Methods

Rating   Name   Duplication   Size   Complexity  
A askLocales() 0 5 2
A getAllLocales() 0 3 1
A getLocales() 0 7 2
A selectLocales() 0 3 1
A resolveSelectedLocales() 0 9 2
A getMethod() 0 5 1
A validatedLocales() 0 7 2
1
<?php
2
3
/*
4
 * This file is part of the "andrey-helldar/laravel-lang-publisher" project.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * @author Andrey Helldar <[email protected]>
10
 *
11
 * @copyright 2021 Andrey Helldar
12
 *
13
 * @license MIT
14
 *
15
 * @see https://github.com/andrey-helldar/laravel-lang-publisher
16
 */
17
18
declare(strict_types=1);
19
20
namespace Helldar\LaravelLangPublisher\Concerns;
21
22
use Helldar\LaravelLangPublisher\Facades\Helpers\Locales;
23
use Helldar\Support\Facades\Helpers\Arr;
24
use Helldar\Support\Facades\Helpers\Str;
25
26
trait Ask
27
{
28
    protected function getLocales(): array
29
    {
30
        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

30
        if ($locales = $this->/** @scrutinizer ignore-call */ argument('locales')) {
Loading history...
31
            return $this->resolveSelectedLocales($locales);
32
        }
33
34
        return $this->askLocales($this->getMethod());
35
    }
36
37
    protected function askLocales(string $method): array
38
    {
39
        $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

39
        $locales = $this->/** @scrutinizer ignore-call */ confirm("Do you want to $method all localizations?") ? $this->getAllLocales() : $this->selectLocales($method);
Loading history...
40
41
        return $this->resolveSelectedLocales($locales);
42
    }
43
44
    protected function getAllLocales(): array
45
    {
46
        return Locales::available();
47
    }
48
49
    protected function selectLocales(string $method)
50
    {
51
        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

51
        return $this->/** @scrutinizer ignore-call */ choice("Select localizations to $method (specify the necessary localizations separated by commas):", $this->getAllLocales(), null, null, true);
Loading history...
52
    }
53
54
    protected function resolveSelectedLocales($locales): array
55
    {
56
        if ($locales === '*') {
57
            return $this->getAllLocales();
58
        }
59
60
        $locales = Arr::wrap($locales);
61
62
        return $this->validatedLocales($locales);
63
    }
64
65
    protected function getMethod(): string
66
    {
67
        $name = class_basename(static::class);
68
69
        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...
70
    }
71
72
    protected function validatedLocales(array $locales): array
73
    {
74
        foreach ($locales as $locale) {
75
            Locales::validate($locale);
76
        }
77
78
        return $locales;
79
    }
80
}
81