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 |
||
| 9 | class CDatabaseModel implements \Anax\DI\IInjectionAware |
||
| 10 | { |
||
| 11 | use \Anax\DI\TInjectable; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Get the table name. |
||
| 15 | * |
||
| 16 | * @return string with the table name. |
||
| 17 | */ |
||
| 18 | public function getSource() |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Find and return all. |
||
| 25 | * |
||
| 26 | * @return array |
||
| 27 | */ |
||
| 28 | View Code Duplication | public function findAll() |
|
| 37 | |||
| 38 | /** |
||
| 39 | * Find and return recent items. |
||
| 40 | * query requires column "created" in table. |
||
| 41 | * |
||
| 42 | * @return array |
||
| 43 | */ |
||
| 44 | View Code Duplication | public function findRecent($count) |
|
| 55 | |||
| 56 | /** |
||
| 57 | * Get object properties. |
||
| 58 | * |
||
| 59 | * @return array with object properties. |
||
| 60 | */ |
||
| 61 | public function getProperties() |
||
| 69 | /** |
||
| 70 | * Set object properties. |
||
| 71 | * |
||
| 72 | * @param array $properties with properties to set. |
||
| 73 | * |
||
| 74 | * @return void |
||
| 75 | */ |
||
| 76 | public function setProperties($properties) |
||
| 85 | /** |
||
| 86 | * Create new row. |
||
| 87 | * |
||
| 88 | * @param array $values key/values to save. |
||
| 89 | * |
||
| 90 | * @return boolean true or false if saving went okey. |
||
| 91 | */ |
||
| 92 | public function create($values) |
||
| 108 | /** |
||
| 109 | * Find and return specific. |
||
| 110 | * |
||
| 111 | * @return this |
||
| 112 | */ |
||
| 113 | View Code Duplication | public function find($id) |
|
| 122 | /** |
||
| 123 | * Save current object/row. |
||
| 124 | * |
||
| 125 | * @param array $values key/values to save or empty to use object properties. |
||
| 126 | * |
||
| 127 | * @return boolean true or false if saving went okey. |
||
| 128 | */ |
||
| 129 | public function save($values = []) |
||
| 140 | /** |
||
| 141 | * Update row. |
||
| 142 | * |
||
| 143 | * @param array $values key/values to save. |
||
| 144 | * |
||
| 145 | * @return boolean true or false if saving went okey. |
||
| 146 | */ |
||
| 147 | public function update($values) |
||
| 164 | /** |
||
| 165 | * Delete row. |
||
| 166 | * |
||
| 167 | * @param integer $id to delete. |
||
| 168 | * |
||
| 169 | * @return boolean true or false if deleting went okey. |
||
| 170 | */ |
||
| 171 | public function delete($id) |
||
| 180 | /** |
||
| 181 | * Build a select-query. |
||
| 182 | * |
||
| 183 | * @param string $columns which columns to select. |
||
| 184 | * |
||
| 185 | * @return $this |
||
| 186 | */ |
||
| 187 | public function query($columns = '*') |
||
| 194 | /** |
||
| 195 | * Build the where part. |
||
| 196 | * |
||
| 197 | * @param string $condition for building the where part of the query. |
||
| 198 | * |
||
| 199 | * @return $this |
||
| 200 | */ |
||
| 201 | public function where($condition) |
||
| 207 | /** |
||
| 208 | * Build the where part. |
||
| 209 | * |
||
| 210 | * @param string $condition for building the where part of the query. |
||
| 211 | * |
||
| 212 | * @return $this |
||
| 213 | */ |
||
| 214 | public function andWhere($condition) |
||
| 220 | /** |
||
| 221 | * Execute the query built. |
||
| 222 | * |
||
| 223 | * @param string $query custom query. |
||
| 224 | * |
||
| 225 | * @return $this |
||
| 226 | */ |
||
| 227 | View Code Duplication | public function execute($params = []) |
|
| 234 | } |
||
| 235 |
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.