Completed
Pull Request — master (#2)
by
unknown
08:42
created

ValidatesInput::validator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
1
<?php
2
3
namespace Cerbero\CommandValidator;
4
5
use Symfony\Component\Console\Input\InputInterface;
6
use Symfony\Component\Console\Output\OutputInterface;
7
8
/**
9
 * Validate input submitted to console commands.
10
 *
11
 * @author    Andrea Marco Sartori
12
 */
13
trait ValidatesInput
14
{
15
    /**
16
     * @author    Andrea Marco Sartori
17
     * @var        Illuminate\Validation\Validator    $validator    Input validator.
18
     */
19
    protected $validator;
20
21
    /**
22
     * Execute the console command.
23
     *
24
     * @param  \Symfony\Component\Console\Input\InputInterface  $input
25
     * @param  \Symfony\Component\Console\Output\OutputInterface  $output
26
     * @return mixed
27
     */
28
    protected function execute(InputInterface $input, OutputInterface $output)
29
    {
30
        if ($this->validator()->passes()) {
31
            return parent::execute($input, $output);
32
        }
33
34
        return $this->error($this->getFormattedErrors());
0 ignored issues
show
Bug introduced by
It seems like error() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
35
    }
36
37
    /**
38
     * Retrieve the input validator.
39
     *
40
     * @author    Andrea Marco Sartori
41
     * @return    Illuminate\Validation\Validator
42
     */
43
    protected function validator()
44
    {
45
        return $this->validator = $this->validator ?: $this->laravel['validator']->make(
0 ignored issues
show
Bug introduced by
The property laravel does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
46
            $this->getValidationArray(), $this->rules(), $this->messages(), $this->attributes()
47
        );
48
    }
49
50
    /**
51
     * Format console command input for validation.
52
     *
53
     * @return array
54
     */
55
    protected function getValidationArray()
56
    {
57
        // Null command arguments and options are filtered to remove such inputs to conform to the Laravel validation
58
        // changes introduced in v5.3 concerning null values, which are no longer ignored
59
        return array_filter(array_merge($this->option(), $this->argument()), function($var) {
0 ignored issues
show
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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
60
            return !is_null($var);
61
        });
62
    }
63
64
    /**
65
     * Retrieve and format the validation errors.
66
     *
67
     * @author    Andrea Marco Sartori
68
     * @return    string
69
     */
70
    protected function getFormattedErrors()
71
    {
72
        $errors = implode("\n", $this->validator()->errors()->all());
73
74
        return "\n\n{$errors}\n";
75
    }
76
77
    /**
78
     * Retrieve the validation rules.
79
     *
80
     * @author    Andrea Marco Sartori
81
     * @return    array
82
     */
83
    abstract protected function rules();
84
85
    /**
86
     * Retrieve the custom error messages.
87
     *
88
     * @author    Andrea Marco Sartori
89
     * @return    array
90
     */
91
    protected function messages()
92
    {
93
        return [];
94
    }
95
96
    /**
97
     * Retrieve the custom attributes for error messages.
98
     *
99
     * @author    Andrea Marco Sartori
100
     * @return    array
101
     */
102
    protected function attributes()
103
    {
104
        return [];
105
    }
106
}
107