Completed
Branch dev-master (f3e1e5)
by Derek Stephen
20:25
created

FieldAbstract   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 280
Duplicated Lines 3.93 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 31
lcom 2
cbo 6
dl 11
loc 280
ccs 86
cts 86
cp 1
rs 9.8
c 0
b 0
f 0

28 Methods

Rating   Name   Duplication   Size   Complexity  
getTag() 0 1 ?
init() 0 1 ?
A __construct() 0 10 2
A getName() 0 4 1
A setName() 0 5 1
A getId() 0 4 1
A setId() 0 5 1
A getClass() 0 4 2
A setClass() 0 5 1
A getValue() 0 4 1
A setValue() 0 6 1
A addValidator() 0 5 1
A getValidators() 0 4 1
A addFilter() 0 5 1
A getFilters() 0 4 1
A getMessages() 0 4 1
A getLabel() 0 4 1
A setLabel() 0 5 1
A setCustomErrorMessage() 0 5 1
A hasCustomErrorMessage() 0 4 1
A getCustomErrorMessage() 0 4 1
A getRenderer() 0 4 1
A setRenderer() 0 5 1
A isRequired() 0 4 1
A setRequired() 0 5 1
A isValid() 11 11 2
A checkForErrors() 0 8 2
A filterValue() 0 11 2

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
 * User: delboy1978uk
4
 * Date: 19/11/2016
5
 * Time: 21:41
6
 */
