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:
Complex classes like AbstractActiveRecord often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AbstractActiveRecord, and based on these observations, apply Extract Interface, too.
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 | 31 | public function __construct(\PDO $pdo) |
|
38 | |||
39 | /** |
||
40 | * {@inheritdoc} |
||
41 | */ |
||
42 | 4 | public function create() |
|
43 | { |
||
44 | try { |
||
45 | 4 | $pdoStatement = $this->getPdo()->prepare($this->getCreateQuery()); |
|
46 | 2 | $pdoStatement->execute($this->getActiveRecordAttributes()); |
|
47 | |||
48 | 2 | $this->setId(intval($this->getPdo()->lastInsertId())); |
|
49 | 4 | } catch (\PDOException $e) { |
|
50 | 2 | throw new ActiveRecordException(sprintf('Can not create a new active record entry in the `%s` table.', $this->getActiveRecordTable()), 0, $e); |
|
51 | } |
||
52 | |||
53 | 2 | return $this; |
|
54 | } |
||
55 | |||
56 | /** |
||
57 | * Returns the create query. |
||
58 | * |
||
59 | * @return string the create query. |
||
60 | */ |
||
61 | 4 | View Code Duplication | private function getCreateQuery() |
|
|||
62 | { |
||
63 | 4 | $columns = array_keys($this->getActiveRecordAttributes()); |
|
64 | 4 | $values = []; |
|
65 | |||
66 | 4 | foreach ($columns as $key => $value) { |
|
67 | 4 | $values[] = sprintf(':%s', $value); |
|
68 | 4 | } |
|
69 | |||
70 | 4 | return sprintf('INSERT INTO `%s` (`%s`) VALUES (%s)', $this->getActiveRecordTable(), implode('`, `', $columns), implode(', ', $values)); |
|
71 | } |
||
72 | |||
73 | /** |
||
74 | * {@inheritdoc} |
||
75 | */ |
||
76 | 9 | public function read($id) |
|
77 | { |
||
78 | try { |
||
79 | 9 | $pdoStatement = $this->getPdo()->prepare($this->getReadQuery()); |
|
80 | 8 | $pdoStatement->execute(['id' => $id]); |
|
81 | 8 | $result = $pdoStatement->fetch(); |
|
82 | |||
83 | 8 | if ($result === false) { |
|
84 | 1 | throw new ActiveRecordException(sprintf('Can not read the non-existent active record entry %d from the `%s` table.', $id, $this->getActiveRecordTable())); |
|
85 | } |
||
86 | |||
87 | 7 | $this->fill($result); |
|
88 | 6 | $this->setId($id); |
|
89 | 9 | } catch (\PDOException $e) { |
|
90 | 1 | throw new ActiveRecordException(sprintf('Can not read active record entry %d from the `%s` table.', $id, $this->getActiveRecordTable()), 0, $e); |
|
91 | } |
||
92 | |||
93 | 6 | return $this; |
|
94 | } |
||
95 | |||
96 | /** |
||
97 | * Returns the read query. |
||
98 | * |
||
99 | * @return string the read query. |
||
100 | */ |
||
101 | 9 | private function getReadQuery() |
|
105 | |||
106 | /** |
||
107 | * {@inheritdoc} |
||
108 | */ |
||
109 | 5 | View Code Duplication | public function update() |
124 | |||
125 | /** |
||
126 | * Returns the update query. |
||
127 | * |
||
128 | * @return string the update query. |
||
129 | */ |
||
130 | 4 | View Code Duplication | private function getUpdateQuery() |
140 | |||
141 | /** |
||
142 | * {@inheritdoc} |
||
143 | */ |
||
144 | 4 | View Code Duplication | public function delete() |
161 | |||
162 | /** |
||
163 | * Returns the delete query. |
||
164 | * |
||
165 | * @return string the delete query. |
||
166 | */ |
||
167 | 3 | private function getDeleteQuery() |
|
171 | |||
172 | /** |
||
173 | * {@inheritdoc} |
||
174 | */ |
||
175 | 2 | public function sync() |
|
183 | |||
184 | /** |
||
185 | * {@inheritdoc} |
||
186 | */ |
||
187 | 10 | public function exists() |
|
191 | |||
192 | /** |
||
193 | * Fill the active record |
||
194 | * |
||
195 | * @param array $fetch |
||
196 | * @return null |
||
197 | */ |
||
198 | 17 | public function fill(array $fetch) |
|
210 | |||
211 | /** |
||
212 | * {@inheritdoc} |
||
213 | */ |
||
214 | 2 | public function searchFirst(array $where = [], array $orderBy = []) |
|
235 | |||
236 | /** |
||
237 | * {@inheritdoc} |
||
238 | */ |
||
239 | 11 | public function search(array $where = [], array $orderBy = [], $limit = -1, $offset = 0) |
|
266 | |||
267 | /** |
||
268 | * Returns the search query with the given where, order by, limit and offset clauses. |
||
269 | * |
||
270 | * @param array $where = [] |
||
271 | * @param array $orderBy = [] |
||
272 | * @param int $limit = -1 |
||
273 | * @param int $offset = 0 |
||
274 | * @return string the search query with the given where, order by, limit and offset clauses. |
||
275 | */ |
||
276 | 13 | private function getSearchQuery($where = [], $orderBy = [], $limit = -1, $offset = 0) |
|
286 | |||
287 | /** |
||
288 | * Returns the search query where clauses. |
||
289 | * |
||
290 | * @param array $where |
||
291 | * @return string the search query where clauses. |
||
292 | */ |
||
293 | 13 | private function getSearchQueryWhereClauses($where) |
|
309 | |||
310 | /** |
||
311 | * Returns the search query where clause. |
||
312 | * |
||
313 | * @param array $where |
||
314 | * @return string the search query where clause. |
||
315 | */ |
||
316 | 6 | private function getSearchQueryWhereClause($key, $value) |
|
330 | |||
331 | /** |
||
332 | * Returns the search query order by clause. |
||
333 | * |
||
334 | * @param array $orderBy |
||
335 | * @return string the search query order by clause. |
||
336 | */ |
||
337 | 11 | private function getSearchQueryOrderByClause($orderBy) |
|
347 | |||
348 | /** |
||
349 | * Returns the search query limit and clause. |
||
350 | * |
||
351 | * @param int $limit = -1 |
||
352 | * @param int $offset = 0 |
||
353 | * @return string the search query limit and clause. |
||
354 | */ |
||
355 | 11 | private function getSearchQueryLimitClause($limit, $offset) |
|
363 | |||
364 | /** |
||
365 | * Returns the PDO. |
||
366 | * |
||
367 | * @return \PDO the PDO. |
||
368 | */ |
||
369 | 28 | public function getPdo() |
|
373 | |||
374 | /** |
||
375 | * Set the PDO. |
||
376 | * |
||
377 | * @param \PDO $pdo |
||
378 | * @return $this |
||
379 | */ |
||
380 | 31 | protected function setPdo($pdo) |
|
386 | |||
387 | /** |
||
388 | * Returns the ID. |
||
389 | * |
||
390 | * @return null|int The ID. |
||
391 | */ |
||
392 | 9 | public function getId() |
|
396 | |||
397 | /** |
||
398 | * Set the ID. |
||
399 | * |
||
400 | * @param int $id |
||
401 | * @return $this |
||
402 | */ |
||
403 | 16 | protected function setId($id) |
|
409 | |||
410 | /** |
||
411 | * Returns the active record table. |
||
412 | * |
||
413 | * @return string the active record table. |
||
414 | */ |
||
415 | abstract protected function getActiveRecordTable(); |
||
416 | |||
417 | /** |
||
418 | * Returns the active record attributes. |
||
419 | * |
||
420 | * @return array the active record attributes. |
||
421 | */ |
||
422 | abstract protected function getActiveRecordAttributes(); |
||
423 | } |
||
424 |
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.