Passed
Push — develop ( 9b8c4e...79e7ae )
by Andrea Marco
03:34 queued 14s
created

EnumTsCommand::handle()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 8
c 1
b 0
f 0
nc 7
nop 0
dl 0
loc 16
ccs 9
cts 9
cp 1
crap 5
rs 9.6111
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cerbero\LaravelEnum\Commands;
6
7
use Cerbero\LaravelEnum\Enums;
8
use Cerbero\LaravelEnum\Services\TypeScript;
9
use Illuminate\Console\Command;
10
11
use function Cerbero\Enum\normalizeEnums;
12
use function Cerbero\LaravelEnum\output;
13
use function Laravel\Prompts\multiselect;
14
15
/**
16
 * The console command to synchronize enums in TypeScript.
17
 */
18
final class EnumTsCommand extends Command
19
{
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'Synchronize enums in TypeScript';
26
27
    /**
28
     * The name and signature of the console command.
29
     *
30
     * @var string
31
     */
32
    protected $signature = 'enum:ts
33
                            {enums?* : The enums to synchronize}
34
                            {--a|all : Whether all enums should be synchronized}
35
                            {--f|force : Whether existing enums should be overwritten}';
36
37
    /**
38
     * Handle the command.
39
     */
40 3
    public function handle(): int
41
    {
42 3
        if (! $enums = $this->enums()) {
43 2
            $this->info('No enums to annotate.');
44
45 2
            return self::SUCCESS;
46
        }
47
48 1
        $succeeded = true;
49 1
        $force = !! $this->option('force');
50
51 1
        foreach($enums as $enum) {
52 1
            $succeeded = output($this->output, $enum, fn() => (new TypeScript($enum))->sync($force)) && $succeeded;
53
        }
54
55 1
        return $succeeded ? self::SUCCESS : self::FAILURE;
56
    }
57
58
    /**
59
     * Retrieve the enums to annotate.
60
     *
61
     * @return list<class-string<\UnitEnum>>
0 ignored issues
show
Bug introduced by
The type Cerbero\LaravelEnum\Commands\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
62
     */
63 3
    private function enums(): array
64
    {
65
        /** @var list<class-string<\UnitEnum>> */
66
        return match (true) {
67 3
            ! empty($enums = (array) $this->argument('enums')) => normalizeEnums($enums),
68 2
            empty($enums = [...Enums::namespaces()]) => [],
69 1
            $this->option('all') => $enums,
70
            /** @phpstan-ignore argument.type */
71 3
            default => multiselect('Enums to synchronize:', $enums, required: true, hint: 'Press space to select.'),
72
        };
73
    }
74
}
75