Completed
Branch dev-master (ae1555)
by Derek Stephen
02:21
created

FieldAbstract   B

Complexity

Total Complexity 37

Size/Duplication

Total Lines 304
Duplicated Lines 3.62 %

Coupling/Cohesion

Components 2
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 11
loc 304
c 0
b 0
f 0
wmc 37
lcom 2
cbo 7
ccs 103
cts 103
cp 1
rs 8.6

30 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 isValid() 11 11 2
A checkForErrors() 0 8 3
A filterValue() 0 11 2
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 6 2
A addNotEmptyValidator() 0 5 1
A removeNotEmptyValidator() 0 11 3

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