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 |
||
11 | class Field extends \PluginSimpleValidate\BaseAbstract\Field implements \PluginSimpleValidate\Contracts\Field |
||
12 | { |
||
13 | /** |
||
14 | * @var string |
||
15 | */ |
||
16 | private $name; |
||
17 | |||
18 | /** |
||
19 | * @var string |
||
20 | */ |
||
21 | private $value; |
||
22 | |||
23 | /** |
||
24 | * @var array |
||
25 | */ |
||
26 | private $errors; |
||
27 | |||
28 | /** |
||
29 | * @var array |
||
30 | * array of Rule |
||
31 | */ |
||
32 | private $rules = []; |
||
33 | |||
34 | /** |
||
35 | * @var bool |
||
36 | */ |
||
37 | private $status; |
||
38 | |||
39 | /** |
||
40 | * Field constructor. |
||
41 | * @param string $name |
||
42 | * @param mixed $value |
||
43 | */ |
||
44 | 7 | public function __construct(string $name, $value) |
|
50 | |||
51 | /** |
||
52 | * @param string $rulesMethod |
||
53 | * @param array $args |
||
54 | * @return $this |
||
55 | */ |
||
56 | 6 | protected function addRules(string $rulesMethod, array $args = []) |
|
61 | |||
62 | /** |
||
63 | * @param Language $language |
||
64 | * @param bool $break_when_error |
||
65 | * @return bool |
||
66 | */ |
||
67 | 6 | View Code Duplication | public function isValid(Language $language, $break_when_error = false) : bool |
90 | |||
91 | /** |
||
92 | * @return string |
||
93 | */ |
||
94 | 2 | public function getName(): string |
|
98 | |||
99 | /** |
||
100 | * @return mixed |
||
101 | */ |
||
102 | 1 | public function getValue() |
|
106 | |||
107 | /** |
||
108 | * @return array |
||
109 | */ |
||
110 | public function getRules(): array |
||
114 | |||
115 | /** |
||
116 | * @return array |
||
117 | */ |
||
118 | 6 | public function getErrors(): array |
|
122 | |||
123 | /** |
||
124 | * @return $this |
||
125 | */ |
||
126 | 3 | public function required() |
|
131 | |||
132 | /** |
||
133 | * @return $this |
||
134 | */ |
||
135 | 2 | public function validEmail() |
|
140 | |||
141 | /** |
||
142 | * @param int|float|double|string $match |
||
143 | * @return $this |
||
144 | */ |
||
145 | 1 | public function equal($match) |
|
150 | |||
151 | /** |
||
152 | * @param int|float|double $limit |
||
153 | * @return $this |
||
154 | */ |
||
155 | 1 | public function lessThan($limit) |
|
160 | |||
161 | /** |
||
162 | * @param int|float|double $lower |
||
163 | * @param int|float|double $upper |
||
164 | * @return $this |
||
165 | */ |
||
166 | 1 | public function between($lower, $upper) |
|
171 | } |
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.