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 |
||
16 | class Field extends \PluginSimpleValidate\BaseAbstract\Field implements MultiValuesField |
||
17 | { |
||
18 | /** |
||
19 | * @param Language $language |
||
20 | * @return bool |
||
21 | */ |
||
22 | 3 | View Code Duplication | public function isValid(Language $language) : bool |
41 | |||
42 | /** |
||
43 | * @param string $rulesMethod |
||
44 | * @param mixed $value |
||
45 | * @param array $args |
||
46 | * @return $this |
||
47 | */ |
||
48 | 3 | public function addRules(string $rulesMethod, $value, array $args = []) |
|
53 | |||
54 | /** |
||
55 | * Field constructor. |
||
56 | * @param string $name |
||
57 | */ |
||
58 | 4 | public function __construct(string $name) |
|
62 | |||
63 | /** |
||
64 | * @param $value |
||
65 | * @param string $message |
||
66 | * @return $this |
||
67 | */ |
||
68 | 2 | public function isTrue($value, string $message = '') |
|
73 | |||
74 | /** |
||
75 | * @param mixed $value |
||
76 | * @return $this |
||
77 | */ |
||
78 | 1 | public function required($value) |
|
83 | |||
84 | /** |
||
85 | * @param mixed $value |
||
86 | * @return $this |
||
87 | */ |
||
88 | public function notEmpty($value) |
||
93 | |||
94 | /** |
||
95 | * @param mixed $value |
||
96 | * @return $this |
||
97 | */ |
||
98 | 1 | public function validEmail($value) |
|
103 | |||
104 | /** |
||
105 | * @param mixed $value |
||
106 | * @param float|int|string $match |
||
107 | * @return $this |
||
108 | */ |
||
109 | public function equal($value, $match) |
||
114 | |||
115 | /** |
||
116 | * @param mixed $value |
||
117 | * @return $this |
||
118 | */ |
||
119 | public function isNumber($value) |
||
124 | |||
125 | /** |
||
126 | * @param mixed $value |
||
127 | * @return $this |
||
128 | */ |
||
129 | public function isAlpha($value) |
||
134 | |||
135 | /** |
||
136 | * @param mixed $value |
||
137 | * @return $this |
||
138 | */ |
||
139 | public function isAlphaOrNumeric($value) |
||
144 | |||
145 | /** |
||
146 | * @param mixed $value |
||
147 | * @return $this |
||
148 | */ |
||
149 | public function isDecimal($value) |
||
154 | |||
155 | /** |
||
156 | * @param mixed $value |
||
157 | * @return $this |
||
158 | */ |
||
159 | public function isInteger($value) |
||
164 | |||
165 | /** |
||
166 | * @param mixed $value |
||
167 | * @return $this |
||
168 | */ |
||
169 | public function isNatural($value) |
||
174 | |||
175 | /** |
||
176 | * @param mixed $value |
||
177 | * @return $this |
||
178 | */ |
||
179 | public function isNaturalNoZero($value) |
||
184 | |||
185 | /** |
||
186 | * @param mixed $value |
||
187 | * @param float|int $limit |
||
188 | * @return $this |
||
189 | */ |
||
190 | public function lessThan($value, $limit) |
||
195 | |||
196 | /** |
||
197 | * @param mixed $value |
||
198 | * @param float|int $limit |
||
199 | * @return $this |
||
200 | */ |
||
201 | public function greaterThan($value, $limit) |
||
206 | |||
207 | /** |
||
208 | * @param mixed $value |
||
209 | * @param float|int $limit |
||
210 | * @return $this |
||
211 | */ |
||
212 | public function lessOrEqualThan($value, $limit) |
||
217 | |||
218 | /** |
||
219 | * @param mixed $value |
||
220 | * @param float|int $limit |
||
221 | * @return $this |
||
222 | */ |
||
223 | public function greaterOrEqualThan($value, $limit) |
||
228 | |||
229 | /** |
||
230 | * @param mixed $value |
||
231 | * @param float|int $lower |
||
232 | * @param float|int $upper |
||
233 | * @return $this |
||
234 | */ |
||
235 | public function between($value, $lower, $upper) |
||
240 | |||
241 | /** |
||
242 | * @param mixed $value |
||
243 | * @param float|int $lower |
||
244 | * @param float|int $upper |
||
245 | * @return $this |
||
246 | */ |
||
247 | public function betweenOrEqual($value, $lower, $upper) |
||
252 | |||
253 | /** |
||
254 | * @param mixed $value |
||
255 | * @param float|int $limit |
||
256 | * @return $this |
||
257 | */ |
||
258 | public function length($value, $limit) |
||
263 | |||
264 | /** |
||
265 | * @param mixed $value |
||
266 | * @param float|int $limit |
||
267 | * @return $this |
||
268 | */ |
||
269 | public function lengthLessThan($value, $limit) |
||
274 | |||
275 | /** |
||
276 | * @param mixed $value |
||
277 | * @param float|int $limit |
||
278 | * @return $this |
||
279 | */ |
||
280 | public function lengthGreaterThan($value, $limit) |
||
285 | |||
286 | /** |
||
287 | * @param mixed $value |
||
288 | * @param float|int $limit |
||
289 | * @return $this |
||
290 | */ |
||
291 | public function lengthLessOrEqualThan($value, $limit) |
||
296 | |||
297 | /** |
||
298 | * @param mixed $value |
||
299 | * @param float|int $limit |
||
300 | * @return $this |
||
301 | */ |
||
302 | public function lengthGreaterOrEqualThan($value, $limit) |
||
307 | |||
308 | /** |
||
309 | * @param mixed $value |
||
310 | * @param float|int $lower |
||
311 | * @param float|int $upper |
||
312 | * @return $this |
||
313 | */ |
||
314 | public function lengthBetween($value, $lower, $upper) |
||
319 | |||
320 | /** |
||
321 | * @param mixed $value |
||
322 | * @param float|int $lower |
||
323 | * @param float|int $upper |
||
324 | * @return $this |
||
325 | */ |
||
326 | 1 | public function lengthBetweenOrEqual($value, $lower, $upper) |
|
331 | |||
332 | /** |
||
333 | * @param mixed $value |
||
334 | * @param string $region |
||
335 | * @return $this |
||
336 | */ |
||
337 | public function isValidPhone($value, string $region) |
||
342 | } |
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.