Completed
Branch master (beb88f)
by Mohamed
05:11 queued 03:39
created

AskAndValidate::askWithValidation()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 0
cts 16
cp 0
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 11
nc 3
nop 2
crap 12
1
<?php
2
3
namespace Moo\FlashCard\Traits;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Validator;
7
8
/**
9
 * Trait AskAndValidate provide command user input for ask and validate the user input
10
 *
11
 * @package Moo\FlashCard\Traits
12
 */
13
trait AskAndValidate
14
{
15
    /**
16
     * @param string $question
17
     * @param string $field
18
     * @return string
19
     */
20
    protected function askWithValidation(string $question, string $field): string
21
    {
22
        // Ask the question and get the answer
23
        $input = $this->ask($question);
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
        // Populate the model
26
        $this->getEntity()->fill([
27
            $field => $input,
28
        ]);
29
30
        // Validate the model data
31
        $validator = $this->getEntity()->getValidator();
32
        if ($validator->fails()) {
33
            // Get error message for the field
34
            $message = (string)$validator->errors()->first($field);
35
            // Display warning message if exists
36
            if (!empty($message)) {
37
                $this->warn($message);
0 ignored issues
show
Bug introduced by
It seems like warn() 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...
38
                // Ask the question again
39
                return $this->askWithValidation($question, $field);
40
            }
41
        }
42
43
        return (string)$input;
44
    }
45
46
    /**
47
     * Required method to return the entity/model that contains the validation rules
48
     *
49
     * @return Model
50
     */
51
    abstract protected function getEntity(): Model;
52
}
53