Completed
Push — master ( ab125a...1101b1 )
by Mohamed
02:50
created

AskAndValidate   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 63.64%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 1
dl 0
loc 40
ccs 7
cts 11
cp 0.6364
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
getEntity() 0 1 ?
B askWithValidation() 0 25 3
1
<?php
2
3
namespace Moo\FlashCard\Traits;
4
5
use Illuminate\Database\Eloquent\Model;
6
7
/**
8
 * Trait AskAndValidate provide command user input for ask and validate the user input
9
 */
10
trait AskAndValidate
11
{
12
    /**
13
     * @param  string $question
14
     * @param  string $field
15
     * @return string
16
     */
17 2
    protected function askWithValidation(string $question, string $field): string
18
    {
19
        // Ask the question and get the answer
20 2
        $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...
21
22
        // Populate the model
23 2
        $this->getEntity()->fill([
24 2
            $field => $input,
25
        ]);
26
27
        // Validate the model data
28 2
        $validator = $this->getEntity()->getValidator();
29 2
        if ($validator->fails()) {
30
            // Get error message for the field
31
            $message = (string) $validator->errors()->first($field);
32
            // Display warning message if exists
33
            if (!empty($message)) {
34
                $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...
35
                // Ask the question again
36
                return $this->askWithValidation($question, $field);
37
            }
38
        }
39
40 2
        return (string) $input;
41
    }
42
43
    /**
44
     * Required method to return the entity/model that contains the validation rules
45
     *
46
     * @return Model
47
     */
48
    abstract protected function getEntity(): Model;
49
}
50