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 | public function __construct(\PDO $pdo) |
||
| 38 | |||
| 39 | /** |
||
| 40 | * {@inheritdoc} |
||
| 41 | */ |
||
| 42 | public function create() |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Returns the create query. |
||
| 53 | * |
||
| 54 | * @return string the create query. |
||
| 55 | */ |
||
| 56 | View Code Duplication | private function getCreateQuery() |
|
| 67 | |||
| 68 | /** |
||
| 69 | * {@inheritdoc} |
||
| 70 | */ |
||
| 71 | public function read($id) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Returns the read query. |
||
| 90 | * |
||
| 91 | * @return string the read query. |
||
| 92 | */ |
||
| 93 | private function getReadQuery() |
||
| 97 | |||
| 98 | /** |
||
| 99 | * {@inheritdoc} |
||
| 100 | */ |
||
| 101 | public function update() |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Returns the update query. |
||
| 111 | * |
||
| 112 | * @return string the update query. |
||
| 113 | */ |
||
| 114 | View Code Duplication | private function getUpdateQuery() |
|
| 124 | |||
| 125 | /** |
||
| 126 | * {@inheritdoc} |
||
| 127 | */ |
||
| 128 | public function delete() |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Returns the delete query. |
||
| 139 | * |
||
| 140 | * @return string the delete query. |
||
| 141 | */ |
||
| 142 | private function getDeleteQuery() |
||
| 146 | |||
| 147 | /** |
||
| 148 | * {@inheritdoc} |
||
| 149 | */ |
||
| 150 | public function exists() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Returns the PDO. |
||
| 157 | * |
||
| 158 | * @return PDO the PDO. |
||
| 159 | */ |
||
| 160 | public function getPdo() |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Returns the ID. |
||
| 167 | * |
||
| 168 | * @return null|int The ID. |
||
| 169 | */ |
||
| 170 | public function getId() |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Returns the active record name. |
||
| 177 | * |
||
| 178 | * @return string the active record name. |
||
| 179 | */ |
||
| 180 | abstract protected function getActiveRecordName(); |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Returns the active record data. |
||
| 184 | * |
||
| 185 | * @return array the active record data. |
||
| 186 | */ |
||
| 187 | abstract protected function getActiveRecordData(); |
||
| 188 | } |
||
| 189 |