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 Route implements DbDriverInterface |
||
12 | { |
||
13 | |||
14 | /** |
||
15 | * @var DbDriverInterface[] |
||
16 | */ |
||
17 | protected $dbDriverInterface = []; |
||
18 | |||
19 | /** |
||
20 | * @var string[] |
||
21 | */ |
||
22 | protected $routes; |
||
23 | |||
24 | /** |
||
25 | * Route constructor. |
||
26 | */ |
||
27 | 15 | public function __construct() |
|
30 | |||
31 | //<editor-fold desc="Route Methods"> |
||
32 | |||
33 | /** |
||
34 | * @param string $routeName |
||
35 | * @param DbDriverInterface[]|DbDriverInterface|string|string[] $dbDriver |
||
36 | * @return \ByJG\AnyDataset\Store\Route |
||
37 | */ |
||
38 | 15 | public function addDbDriverInterface($routeName, $dbDriver) |
|
54 | |||
55 | /** |
||
56 | * @param $routeName |
||
57 | * @param null $table |
||
58 | * @return \ByJG\AnyDataset\Store\Route |
||
59 | * @throws \ByJG\AnyDataset\Exception\RouteNotFoundException |
||
60 | */ |
||
61 | 5 | View Code Duplication | public function addRouteForSelect($routeName, $table = null) |
68 | |||
69 | /** |
||
70 | * @param $routeName |
||
71 | * @param null $table |
||
72 | * @return \ByJG\AnyDataset\Store\Route |
||
73 | * @throws \ByJG\AnyDataset\Exception\RouteNotFoundException |
||
74 | */ |
||
75 | 5 | View Code Duplication | public function addRouteForInsert($routeName, $table = null) |
82 | |||
83 | /** |
||
84 | * @param $routeName |
||
85 | * @param null $table |
||
86 | * @return \ByJG\AnyDataset\Store\Route |
||
87 | * @throws \ByJG\AnyDataset\Exception\RouteNotFoundException |
||
88 | */ |
||
89 | 5 | View Code Duplication | public function addRouteForUpdate($routeName, $table = null) |
96 | |||
97 | /** |
||
98 | * @param $routeName |
||
99 | * @param null $table |
||
100 | * @return \ByJG\AnyDataset\Store\Route |
||
101 | * @throws \ByJG\AnyDataset\Exception\RouteNotFoundException |
||
102 | */ |
||
103 | 5 | View Code Duplication | public function addRouteForDelete($routeName, $table = null) |
110 | |||
111 | /** |
||
112 | * @param $routeName |
||
113 | * @param $table |
||
114 | * @return \ByJG\AnyDataset\Store\Route |
||
115 | * @throws \ByJG\AnyDataset\Exception\RouteNotFoundException |
||
116 | */ |
||
117 | 1 | public function addRouteForTable($routeName, $table) |
|
123 | |||
124 | /** |
||
125 | * @param $routeName |
||
126 | * @param null $table |
||
127 | * @return \ByJG\AnyDataset\Store\Route |
||
128 | * @throws \ByJG\AnyDataset\Exception\RouteNotFoundException |
||
129 | */ |
||
130 | 3 | public function addRouteForWrite($routeName, $table = null) |
|
137 | |||
138 | /** |
||
139 | * @param $routeName |
||
140 | * @param null $table |
||
141 | * @return \ByJG\AnyDataset\Store\Route |
||
142 | * @throws \ByJG\AnyDataset\Exception\RouteNotFoundException |
||
143 | */ |
||
144 | 3 | public function addRouteForRead($routeName, $table = null) |
|
148 | |||
149 | /** |
||
150 | * @param $routeName |
||
151 | * @param $field |
||
152 | * @param $value |
||
153 | * @return \ByJG\AnyDataset\Store\Route |
||
154 | * @throws \ByJG\AnyDataset\Exception\RouteNotFoundException |
||
155 | */ |
||
156 | 2 | public function addRouteForFilter($routeName, $field, $value) |
|
160 | |||
161 | /** |
||
162 | * @param $routeName |
||
163 | * @return \ByJG\AnyDataset\Store\Route |
||
164 | * @throws \ByJG\AnyDataset\Exception\RouteNotFoundException |
||
165 | */ |
||
166 | 1 | public function addDefaultRoute($routeName) |
|
170 | |||
171 | /** |
||
172 | * @param $routeName |
||
173 | * @param $regEx |
||
174 | * @return \ByJG\AnyDataset\Store\Route |
||
175 | * @throws \ByJG\AnyDataset\Exception\RouteNotFoundException |
||
176 | */ |
||
177 | 13 | public function addCustomRoute($routeName, $regEx) |
|
185 | |||
186 | /** |
||
187 | * @param $sql |
||
188 | * @return DbDriverInterface |
||
189 | * @throws \ByJG\AnyDataset\Exception\RouteNotMatchedException |
||
190 | */ |
||
191 | 13 | public function matchRoute($sql) |
|
209 | //</editor-fold> |
||
210 | |||
211 | //<editor-fold desc="DbDriverInterface"> |
||
212 | |||
213 | /** |
||
214 | * @param string $sql |
||
215 | * @param null $params |
||
216 | * @return \ByJG\AnyDataset\Dataset\GenericIterator |
||
217 | * @throws \ByJG\AnyDataset\Exception\RouteNotMatchedException |
||
218 | */ |
||
219 | public function getIterator($sql, $params = null) |
||
224 | |||
225 | /** |
||
226 | * @param $sql |
||
227 | * @param null $array |
||
228 | * @return mixed |
||
229 | * @throws \ByJG\AnyDataset\Exception\RouteNotMatchedException |
||
230 | */ |
||
231 | public function getScalar($sql, $array = null) |
||
236 | |||
237 | /** |
||
238 | * @param $tablename |
||
239 | * @throws \ByJG\AnyDataset\Exception\NotImplementedException |
||
240 | */ |
||
241 | public function getAllFields($tablename) |
||
245 | |||
246 | /** |
||
247 | * @param $sql |
||
248 | * @param null $array |
||
249 | * @return mixed |
||
250 | * @throws \ByJG\AnyDataset\Exception\RouteNotMatchedException |
||
251 | */ |
||
252 | public function execute($sql, $array = null) |
||
257 | |||
258 | /** |
||
259 | * @throws \ByJG\AnyDataset\Exception\NotImplementedException |
||
260 | */ |
||
261 | public function beginTransaction() |
||
265 | |||
266 | /** |
||
267 | * @throws \ByJG\AnyDataset\Exception\NotImplementedException |
||
268 | */ |
||
269 | public function commitTransaction() |
||
273 | |||
274 | /** |
||
275 | * @throws \ByJG\AnyDataset\Exception\NotImplementedException |
||
276 | */ |
||
277 | public function rollbackTransaction() |
||
281 | |||
282 | /** |
||
283 | * @return \PDO|void |
||
284 | * @throws \ByJG\AnyDataset\Exception\NotImplementedException |
||
285 | */ |
||
286 | public function getDbConnection() |
||
290 | |||
291 | /** |
||
292 | * @param $name |
||
293 | * @param $value |
||
294 | * @throws \ByJG\AnyDataset\Exception\NotImplementedException |
||
295 | */ |
||
296 | public function setAttribute($name, $value) |
||
300 | |||
301 | /** |
||
302 | * @param $name |
||
303 | * @throws \ByJG\AnyDataset\Exception\NotImplementedException |
||
304 | */ |
||
305 | public function getAttribute($name) |
||
309 | |||
310 | /** |
||
311 | * @param $sql |
||
312 | * @param null $array |
||
313 | * @return mixed |
||
314 | * @throws \ByJG\AnyDataset\Exception\RouteNotMatchedException |
||
315 | */ |
||
316 | public function executeAndGetId($sql, $array = null) |
||
321 | |||
322 | /** |
||
323 | * @return \ByJG\AnyDataset\DbFunctionsInterface|void |
||
324 | * @throws \ByJG\AnyDataset\Exception\NotImplementedException |
||
325 | */ |
||
326 | public function getDbHelper() |
||
330 | |||
331 | /** |
||
332 | * @return void |
||
333 | * @throws \ByJG\AnyDataset\Exception\NotImplementedException |
||
334 | */ |
||
335 | public function getUri() |
||
339 | |||
340 | /** |
||
341 | * @throws \ByJG\AnyDataset\Exception\NotImplementedException |
||
342 | */ |
||
343 | public function isSupportMultRowset() |
||
347 | |||
348 | /** |
||
349 | * @param $multipleRowSet |
||
350 | * @throws \ByJG\AnyDataset\Exception\NotImplementedException |
||
351 | */ |
||
352 | public function setSupportMultRowset($multipleRowSet) |
||
356 | //</editor-fold> |
||
357 | } |
||
358 |
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.