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 |
||
15 | abstract class AbstractEndpointRepository implements |
||
16 | DatabaseAwareInterface, |
||
17 | RedisAwareInterface, |
||
18 | UuidAwareInterface |
||
19 | { |
||
20 | use DatabaseAwareTrait; |
||
21 | use RedisAwareTrait; |
||
22 | use UuidAwareTrait; |
||
23 | |||
24 | /** |
||
25 | * Determines the table that the DB is interfacing with |
||
26 | * |
||
27 | * @return string |
||
28 | */ |
||
29 | abstract public function getTable(); |
||
30 | |||
31 | /** |
||
32 | * Determines the primary key of the table |
||
33 | * |
||
34 | * @return string |
||
35 | */ |
||
36 | abstract public function getPrimaryKey(); |
||
37 | |||
38 | /** |
||
39 | * Determines the Result key of the table |
||
40 | * |
||
41 | * @return string |
||
42 | */ |
||
43 | abstract public function getResultKey(); |
||
44 | |||
45 | /** |
||
46 | * Builds a new query factory ready for use with the QueryObjects |
||
47 | * |
||
48 | * @return \Aura\SqlQuery\AbstractQuery |
||
49 | */ |
||
50 | public function newQuery() |
||
59 | |||
60 | public function fireStatementAndReturn($query, $single = false) |
||
70 | |||
71 | /** |
||
72 | * Reads a single record from the database |
||
73 | * |
||
74 | * @param string $id |
||
75 | * |
||
76 | * @return array |
||
77 | */ |
||
78 | View Code Duplication | public function readSingle($id, $keyType = 'primary') |
|
88 | |||
89 | /** |
||
90 | * Reads all related records from the database |
||
91 | * |
||
92 | * @param string $id |
||
93 | * |
||
94 | * @return array |
||
95 | */ |
||
96 | View Code Duplication | public function readAll($id, $keyType = 'primary') |
|
106 | |||
107 | public function returnKeyType($key) |
||
119 | } |
||
120 |
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.