Passed
Push — feature/second-release ( 3d3b5e...855c58 )
by Andrea Marco
14:24
created

EnumAnnotateCommand::enums()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 15
rs 10
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\Annotator;
9
use Illuminate\Console\Command;
10
use Throwable;
11
12
use function Laravel\Prompts\multiselect;
13
14
/**
15
 * The console command to annotate enums.
16
 */
17
final class EnumAnnotateCommand extends Command
18
{
19
    /**
20
     * The console command description.
21
     *
22
     * @var string
23
     */
24
    protected $description = 'Annotate enums to ease IDE autocompletion';
25
26
    /**
27
     * The name and signature of the console command.
28
     *
29
     * @var string
30
     */
31
    protected $signature = 'enum:annotate {enums?*} {--a|all} {--f|force}';
32
33
    /**
34
     * Handle the command.
35
     */
36
    public function handle(Annotator $annotator): int
37
    {
38
        if (! $enums = $this->enums()) {
39
            $this->info('No enums to annotate.');
40
41
            return self::SUCCESS;
42
        }
43
44
        $e = null;
45
        $succeeded = true;
46
47
        foreach($enums as $enum) {
48
            try {
49
                $succeeded = $annotator->annotate($enum, !! $this->option('force')) && $succeeded;
50
            } catch (Throwable $e) {
51
                $succeeded = false;
52
            }
53
54
            $message = $succeeded
55
                ? "<bg=#16a34a;fg=#fff;options=bold> DONE </> {$enum}\n"
56
                : "<bg=#e11d48;fg=#fff;options=bold> FAIL </> {$enum} <fg=#e11d48>{$e?->getMessage()}</>\n";
57
58
            $this->line($message);
59
        }
60
61
        return $succeeded ? self::SUCCESS : self::FAILURE;
62
    }
63
64
    /**
65
     * Retrieve the enums to annotate.
66
     *
67
     * @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...
68
     */
69
    private function enums(): array
70
    {
71
        if ($enums = (array) $this->argument('enums')) {
72
            /** @var list<string> $enums */
73
            $namespaces = array_map(fn(string $enum) => str_replace('/', '\\', $enum), $enums);
0 ignored issues
show
Bug introduced by
$enums of type Cerbero\LaravelEnum\Commands\list is incompatible with the type array expected by parameter $array of array_map(). ( Ignorable by Annotation )

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

73
            $namespaces = array_map(fn(string $enum) => str_replace('/', '\\', $enum), /** @scrutinizer ignore-type */ $enums);
Loading history...
74
75
            /** @var list<class-string<\UnitEnum>> */
76
            return array_unique(array_filter($namespaces, 'enum_exists'));
77
        }
78
79
        /** @var list<class-string<\UnitEnum>> */
80
        return match (true) {
81
            empty($enums = [...Enums::namespaces()]) => [],
82
            $this->option('all') => $enums,
83
            default => multiselect('Enums to annotate:', $enums, required: true, hint: 'Press space to select.'),
84
        };
85
    }
86
}
87