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 |
||
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) |
|
62 | |||
63 | /** |
||
64 | * @return string |
||
65 | */ |
||
66 | 10 | public function getName() |
|
70 | |||
71 | /** |
||
72 | * @param string $name |
||
73 | * @return FieldAbstract |
||
74 | */ |
||
75 | 19 | public function setName($name) |
|
80 | |||
81 | /** |
||
82 | * @return string |
||
83 | */ |
||
84 | 11 | public function getId() |
|
88 | |||
89 | /** |
||
90 | * @param string $id |
||
91 | * @return FieldAbstract |
||
92 | */ |
||
93 | 8 | public function setId($id) |
|
98 | |||
99 | /** |
||
100 | * @return string |
||
101 | */ |
||
102 | 1 | public function getClass() |
|
106 | |||
107 | /** |
||
108 | * @param string $class |
||
109 | * @return FieldAbstract |
||
110 | */ |
||
111 | 5 | public function setClass($class) |
|
116 | |||
117 | /** |
||
118 | * @return mixed |
||
119 | */ |
||
120 | 15 | public function getValue() |
|
124 | |||
125 | /** |
||
126 | * @param mixed $value |
||
127 | * @return FieldAbstract |
||
128 | */ |
||
129 | 12 | public function setValue($value) |
|
135 | |||
136 | /** |
||
137 | * @param ValidatorInterface $validator |
||
138 | * @return $this |
||
139 | */ |
||
140 | 16 | public function addValidator(ValidatorInterface $validator) |
|
145 | |||
146 | /** |
||
147 | * @return ValidatorCollection |
||
148 | */ |
||
149 | 1 | public function getValidators() |
|
153 | |||
154 | /** |
||
155 | * @param FilterInterface $filter |
||
156 | * @return $this |
||
157 | */ |
||
158 | 16 | public function addFilter(FilterInterface $filter) |
|
163 | |||
164 | /** |
||
165 | * @return FilterCollection |
||
166 | */ |
||
167 | 1 | public function getFilters() |
|
171 | |||
172 | /** |
||
173 | * @return bool |
||
174 | * @throws Exception If validation of $value is impossible |
||
175 | */ |
||
176 | 11 | View Code Duplication | public function isValid() |
|
|||
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() |
|
219 | |||
220 | /** |
||
221 | * @return string |
||
222 | */ |
||
223 | 10 | public function getLabel() |
|
227 | |||
228 | /** |
||
229 | * @param string $label |
||
230 | * @return FieldAbstract |
||
231 | */ |
||
232 | 3 | public function setLabel($label) |
|
237 | |||
238 | /** |
||
239 | * @param string $message |
||
240 | * @return $this |
||
241 | */ |
||
242 | 2 | public function setCustomErrorMessage($message) |
|
247 | |||
248 | /** |
||
249 | * @return bool |
||
250 | */ |
||
251 | 3 | public function hasCustomErrorMessage() |
|
255 | |||
256 | /** |
||
257 | * @return string |
||
258 | */ |
||
259 | 2 | public function getCustomErrorMessage() |
|
263 | |||
264 | /** |
||
265 | * @return FieldRendererInterface |
||
266 | */ |
||
267 | 11 | public function getRenderer() |
|
271 | |||
272 | /** |
||
273 | * @param FieldRendererInterface $renderer |
||
274 | * @return $this |
||
275 | */ |
||
276 | 8 | public function setRenderer(FieldRendererInterface $renderer) |
|
281 | |||
282 | /** |
||
283 | * @return boolean |
||
284 | */ |
||
285 | 10 | public function isRequired() |
|
289 | |||
290 | /** |
||
291 | * @param boolean $required |
||
292 | * @return FieldAbstract |
||
293 | */ |
||
294 | 3 | public function setRequired($required) |
|
299 | } |
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.