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 |
||
7 | trait DatabaseTransactionsTrait |
||
8 | { |
||
9 | /** |
||
10 | * Creates a record in the database. |
||
11 | * |
||
12 | * @param string $table The table where the a new record is made. |
||
13 | * @param array $record The record to be made in the database. |
||
14 | * |
||
15 | * @return bool |
||
16 | */ |
||
17 | public function createRecord($table, $record) |
||
65 | |||
66 | /** |
||
67 | * Remove a record in the database. |
||
68 | * |
||
69 | * @param string $table The table where the record is removed in the database. |
||
70 | * @param string $pk The primary key value of the record. |
||
71 | * |
||
72 | * @return bool Returns boolean true if the record was successfully deleted or else it returns false. |
||
73 | */ |
||
74 | View Code Duplication | public function deleteRecord($table, $pk) |
|
83 | |||
84 | /** |
||
85 | * Returns a particular record in a table. |
||
86 | * |
||
87 | * @param string $table The table of the record. |
||
88 | * @param string $pk The primary key value of the record. |
||
89 | * |
||
90 | * @return array An array containing the particular record. |
||
91 | */ |
||
92 | View Code Duplication | public function findRecord($table, $pk) |
|
101 | |||
102 | /** |
||
103 | * Returns all the records in a table. |
||
104 | * |
||
105 | * @param string $table The table inspected for all its records. |
||
106 | * |
||
107 | * @return array All the records in the table. |
||
108 | */ |
||
109 | View Code Duplication | public function getAllRecords($table) |
|
117 | |||
118 | /** |
||
119 | * Update a record in the database. |
||
120 | * |
||
121 | * @param string $table The table where the record update is being made. |
||
122 | * @param string $pk The primary key value of the record to be updated. |
||
123 | * @param array $record The updates to be made to the record in the database. |
||
124 | * |
||
125 | * @return bool Returns boolean true if the record was successfully updated or else it returns false. |
||
126 | */ |
||
127 | public function updateRecord($table, $pk, $record) |
||
156 | } |
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.