Completed
Push — master ( 121fac...aa6513 )
by Harry Osmar
06:34 queued 03:36
created

Validation   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 113
Duplicated Lines 22.12 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 77.42%

Importance

Changes 0
Metric Value
dl 25
loc 113
ccs 24
cts 31
cp 0.7742
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\Libraries\Language;
6
7
class Validation implements \PluginSimpleValidate\Contracts\Validation
8
{
9
    /**
10
     * @var array
11
     * array of Field
12
     */
13
    private $fields;
14
15
    /**
16
     * @var array
17
     */
18
    private $errors;
19
20
    /**
21
     * @var Language
22
     */
23
    private $language;
24
25
    /**
26
     * @var bool
27
     */
28
    private $status;
29
30
    /**
31
     * Validation constructor.
32
     * @param Language $language
33
     */
34 2
    public function __construct(Language $language)
35
    {
36 2
        $this->language = $language;
37 2
        $this->fields = [];
38 2
        $this->errors = [];
39 2
        $this->status = false;
40 2
    }
41
42
    /**
43
     * @param \PluginSimpleValidate\Contracts\Field $field
44
     * @return $this
45
     */
46 2
    public function addField(\PluginSimpleValidate\Contracts\Field $field)
47
    {
48 2
        $this->fields[$field->getName()] = $field;
49 2
        return $this;
50
    }
51
52
    /**
53
     * @param bool $break_when_error
54
     * @return bool
55
     */
56 2 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...
57
    {
58 2
        $this->emptyErrors();
59
60
        /** @var Field $field */
61 2
        foreach ($this->fields as $field) {
62 2
            if (!$field->isValid($this->language)) {
63 2
                $this->status = false;
64 2
                $this->errors[$field->getName()] = $field->getErrors();
65
66
                /**
67
                 * break when there is any field error
68
                 */
69 2
                if ($break_when_error) {
70 2
                    break;
71
                }
72
            }
73
        }
74
75 2
        if (empty($this->errors)) {
76
            $this->status = true;
77
        }
78
79 2
        return $this->status;
80
    }
81
82
    /**
83
     * @return array
84
     */
85 2
    public function getErrors()
86
    {
87 2
        return $this->errors;
88
    }
89
90
    /**
91
     * @return $this
92
     */
93 2
    protected function emptyErrors()
94
    {
95 2
        $this->errors = [];
96 2
        return $this;
97
    }
98
99
    /**
100
     * @param string $fieldName
101
     * @return array|Field|null
102
     */
103
    public function getFields(string $fieldName = '')
104
    {
105
        if (empty($fieldName)) {
106
            return $this->fields;
107
        }
108
109
        return isset($this->fields[$fieldName]) ? $this->fields[$fieldName] : null;
110
    }
111
112
    /**
113
     * @return bool
114
     */
115
    public function getStatus(): bool
116
    {
117
        return $this->status;
118
    }
119
}