Validation   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 113
Duplicated Lines 22.12 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 83.87%

Importance

Changes 0
Metric Value
dl 25
loc 113
ccs 26
cts 31
cp 0.8387
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A addField() 0 5 1
B run() 25 25 5
A getErrors() 0 4 1
A emptyErrors() 0 5 1
A getFields() 0 8 3
A getStatus() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace PluginSimpleValidate;
4
5
use PluginSimpleValidate\Contracts\BaseField;
6
use PluginSimpleValidate\Libraries\Language;
7
8
class Validation implements \PluginSimpleValidate\Contracts\Validation
9
{
10
    /**
11
     * @var array
12
     * array of Field
13
     */
14
    private $fields;
15
16
    /**
17
     * @var array
18
     */
19
    private $errors;
20
21
    /**
22
     * @var Language
23
     */
24
    private $language;
25
26
    /**
27
     * @var bool
28
     */
29
    private $status;
30
31
    /**
32
     * Validation constructor.
33
     * @param Language $language
34
     */
35 6
    public function __construct(Language $language)
36
    {
37 6
        $this->language = $language;
38 6
        $this->fields = [];
39 6
        $this->errors = [];
40 6
        $this->status = false;
41 6
    }
42
43
    /**
44
     * @param BaseField $field
45
     * @return $this
46
     */
47 6
    public function addField(BaseField $field)
48
    {
49 6
        $this->fields[$field->getName()] = $field;
50 6
        return $this;
51
    }
52
53
    /**
54
     * @param bool $break_when_error
55
     * @return bool
56
     */
57 6 View Code Duplication
    public function run($break_when_error = false) : bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
    {
59 6
        $this->emptyErrors();
60
61
        /** @var Field $field */
62 6
        foreach ($this->fields as $field) {
63 6
            if (!$field->isValid($this->language)) {
64 6
                $this->status = false;
65 6
                $this->errors[$field->getName()] = $field->getErrors();
66
67
                /**
68
                 * break when there is any field error
69
                 */
70 6
                if ($break_when_error) {
71 6
                    break;
72
                }
73
            }
74
        }
75
76 6
        if (empty($this->errors)) {
77
            $this->status = true;
78
        }
79
80 6
        return $this->status;
81
    }
82
83
    /**
84
     * @return array
85
     */
86 6
    public function getErrors()
87
    {
88 6
        return $this->errors;
89
    }
90
91
    /**
92
     * @return $this
93
     */
94 6
    protected function emptyErrors()
95
    {
96 6
        $this->errors = [];
97 6
        return $this;
98
    }
99
100
    /**
101
     * @param string $fieldName
102
     * @return array|Field|null
103
     */
104
    public function getFields(string $fieldName = '')
105
    {
106
        if (empty($fieldName)) {
107
            return $this->fields;
108
        }
109
110
        return isset($this->fields[$fieldName]) ? $this->fields[$fieldName] : null;
111
    }
112
113
    /**
114
     * @return bool
115
     */
116 1
    public function getStatus(): bool
117
    {
118 1
        return $this->status;
119
    }
120
}