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 | trait FilterValidation |
||
12 | { |
||
13 | /** |
||
14 | * Check if input is integer and stay |
||
15 | * in range between $min and $max. |
||
16 | * |
||
17 | * @param mixed $input Input field |
||
18 | * @param int $min Min range of input |
||
19 | * @param int|string $max Max range of input |
||
20 | * |
||
21 | * @return bool |
||
22 | */ |
||
23 | public function isIntAndBetween($input, $min, $max = "") |
||
37 | |||
38 | /** |
||
39 | * Check if key or value is not of any described types. |
||
40 | * |
||
41 | * @param string $key Value of key |
||
42 | * @param string $keyParams Not-Types of key |
||
43 | * @param string $value Value of value |
||
44 | * @param string|array $valueParams Not-Types of value |
||
45 | * |
||
46 | * @return bool |
||
47 | */ |
||
48 | public function isKeyAndValueAre($key, $keyParams, $value, $valueParams) |
||
68 | |||
69 | /** |
||
70 | * Check if value is any of these types. |
||
71 | * |
||
72 | * @param string $param Value to check |
||
73 | * @param array $types List of types |
||
74 | * |
||
75 | * @return bool |
||
76 | */ |
||
77 | public function isParamOfTypeMultiple($param, $types) |
||
87 | |||
88 | /** |
||
89 | * Check if value is not that type. |
||
90 | * |
||
91 | * @param string $param Value |
||
92 | * @param string $type Type |
||
93 | * |
||
94 | * @return bool |
||
95 | */ |
||
96 | public function isParamOfTypeSingle($param, $type) |
||
100 | } |
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.