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 Query 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 | 13 | private function getSearchQueryWhere($query, $where) |
|
| 241 | |||
| 242 | 13 | private function getSearchQueryOrderBy($query, $orderBy) |
|
| 250 | |||
| 251 | 13 | private function getSearchQueryLimit($query, $limit, $offset) |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Returns the PDO. |
||
| 263 | * |
||
| 264 | * @return \PDO the PDO. |
||
| 265 | */ |
||
| 266 | 28 | public function getPdo() |
|
| 270 | |||
| 271 | /** |
||
| 272 | * Set the PDO. |
||
| 273 | * |
||
| 274 | * @param \PDO $pdo |
||
| 275 | * @return $this |
||
| 276 | */ |
||
| 277 | 31 | protected function setPdo($pdo) |
|
| 283 | |||
| 284 | /** |
||
| 285 | * Returns the ID. |
||
| 286 | * |
||
| 287 | * @return null|int The ID. |
||
| 288 | */ |
||
| 289 | 9 | public function getId() |
|
| 293 | |||
| 294 | /** |
||
| 295 | * Set the ID. |
||
| 296 | * |
||
| 297 | * @param int $id |
||
| 298 | * @return $this |
||
| 299 | */ |
||
| 300 | 16 | protected function setId($id) |
|
| 306 | |||
| 307 | /** |
||
| 308 | * Returns the active record table. |
||
| 309 | * |
||
| 310 | * @return string the active record table. |
||
| 311 | */ |
||
| 312 | abstract protected function getActiveRecordTable(); |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Returns the active record columns. |
||
| 316 | * |
||
| 317 | * @return array the active record columns. |
||
| 318 | */ |
||
| 319 | abstract protected function getActiveRecordColumns(); |
||
| 320 | } |
||
| 321 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.