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 |
||
18 | abstract class AbstractActiveRecord implements ActiveRecordInterface |
||
19 | { |
||
20 | /** @var \PDO The PDO object. */ |
||
21 | private $pdo; |
||
22 | |||
23 | /** @var null|int The ID. */ |
||
24 | private $id; |
||
25 | |||
26 | /** |
||
27 | * Construct an abstract pdo active record with the given pdo. |
||
28 | * |
||
29 | * @param \PDO $pdo |
||
30 | */ |
||
31 | 21 | public function __construct(\PDO $pdo) |
|
38 | |||
39 | /** |
||
40 | * {@inheritdoc} |
||
41 | */ |
||
42 | 3 | public function create() |
|
55 | |||
56 | /** |
||
57 | * Returns the create query. |
||
58 | * |
||
59 | * @return string the create query. |
||
60 | */ |
||
61 | 3 | View Code Duplication | private function getCreateQuery() |
72 | |||
73 | /** |
||
74 | * {@inheritdoc} |
||
75 | */ |
||
76 | 6 | public function read($id) |
|
90 | |||
91 | /** |
||
92 | * Returns the read query. |
||
93 | * |
||
94 | * @return string the read query. |
||
95 | */ |
||
96 | 6 | private function getReadQuery() |
|
100 | |||
101 | /** |
||
102 | * {@inheritdoc} |
||
103 | */ |
||
104 | 4 | View Code Duplication | public function update() |
119 | |||
120 | /** |
||
121 | * Returns the update query. |
||
122 | * |
||
123 | * @return string the update query. |
||
124 | */ |
||
125 | 3 | View Code Duplication | private function getUpdateQuery() |
135 | |||
136 | /** |
||
137 | * {@inheritdoc} |
||
138 | */ |
||
139 | 3 | View Code Duplication | public function delete() |
156 | |||
157 | /** |
||
158 | * Returns the delete query. |
||
159 | * |
||
160 | * @return string the delete query. |
||
161 | */ |
||
162 | 2 | private function getDeleteQuery() |
|
166 | |||
167 | /** |
||
168 | * {@inheritdoc} |
||
169 | */ |
||
170 | 8 | public function exists() |
|
174 | |||
175 | /** |
||
176 | * Returns the PDO. |
||
177 | * |
||
178 | * @return \PDO the PDO. |
||
179 | */ |
||
180 | 19 | public function getPdo() |
|
184 | |||
185 | /** |
||
186 | * Set the PDO. |
||
187 | * |
||
188 | * @param \PDO $pdo |
||
189 | * @return $this |
||
190 | */ |
||
191 | 21 | protected function setPdo($pdo) |
|
197 | |||
198 | /** |
||
199 | * Returns the ID. |
||
200 | * |
||
201 | * @return null|int The ID. |
||
202 | */ |
||
203 | 6 | public function getId() |
|
207 | |||
208 | /** |
||
209 | * Set the ID. |
||
210 | * |
||
211 | * @param int $id |
||
212 | * @return $this |
||
213 | */ |
||
214 | 8 | protected function setId($id) |
|
220 | |||
221 | /** |
||
222 | * Returns the active record name. |
||
223 | * |
||
224 | * @return string the active record name. |
||
225 | */ |
||
226 | abstract protected function getActiveRecordName(); |
||
227 | |||
228 | /** |
||
229 | * Returns the active record data. |
||
230 | * |
||
231 | * @return array the active record data. |
||
232 | */ |
||
233 | abstract protected function getActiveRecordData(); |
||
234 | |||
235 | /** |
||
236 | * Set the active record data. |
||
237 | * |
||
238 | * @param array $fetch |
||
239 | * @return null |
||
240 | */ |
||
241 | 8 | protected function setActiveRecordData(array $fetch) |
|
253 | } |
||
254 |
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.