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 |
||
20 | abstract class AbstractActiveRecord implements ActiveRecordInterface |
||
21 | { |
||
22 | /** @var \PDO The PDO object. */ |
||
23 | private $pdo; |
||
24 | |||
25 | /** @var null|int The ID. */ |
||
26 | private $id; |
||
27 | |||
28 | /** |
||
29 | * Construct an abstract pdo active record with the given pdo. |
||
30 | * |
||
31 | * @param \PDO $pdo |
||
32 | */ |
||
33 | 31 | public function __construct(\PDO $pdo) |
|
40 | |||
41 | /** |
||
42 | * {@inheritdoc} |
||
43 | */ |
||
44 | 4 | public function create() |
|
58 | |||
59 | /** |
||
60 | * {@inheritdoc} |
||
61 | */ |
||
62 | 9 | public function read($id) |
|
83 | |||
84 | /** |
||
85 | * {@inheritdoc} |
||
86 | */ |
||
87 | 5 | View Code Duplication | public function update() |
104 | |||
105 | /** |
||
106 | * {@inheritdoc} |
||
107 | */ |
||
108 | 4 | View Code Duplication | public function delete() |
127 | |||
128 | /** |
||
129 | * {@inheritdoc} |
||
130 | */ |
||
131 | 2 | public function sync() |
|
139 | |||
140 | /** |
||
141 | * {@inheritdoc} |
||
142 | */ |
||
143 | 10 | public function exists() |
|
147 | |||
148 | /** |
||
149 | * Fill the active record |
||
150 | * |
||
151 | * @param array $fetch |
||
152 | * @return null |
||
153 | */ |
||
154 | 17 | public function fill(array $fetch) |
|
166 | |||
167 | /** |
||
168 | * {@inheritdoc} |
||
169 | */ |
||
170 | 28 | public function searchOne(array $where = [], array $orderBy = []) |
|
187 | |||
188 | /** |
||
189 | * {@inheritdoc} |
||
190 | */ |
||
191 | 10 | public function search(array $where = [], array $orderBy = [], $limit = -1, $offset = 0) |
|
211 | |||
212 | /** |
||
213 | * Returns the search query result with the given where, order by, limit and offset clauses. |
||
214 | * |
||
215 | * @param array $where = [] |
||
216 | * @param array $orderBy = [] |
||
217 | * @param int $limit = -1 |
||
218 | * @param int $offset = 0 |
||
219 | * @return \miBadger\Query\QueryResult the search query result with the given where, order by, limit and offset clauses. |
||
220 | */ |
||
221 | 13 | private function getSearchQueryResult(array $where = [], array $orderBy = [], $limit = -1, $offset = 0) |
|
232 | |||
233 | /** |
||
234 | * Returns the given query after adding the given where conditions. |
||
235 | * |
||
236 | * @param \miBadger\Query\Query $query |
||
237 | * @param array $where |
||
238 | * @return \miBadger\Query\Query the given query after adding the given where conditions. |
||
239 | */ |
||
240 | 13 | private function getSearchQueryWhere($query, $where) |
|
248 | |||
249 | /** |
||
250 | * Returns the given query after adding the given order by conditions. |
||
251 | * |
||
252 | * @param \miBadger\Query\Query $query |
||
253 | * @param array $orderBy |
||
254 | * @return \miBadger\Query\Query the given query after adding the given order by conditions. |
||
255 | */ |
||
256 | 13 | private function getSearchQueryOrderBy($query, $orderBy) |
|
264 | |||
265 | /** |
||
266 | * Returns the given query after adding the given limit and offset conditions. |
||
267 | * |
||
268 | * @param \miBadger\Query\Query $query |
||
269 | * @param int $limit |
||
270 | * @param int $offset |
||
271 | * @return \miBadger\Query\Query the given query after adding the given limit and offset conditions. |
||
272 | */ |
||
273 | 13 | private function getSearchQueryLimit($query, $limit, $offset) |
|
282 | |||
283 | /** |
||
284 | * Returns the PDO. |
||
285 | * |
||
286 | * @return \PDO the PDO. |
||
287 | */ |
||
288 | 28 | public function getPdo() |
|
292 | |||
293 | /** |
||
294 | * Set the PDO. |
||
295 | * |
||
296 | * @param \PDO $pdo |
||
297 | * @return $this |
||
298 | */ |
||
299 | 31 | protected function setPdo($pdo) |
|
305 | |||
306 | /** |
||
307 | * Returns the ID. |
||
308 | * |
||
309 | * @return null|int The ID. |
||
310 | */ |
||
311 | 9 | public function getId() |
|
315 | |||
316 | /** |
||
317 | * Set the ID. |
||
318 | * |
||
319 | * @param int $id |
||
320 | * @return $this |
||
321 | */ |
||
322 | 16 | protected function setId($id) |
|
328 | |||
329 | /** |
||
330 | * Returns the active record table. |
||
331 | * |
||
332 | * @return string the active record table. |
||
333 | */ |
||
334 | abstract protected function getActiveRecordTable(); |
||
335 | |||
336 | /** |
||
337 | * Returns the active record columns. |
||
338 | * |
||
339 | * @return array the active record columns. |
||
340 | */ |
||
341 | abstract protected function getActiveRecordColumns(); |
||
342 | } |
||
343 |
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.