Completed
Push — master ( 182185...cd5526 )
by Abdelrahman
01:17 queued 11s
created

ArtisanCanValidateAnswers::validateInput()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 3
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Support\Traits;
6
7
use Illuminate\Support\Facades\Validator;
8
9
trait ArtisanCanValidateAnswers
10
{
11
    /**
12
     * Require valid answer for the asked question.
13
     *
14
     * @param string       $question
15
     * @param string       $field
16
     * @param string|array $rules
17
     * @param null         $default
18
     *
19
     * @return mixed
20
     */
21
    protected function askValid($question, $field, $rules, $default = null)
22
    {
23
        $value = $this->ask($question, $default);
0 ignored issues
show
Bug introduced by
It seems like ask() 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...
24
25
        if ($message = $this->validateInput($field, $value, $rules)) {
26
            $this->error($message);
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...
27
28
            return $this->askValid($question, $field, $rules, $default);
29
        }
30
31
        return $value;
32
    }
33
34
    /**
35
     * Validate input field.
36
     *
37
     * @param string $field
38
     * @param string $value
39
     * @param string|array $rules
40
     *
41
     * @return string|null
42
     */
43
    protected function validateInput($field, $value, $rules)
44
    {
45
        $validator = Validator::make(
46
            [$field => $value],
47
            [$field => $rules]
48
        );
49
50
        return $validator->fails() ? $validator->errors()->first($field) : null;
51
    }
52
}
53