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 |
||
| 12 | class ActiveRecordModel |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var DatabaseQueryBuilder $db the object for persistent |
||
| 16 | * storage. |
||
| 17 | */ |
||
| 18 | protected $db = null; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var string $tableName name of the database table. |
||
| 22 | */ |
||
| 23 | protected $tableName = null; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var string $tableIdColumn name of the id column in the database table. |
||
| 27 | */ |
||
| 28 | protected $tableIdColumn = "id"; |
||
| 29 | |||
| 30 | |||
| 31 | |||
| 32 | /** |
||
| 33 | * Set the database object to use for accessing storage. |
||
| 34 | * |
||
| 35 | * @param DatabaseQueryBuilder $db as database access object. |
||
| 36 | * |
||
| 37 | * @return void |
||
| 38 | */ |
||
| 39 | 5 | public function setDb(DatabaseQueryBuilder $db) |
|
| 43 | |||
| 44 | |||
| 45 | |||
| 46 | /** |
||
| 47 | * Check if database is injected or throw an exception. |
||
| 48 | * |
||
| 49 | * @throws ActiveRecordException when database is not set. |
||
| 50 | * |
||
| 51 | * @return void |
||
| 52 | */ |
||
| 53 | 6 | protected function checkDb() |
|
| 59 | |||
| 60 | |||
| 61 | |||
| 62 | /** |
||
| 63 | * Get essential object properties. |
||
| 64 | * |
||
| 65 | * @return array with object properties. |
||
| 66 | */ |
||
| 67 | 5 | protected function getProperties() |
|
| 78 | |||
| 79 | |||
| 80 | |||
| 81 | /** |
||
| 82 | * Find and return first object found by search criteria and use |
||
| 83 | * its data to populate this instance. |
||
| 84 | * |
||
| 85 | * @param string $column to use in where statement. |
||
| 86 | * @param mixed $value to use in where statement. |
||
| 87 | * |
||
| 88 | * @return this |
||
| 89 | */ |
||
| 90 | 1 | public function find($column, $value) |
|
| 94 | |||
| 95 | |||
| 96 | |||
| 97 | /** |
||
| 98 | * Find and return first object by its tableIdColumn and use |
||
| 99 | * its data to populate this instance. |
||
| 100 | * |
||
| 101 | * @param string $column to use in where statement. |
||
|
|
|||
| 102 | * @param mixed $value to use in where statement. |
||
| 103 | * |
||
| 104 | * @return this |
||
| 105 | */ |
||
| 106 | 3 | public function findById($value) |
|
| 110 | |||
| 111 | |||
| 112 | |||
| 113 | /** |
||
| 114 | * Find and return first object found by search criteria and use |
||
| 115 | * its data to populate this instance. |
||
| 116 | * |
||
| 117 | * The search criteria `$where` of can be set up like this: |
||
| 118 | * `id = ?` |
||
| 119 | * `id1 = ? and id2 = ?` |
||
| 120 | * |
||
| 121 | * The `$value` can be a single value or an array of values. |
||
| 122 | * |
||
| 123 | * @param string $where to use in where statement. |
||
| 124 | * @param mixed $value to use in where statement. |
||
| 125 | * |
||
| 126 | * @return this |
||
| 127 | */ |
||
| 128 | 3 | View Code Duplication | public function findWhere($where, $value) |
| 139 | |||
| 140 | |||
| 141 | |||
| 142 | /** |
||
| 143 | * Find and return all. |
||
| 144 | * |
||
| 145 | * @return array of object of this class |
||
| 146 | */ |
||
| 147 | 1 | public function findAll() |
|
| 156 | |||
| 157 | |||
| 158 | |||
| 159 | /** |
||
| 160 | * Find and return all matching the search criteria. |
||
| 161 | * |
||
| 162 | * The search criteria `$where` of can be set up like this: |
||
| 163 | * `id = ?` |
||
| 164 | * `id IN [?, ?]` |
||
| 165 | * |
||
| 166 | * The `$value` can be a single value or an array of values. |
||
| 167 | * |
||
| 168 | * @param string $where to use in where statement. |
||
| 169 | * @param mixed $value to use in where statement. |
||
| 170 | * |
||
| 171 | * @return array of object of this class |
||
| 172 | */ |
||
| 173 | 1 | View Code Duplication | public function findAllWhere($where, $value) |
| 184 | |||
| 185 | |||
| 186 | |||
| 187 | /** |
||
| 188 | * Save current object/row, insert if id is missing and do an |
||
| 189 | * update if the id exists. |
||
| 190 | * |
||
| 191 | * @return void |
||
| 192 | */ |
||
| 193 | 6 | public function save() |
|
| 201 | |||
| 202 | |||
| 203 | |||
| 204 | /** |
||
| 205 | * Save current object/row, update with where. |
||
| 206 | * |
||
| 207 | * @param string $where to use in where statement. |
||
| 208 | * @param mixed $params to use in where statement. |
||
| 209 | * |
||
| 210 | * @return void |
||
| 211 | */ |
||
| 212 | public function saveWhere($where, $param) |
||
| 216 | |||
| 217 | |||
| 218 | |||
| 219 | /** |
||
| 220 | * Create new row. |
||
| 221 | * |
||
| 222 | * @return void |
||
| 223 | */ |
||
| 224 | 6 | View Code Duplication | protected function create() |
| 238 | |||
| 239 | |||
| 240 | |||
| 241 | /** |
||
| 242 | * Update row. |
||
| 243 | * |
||
| 244 | * @return void |
||
| 245 | */ |
||
| 246 | 2 | View Code Duplication | protected function update() |
| 260 | |||
| 261 | |||
| 262 | |||
| 263 | /** |
||
| 264 | * Update row where. |
||
| 265 | * |
||
| 266 | * @param string $where to use in where statement. |
||
| 267 | * @param mixed $value to use in where statement. |
||
| 268 | * |
||
| 269 | * @return void |
||
| 270 | */ |
||
| 271 | protected function updateWhere($where, $param) |
||
| 287 | |||
| 288 | |||
| 289 | |||
| 290 | /** |
||
| 291 | * Delete row. |
||
| 292 | * |
||
| 293 | * @param integer $id to delete or use $this->{$this->tableIdColumn} as default. |
||
| 294 | * |
||
| 295 | * @return void |
||
| 296 | */ |
||
| 297 | 1 | public function delete($id = null) |
|
| 309 | |||
| 310 | |||
| 311 | |||
| 312 | /** |
||
| 313 | * Delete row where. |
||
| 314 | * |
||
| 315 | * @param string $where to use in where statement. |
||
| 316 | * @param mixed $value to use in where statement. |
||
| 317 | * |
||
| 318 | * @return void |
||
| 319 | */ |
||
| 320 | View Code Duplication | public function deleteWhere($where, $value) |
|
| 332 | } |
||
| 333 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.