7
8
namespace Del\Form\Field;
9
10
use Del\Form\Collection\FilterCollection;
11
use Del\Form\Collection\ValidatorCollection;
12
use Del\Form\Filter\FilterInterface;
13
use Del\Form\Renderer\Field\DefaultRender;
14
use Del\Form\Renderer\Field\FieldRendererInterface;
15
use Del\Form\Renderer\Field\TextRender;
16
use Del\Form\Traits\HasAttributesTrait;
17
use Del\Form\Validator\ValidatorInterface;
18
use Exception;
19
20
abstract class FieldAbstract implements FieldInterface
21
{
22
    /**  @var FilterCollection $filterCollection */
23
    private $filterCollection;
24
25
    /**  @var ValidatorCollection $validatorCollection */
26
    private $validatorCollection;
27
28
    /** @var FieldRendererInterface $renderer  */
29
    private $renderer;
30
31
    /** @var array $errorMessages */
32
    private $errorMessages;
33
34
    /** @var string $customErrorMessage */
35
    private $customErrorMessage;
36
37
    /** @var string $label */
38
    private $label;
39
40
    /** @var bool $required */
41
    private $required;
42
43
    use HasAttributesTrait;
44
45
    /**
46
     * @return string
47
     */
48
    abstract public function getTag();
49
50
    abstract public function init();
51
52 19
    public function __construct($name, $value = null)
53
    {
54 19
        $this->required = false;
55 19
        $this->filterCollection = new FilterCollection();
56 19
        $this->validatorCollection = new ValidatorCollection();
57 19
        $this->renderer = new TextRender();
58 19
        $this->setName($name);
59 19
        is_null($value) ? null : $this->setValue($value);
60 19
        $this->init();
61 19
    }
62
63
    /**
64
     * @return string
65
     */
66 10
    public function getName()
67
    {
68 10
        return $this->getAttribute('name');
69
    }
70
71
    /**
72
     * @param string $name
73
     * @return FieldAbstract
74
     */
75 19
    public function setName($name)
76
    {
77 19
        $this->setAttribute('name', $name);
78 19
        return $this;
79
    }
80
81
    /**
82
     * @return string
83
     */
84 11
    public function getId()
85
    {
86 11
        return $this->getAttribute('id');
87
    }
88
89
    /**
90
     * @param string $id
91
     * @return FieldAbstract
92
     */
93 8
    public function setId($id)
94
    {
95 8
        $this->setAttribute('id', $id);
96 8
        return $this;
97
    }
98
99
    /**
100
     * @return string
101
     */
102 1
    public function getClass()
103
    {
104 1
        return $this->getAttribute('class') ?: 'form-control';
105
    }
106
107
    /**
108
     * @param string $class
109
     * @return FieldAbstract
110
     */
111 5
    public function setClass($class)
112
    {
113 5
        $this->setAttribute('class', $class);
114 5
        return $this;
115
    }
116
117
    /**
118
     * @return mixed
119
     */
120 15
    public function getValue()
121
    {
122 15
        return $this->getAttribute('value');
123
    }
124
125
    /**
126
     * @param mixed $value
127
     * @return FieldAbstract
128
     */
129 12
    public function setValue($value)
130
    {
131 12
        $this->setAttribute('value', $value);
132 12
        $this->filterValue();
133 12
        return $this;
134
    }
135
136
    /**
137
     * @param ValidatorInterface $validator
138
     * @return $this
139
     */
140 16
    public function addValidator(ValidatorInterface $validator)
141
    {
142 16
        $this->validatorCollection->append($validator);
143 16
        return $this;
144
    }
145
146
    /**
147
     * @return ValidatorCollection
148
     */
149 1
    public function getValidators()
150
    {
151 1
        return $this->validatorCollection;
152
    }
153
154
    /**
155
     * @param FilterInterface $filter
156
     * @return $this
157
     */
158 16
    public function addFilter(FilterInterface $filter)
159
    {
160 16
        $this->filterCollection->append($filter);
161 16
        return $this;
162
    }
163
164
    /**
165
     * @return FilterCollection
166
     */
167 1
    public function getFilters()
168
    {
169 1
        return $this->filterCollection;
170
    }
171
172
    /**
173
     * @return bool
174
     * @throws Exception If validation of $value is impossible
175
     */
176 11 View Code Duplication
    public function isValid()
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...
177
    {
178 11
        $this->errorMessages = [];
179 11
        $this->validatorCollection->rewind();
180 11
        while ($this->validatorCollection->valid()) {
181 8
            $this->checkForErrors($this->validatorCollection->current());
182 8
            $this->validatorCollection->next();
183
        }
184 11
        $count = count($this->errorMessages);
185 11
        return $count == 0;
186
    }
187
188
    /**
189
     * @param ValidatorInterface $validator
190
     */
191 8
    private function checkForErrors(ValidatorInterface $validator)
192
    {
193 8
        $value = $this->getValue();
194
195 8
        if (!$validator->isValid($value)) {
196 7
            $this->errorMessages = array_merge($this->errorMessages, $validator->getMessages());
197
        }
198 8
    }
199
200 12
    private function filterValue()
201
    {
202 12
        $value = $this->getAttribute('value');
203 12
        $this->filterCollection->rewind();
204 12
        while ($this->filterCollection->valid()) {
205 4
            $value = $this->filterCollection->current()->filter($value);
206 4
            $this->filterCollection->next();
207
        }
208 12
        $this->filterCollection->rewind();
209 12
        $this->setAttribute('value', $value);
210 12
    }
211
212
    /**
213
     * @return array
214
     */
215 3
    public function getMessages()
216
    {
217 3
        return array_values($this->errorMessages);
218
    }
219
220
    /**
221
     * @return string
222
     */
223 10
    public function getLabel()
224
    {
225 10
        return $this->label;
226
    }
227
228
    /**
229
     * @param string $label
230
     * @return FieldAbstract
231
     */
232 3
    public function setLabel($label)
233
    {
234 3
        $this->label = $label;
235 3
        return $this;
236
    }
237
238
    /**
239
     * @param string $message
240
     * @return $this
241
     */
242 2
    public function setCustomErrorMessage($message)
243
    {
244 2
        $this->customErrorMessage = $message;
245 2
        return $this;
246
    }
247
248
    /**
249
     * @return bool
250
     */
251 3
    public function hasCustomErrorMessage()
252
    {
253 3
        return $this->customErrorMessage != null;
254
    }
255
256
    /**
257
     * @return string
258
     */
259 2
    public function getCustomErrorMessage()
260
    {
261 2
        return $this->customErrorMessage;
262
    }
263
264
    /**
265
     * @return FieldRendererInterface
266
     */
267 11
    public function getRenderer()
268
    {
269 11
        return $this->renderer;
270
    }
271
272
    /**
273
     * @param FieldRendererInterface $renderer
274
     * @return $this
275
     */
276 8
    public function setRenderer(FieldRendererInterface $renderer)
277
    {
278 8
        $this->renderer = $renderer;
279 8
        return $this;
280
    }
281
282
    /**
283
     * @return boolean
284
     */
285 10
    public function isRequired()
286
    {
287 10
        return $this->required;
288
    }
289
290
    /**
291
     * @param boolean $required
292
     * @return FieldAbstract
293
     */
294 3
    public function setRequired($required)
295
    {
296 3
        $this->required = $required;
297 3
        return $this;
298
    }
299
}