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 Validation |
||
17 | { |
||
18 | use FilterCheck; |
||
19 | use FilterValidation; |
||
20 | |||
21 | /** |
||
22 | * Mock for enabled filters. |
||
23 | * |
||
24 | * @var array |
||
25 | */ |
||
26 | protected $enabledFilters = []; |
||
27 | |||
28 | /** |
||
29 | * Validation constructor. |
||
30 | * |
||
31 | * @param array $filters |
||
32 | */ |
||
33 | public function __construct($filters = []) |
||
39 | |||
40 | /** |
||
41 | * Check validation for $input |
||
42 | * |
||
43 | * @param string $type |
||
44 | * @param string|array $input |
||
45 | * |
||
46 | * @return bool |
||
47 | */ |
||
48 | public function validation($type = "", $input) |
||
65 | |||
66 | /** |
||
67 | * Validation for FIELD_LIST parameter. |
||
68 | * |
||
69 | * @param string|array $input |
||
70 | * |
||
71 | * @return bool |
||
72 | */ |
||
73 | protected function validFieldList($input) |
||
87 | |||
88 | /** |
||
89 | * Validation for LIMIT parameter. |
||
90 | * |
||
91 | * @param string|array $input |
||
92 | * |
||
93 | * @return bool |
||
94 | */ |
||
95 | View Code Duplication | protected function validLimit($input) |
|
103 | |||
104 | /** |
||
105 | * Validation for OFFSET parameter. |
||
106 | * |
||
107 | * @param string|array $input |
||
108 | * |
||
109 | * @return bool |
||
110 | */ |
||
111 | View Code Duplication | protected function validOffset($input) |
|
119 | |||
120 | /** |
||
121 | * Validation for FILTER parameter. |
||
122 | * |
||
123 | * @param string|array $input |
||
124 | * |
||
125 | * @return bool |
||
126 | */ |
||
127 | protected function validFilter($input) |
||
141 | |||
142 | /** |
||
143 | * Validation for SORT parameter. |
||
144 | * |
||
145 | * @param string|array $input |
||
146 | * |
||
147 | * @return bool |
||
148 | */ |
||
149 | protected function validSort($input) |
||
166 | } |
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.