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 |
||
14 | class Datatable19QueryParser extends QueryParser |
||
15 | { |
||
16 | |||
17 | /** |
||
18 | * Method to determine if this parser can handle the query parameters. If so then the parser should return true |
||
19 | * and be able to return a DTQueryConfiguration |
||
20 | * |
||
21 | * @param Request $request The current request, that should be investigated |
||
22 | * @return bool true if the parser is able to parse the query parameters and to return a DTQueryConfiguration |
||
23 | */ |
||
24 | public function canParse(Request $request) |
||
28 | |||
29 | /** |
||
30 | * Method that should parse the request and return a DTQueryConfiguration |
||
31 | * |
||
32 | * @param Request $request The current request that should be investigated |
||
33 | * @param ColumnConfiguration[] $columnConfiguration The configuration of the columns |
||
34 | * @return QueryConfiguration the configuration the provider can use to prepare the data |
||
35 | */ |
||
36 | View Code Duplication | public function parse(Request $request, array $columnConfiguration) |
|
57 | |||
58 | /** |
||
59 | * Helper function that will check if a variable is empty |
||
60 | * @param mixed $string |
||
61 | * @return bool true if empty, false otherwise |
||
62 | */ |
||
63 | private function isEmpty($string) |
||
67 | |||
68 | /** |
||
69 | * Helper function that will check if a variable has a value |
||
70 | * |
||
71 | * NOTE: (this is almost the opposite of isEmpty, but it is *not* the same) |
||
72 | * |
||
73 | * @param mixed $string |
||
74 | * @return bool true if empty, false otherwise |
||
75 | */ |
||
76 | private function hasValue($string) |
||
80 | |||
81 | /** |
||
82 | * @param ParameterBag $query |
||
83 | * @param QueryConfigurationBuilder $builder |
||
84 | */ |
||
85 | public function getDrawCall($query, $builder) |
||
91 | |||
92 | /** |
||
93 | * @param ParameterBag $query |
||
94 | * @param QueryConfigurationBuilder $builder |
||
95 | */ |
||
96 | public function getStart($query, $builder) |
||
102 | |||
103 | /** |
||
104 | * @param ParameterBag $query |
||
105 | * @param QueryConfigurationBuilder $builder |
||
106 | */ |
||
107 | public function getLength($query, $builder) |
||
113 | |||
114 | /** |
||
115 | * @param ParameterBag $query |
||
116 | * @param QueryConfigurationBuilder $builder |
||
117 | */ |
||
118 | public function getSearch($query, $builder) |
||
124 | |||
125 | /** |
||
126 | * @param ParameterBag $query |
||
127 | * @param QueryConfigurationBuilder $builder |
||
128 | * @param ColumnConfiguration[] $columnConfiguration |
||
129 | */ |
||
130 | public function getSearchColumns($query, $builder, array $columnConfiguration) |
||
141 | |||
142 | /** |
||
143 | * @param ParameterBag $query |
||
144 | * @param QueryConfigurationBuilder $builder |
||
145 | * @param ColumnConfiguration[] $columnConfiguration |
||
146 | * @throws DatatableException when a column for sorting is out of bounds |
||
147 | * @return bool success? |
||
148 | */ |
||
149 | private function determineSortableColumns($query, $builder, array $columnConfiguration) |
||
164 | |||
165 | /** |
||
166 | * Find out how many columns we are sorting by for the sorting loop |
||
167 | * @see determineSortableColumns |
||
168 | * @param ParameterBag $query |
||
169 | * @return int |
||
170 | */ |
||
171 | private function getNumberOfSortingColumns(ParameterBag $query) |
||
178 | |||
179 | /** |
||
180 | * Add a column for ordering to the QueryConfigurationBuilder |
||
181 | * @see determineSortableColumns |
||
182 | * @param $builder |
||
183 | * @param $columnConfiguration |
||
184 | * @param $item |
||
185 | * @param $direction |
||
186 | * @throws DatatableException |
||
187 | */ |
||
188 | private function addColumnForOrdering($builder, $columnConfiguration, $item, $direction) |
||
196 | |||
197 | /** |
||
198 | * @param ColumnConfiguration[] $columnConfiguration |
||
199 | * @param int $item |
||
200 | * @return ColumnConfiguration a specific item from $ColumnConfiguration |
||
201 | * @throws DatatableException |
||
202 | */ |
||
203 | private function getColumnFromConfiguration(array $columnConfiguration, $item) |
||
213 | |||
214 | /** |
||
215 | * @param ParameterBag $query |
||
216 | * @param QueryConfigurationBuilder $builder |
||
217 | */ |
||
218 | public function getRegex($query, $builder) |
||
224 | } |
||
225 |
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.