Passed
Push — master ( 289a67...2a2cfd )
by
unknown
01:53
created

Field   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 171
Duplicated Lines 13.45 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 93.18%

Importance

Changes 0
Metric Value
dl 23
loc 171
ccs 41
cts 44
cp 0.9318
rs 10
c 0
b 0
f 0
wmc 17
lcom 1
cbo 2

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A addRules() 0 5 1
B isValid() 23 23 5
A getName() 0 4 1
A getValue() 0 4 1
A getRules() 0 4 1
A getErrors() 0 4 1
A required() 0 5 1
A validEmail() 0 5 1
A equal() 0 5 1
A lessThan() 0 5 1
A between() 0 5 1
A isTrue() 0 5 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 const PluginSimpleValidate\helper\Validate\VAR_IS_TRUE;
6
use const PluginSimpleValidate\helper\Validate\VAR_LIMIT;
7
use const PluginSimpleValidate\helper\Validate\VAR_LOWER_LIMIT;
8
use const PluginSimpleValidate\helper\Validate\VAR_MATCH;
9
use const PluginSimpleValidate\helper\Validate\VAR_MESSAGE;
10
use const PluginSimpleValidate\helper\Validate\VAR_UPPER_LIMIT;
11
use PluginSimpleValidate\Libraries\Language;
12
13
class Field extends \PluginSimpleValidate\BaseAbstract\Field implements \PluginSimpleValidate\Contracts\Field
14
{
15
    /**
16
     * @var string
17
     */
18
    private $name;
19
20
    /**
21
     * @var string
22
     */
23
    private $value;
24
25
    /**
26
     * @var array
27
     */
28
    private $errors;
29
30
    /**
31
     * @var array
32
     * array of Rule
33
     */
34
    private $rules = [];
35
36
    /**
37
     * @var bool
38
     */
39
    private $status;
40
41
    /**
42
     * Field constructor.
43
     * @param string $name
44
     * @param mixed $value
45
     */
46 8
    public function __construct(string $name, $value)
47
    {
48 8
        $this->name = $name;
49 8
        $this->value = $value;
50 8
        $this->errors = [];
51 8
    }
52
53
    /**
54
     * @param string $rulesMethod
55
     * @param array $args
56
     * @return $this
57
     */
58 7
    protected function addRules(string $rulesMethod, array $args = [])
59
    {
60 7
        $this->rules[$rulesMethod] = RuleMapping::getRule($rulesMethod, $args);
61 7
        return $this;
62
    }
63
64
    /**
65
     * @param Language $language
66
     * @param bool $break_when_error
67
     * @return bool
68
     */
69 7 View Code Duplication
    public function isValid(Language $language, $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...
70
    {
71
        /** @var \PluginSimpleValidate\Contracts\Rule $rule */
72 7
        foreach ($this->rules as $ruleName => $rule) {
73 7
            if (!$rule->isValid($language, $this->value)) {
74 7
                $this->status = false;
75 7
                $this->errors[] = $rule->getError();
76
77
                /**
78
                 * break when there is any rule error
79
                 */
80 7
                if ($break_when_error === true) {
81 7
                    break;
82
                }
83
            }
84
        }
85
86 7
        if (empty($this->errors)) {
87
            $this->status = true;
88
        }
89
90 7
        return $this->status;
91
    }
92
93
    /**
94
     * @return string
95
     */
96 2
    public function getName(): string
97
    {
98 2
        return $this->name;
99
    }
100
101
    /**
102
     * @return mixed
103
     */
104 1
    public function getValue()
105
    {
106 1
        return $this->value;
107
    }
108
109
    /**
110
     * @return array
111
     */
112
    public function getRules(): array
113
    {
114
        return $this->rules;
115
    }
116
117
    /**
118
     * @return array
119
     */
120 7
    public function getErrors(): array
121
    {
122 7
        return $this->errors;
123
    }
124
125
    /**
126
     * @return $this
127
     */
128 3
    public function required()
129
    {
130 3
        $this->addRules('required');
131 3
        return $this;
132
    }
133
134
    /**
135
     * @return $this
136
     */
137 2
    public function validEmail()
138
    {
139 2
        $this->addRules('valid_email');
140 2
        return $this;
141
    }
142
143
    /**
144
     * @param int|float|double|string $match
145
     * @return $this
146
     */
147 1
    public function equal($match)
148
    {
149 1
        $this->addRules('equal', [VAR_MATCH => $match]);
150 1
        return $this;
151
    }
152
153
    /**
154
     * @param int|float|double $limit
155
     * @return $this
156
     */
157 1
    public function lessThan($limit)
158
    {
159 1
        $this->addRules('less_than', [VAR_LIMIT => $limit]);
160 1
        return $this;
161
    }
162
163
    /**
164
     * @param int|float|double $lower
165
     * @param int|float|double $upper
166
     * @return $this
167
     */
168 1
    public function between($lower, $upper)
169
    {
170 1
        $this->addRules('between', [VAR_LOWER_LIMIT => $lower, VAR_UPPER_LIMIT => $upper]);
171 1
        return $this;
172
    }
173
174
    /**
175
     * @param string $message
176
     * @return $this
177
     */
178 1
    public function isTrue(string $message = '')
179
    {
180 1
        $this->addRules('is_true', [VAR_MESSAGE => $message]);
181 1
        return $this;
182
    }
183
}