ValidatesInput::attributes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cerbero\CommandValidator;
6
7
use Illuminate\Contracts\Validation\Validator;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
/**
12
 * The trait to validate the input of console commands.
13
 */
14
trait ValidatesInput
15
{
16
    /**
17
     * The validator.
18
     */
19
    protected Validator $validator;
20
21
    /**
22
     * Retrieve the validation rules.
23
     *
24
     * @return array<string, mixed>
25
     */
26
    abstract protected function rules(): array;
27
28
    /**
29
     * Execute the console command.
30
     */
31 3
    protected function execute(InputInterface $input, OutputInterface $output): int
32
    {
33 3
        if ($this->validator()->fails()) {
34 2
            $this->printErrors($this->formatErrors());
35
36 2
            return static::FAILURE;
0 ignored issues
show
Bug introduced by
The constant Cerbero\CommandValidator\ValidatesInput::FAILURE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
37
        }
38
39 1
        return parent::execute($input, $output);
40
    }
41
42
    /**
43
     * Retrieve the validator.
44
     */
45 3
    protected function validator(): Validator
46
    {
47
        /** @phpstan-ignore-next-line */
48 3
        return $this->validator ??= $this->laravel['validator']->make(
49 3
            $this->getDataToValidate(),
50 3
            $this->rules(),
51 3
            $this->messages(),
52 3
            $this->attributes(),
53 3
        );
54
    }
55
56
    /**
57
     * Retrieve the data to validate.
58
     *
59
     * @return array<string, mixed>
60
     */
61 3
    protected function getDataToValidate(): array
62
    {
63 3
        return array_filter([...$this->argument(), ...$this->option()], fn(mixed $value) => $value !== null);
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

63
        return array_filter([...$this->/** @scrutinizer ignore-call */ argument(), ...$this->option()], fn(mixed $value) => $value !== null);
Loading history...
Bug introduced by
It seems like option() 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

63
        return array_filter([...$this->argument(), ...$this->/** @scrutinizer ignore-call */ option()], fn(mixed $value) => $value !== null);
Loading history...
64
    }
65
66
    /**
67
     * Print the given errors to the console.
68
     */
69 2
    protected function printErrors(string $errors): void
70
    {
71 2
        $this->output->block($errors, style: 'fg=white;bg=red', prefix: '  ', padding: true);
72
    }
73
74
    /**
75
     * Format the validation errors.
76
     */
77 2
    protected function formatErrors(): string
78
    {
79 2
        return implode(PHP_EOL, $this->validator()->errors()->all());
80
    }
81
82
    /**
83
     * Retrieve the custom error messages.
84
     *
85
     * @return array<string, string>
86
     */
87 1
    protected function messages(): array
88
    {
89 1
        return [];
90
    }
91
92
    /**
93
     * Retrieve the custom error attributes.
94
     *
95
     * @return array<string, string>
96
     */
97 1
    protected function attributes(): array
98
    {
99 1
        return [];
100
    }
101
}
102