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 |
||
8 | class Field extends \PluginSimpleValidate\BaseAbstract\Field |
||
9 | { |
||
10 | /** |
||
11 | * @var string |
||
12 | */ |
||
13 | private $name; |
||
14 | |||
15 | /** |
||
16 | * @var string |
||
17 | */ |
||
18 | private $value; |
||
19 | |||
20 | /** |
||
21 | * @var array |
||
22 | */ |
||
23 | private $errors; |
||
24 | |||
25 | /** |
||
26 | * @var array |
||
27 | * array of Rule |
||
28 | */ |
||
29 | private $rules = []; |
||
30 | |||
31 | /** |
||
32 | * @var bool |
||
33 | */ |
||
34 | private $status; |
||
35 | |||
36 | /** |
||
37 | * Field constructor. |
||
38 | * @param string $name |
||
39 | * @param mixed $value |
||
40 | */ |
||
41 | 9 | public function __construct(string $name, $value) |
|
47 | |||
48 | /** |
||
49 | * @param string $rulesMethod |
||
50 | * @param array $args |
||
51 | * @return $this |
||
52 | */ |
||
53 | 8 | protected function addRules(string $rulesMethod, array $args = []) |
|
58 | |||
59 | /** |
||
60 | * @param Language $language |
||
61 | * @return bool |
||
62 | */ |
||
63 | 8 | View Code Duplication | public function isValid(Language $language) : bool |
79 | |||
80 | /** |
||
81 | * @return string |
||
82 | */ |
||
83 | 3 | public function getName(): string |
|
87 | |||
88 | /** |
||
89 | * @return mixed |
||
90 | */ |
||
91 | 1 | public function getValue() |
|
95 | |||
96 | /** |
||
97 | * @return array |
||
98 | */ |
||
99 | public function getRules(): array |
||
103 | |||
104 | /** |
||
105 | * @return array |
||
106 | */ |
||
107 | 8 | public function getErrors(): array |
|
111 | |||
112 | /** |
||
113 | * @return $this |
||
114 | */ |
||
115 | 4 | public function required() |
|
120 | |||
121 | /** |
||
122 | * @return $this |
||
123 | */ |
||
124 | 3 | public function validEmail() |
|
129 | |||
130 | /** |
||
131 | * @param int|float|double|string $match |
||
132 | * @return $this |
||
133 | */ |
||
134 | 1 | public function equal($match) |
|
139 | |||
140 | /** |
||
141 | * @param string $message |
||
142 | * @return $this |
||
143 | */ |
||
144 | 1 | public function isTrue(string $message = '') |
|
149 | |||
150 | /** |
||
151 | * @return \PluginSimpleValidate\Contracts\Field |
||
152 | */ |
||
153 | public function isNumber() |
||
158 | |||
159 | /** |
||
160 | * @return \PluginSimpleValidate\Contracts\Field |
||
161 | */ |
||
162 | public function isAlpha() |
||
167 | |||
168 | /** |
||
169 | * @return \PluginSimpleValidate\Contracts\Field |
||
170 | */ |
||
171 | 2 | public function isAlphaOrNumeric() |
|
176 | |||
177 | /** |
||
178 | * @return \PluginSimpleValidate\Contracts\Field |
||
179 | */ |
||
180 | public function isDecimal() |
||
185 | |||
186 | /** |
||
187 | * @return \PluginSimpleValidate\Contracts\Field |
||
188 | */ |
||
189 | public function isInteger() |
||
194 | |||
195 | /** |
||
196 | * @return \PluginSimpleValidate\Contracts\Field |
||
197 | */ |
||
198 | public function isNatural() |
||
203 | |||
204 | /** |
||
205 | * @return \PluginSimpleValidate\Contracts\Field |
||
206 | */ |
||
207 | public function isNaturalNoZero() |
||
212 | |||
213 | /** |
||
214 | * @param int|float|double $limit |
||
215 | * @return $this |
||
216 | */ |
||
217 | 1 | public function lessThan($limit) |
|
222 | |||
223 | /** |
||
224 | * @param int|float|double $limit |
||
225 | * @return \PluginSimpleValidate\Contracts\Field |
||
226 | */ |
||
227 | public function greaterThan($limit) |
||
232 | |||
233 | /** |
||
234 | * @param int|float|double $limit |
||
235 | * @return \PluginSimpleValidate\Contracts\Field |
||
236 | */ |
||
237 | public function lessOrEqualThan($limit) |
||
242 | |||
243 | /** |
||
244 | * @param int|float|double $limit |
||
245 | * @return \PluginSimpleValidate\Contracts\Field |
||
246 | */ |
||
247 | public function greaterOrEqualThan($limit) |
||
252 | |||
253 | /** |
||
254 | * @param int|float|double $lower |
||
255 | * @param int|float|double $upper |
||
256 | * @return $this |
||
257 | */ |
||
258 | 1 | public function between($lower, $upper) |
|
263 | |||
264 | /** |
||
265 | * @param int|float|double $lower |
||
266 | * @param int|float|double $upper |
||
267 | * @return \PluginSimpleValidate\Contracts\Field |
||
268 | */ |
||
269 | public function betweenOrEqual($lower, $upper) |
||
274 | |||
275 | /** |
||
276 | * @param int|float|double $limit |
||
277 | * @return \PluginSimpleValidate\Contracts\Field |
||
278 | */ |
||
279 | public function length($limit) |
||
284 | |||
285 | /** |
||
286 | * @param int|float|double $limit |
||
287 | * @return \PluginSimpleValidate\Contracts\Field |
||
288 | */ |
||
289 | public function lengthLessThan($limit) |
||
294 | |||
295 | /** |
||
296 | * @param int|float|double $limit |
||
297 | * @return \PluginSimpleValidate\Contracts\Field |
||
298 | */ |
||
299 | 2 | public function lengthGreaterThan($limit) |
|
304 | |||
305 | /** |
||
306 | * @param int|float|double $limit |
||
307 | * @return \PluginSimpleValidate\Contracts\Field |
||
308 | */ |
||
309 | public function lengthLessOrEqualThan($limit) |
||
314 | |||
315 | /** |
||
316 | * @param int|float|double $limit |
||
317 | * @return \PluginSimpleValidate\Contracts\Field |
||
318 | */ |
||
319 | public function lengthGreaterOrEqualThan($limit) |
||
324 | |||
325 | /** |
||
326 | * @param int|float|double $lower |
||
327 | * @param int|float|double $upper |
||
328 | * @return \PluginSimpleValidate\Contracts\Field |
||
329 | */ |
||
330 | public function lengthBetween($lower, $upper) |
||
335 | |||
336 | /** |
||
337 | * @param int|float|double $lower |
||
338 | * @param int|float|double $upper |
||
339 | * @return \PluginSimpleValidate\Contracts\Field |
||
340 | */ |
||
341 | public function lengthBetweenOrEqual($lower, $upper) |
||
346 | } |
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.