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 |
||
| 28 | trait WriteQueries |
||
| 29 | { |
||
| 30 | use ReadQueries; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * insertOne |
||
| 34 | * |
||
| 35 | * Insert a new entity in the database. The entity is passed by reference. |
||
| 36 | * It is updated with values returned by the database (ie, default values). |
||
| 37 | * |
||
| 38 | * @access public |
||
| 39 | * @param FlexibleEntityInterface $entity |
||
| 40 | * @return Model $this |
||
| 41 | */ |
||
| 42 | public function insertOne(FlexibleEntityInterface &$entity) |
||
| 66 | |||
| 67 | /** |
||
| 68 | * updateOne |
||
| 69 | * |
||
| 70 | * Update the entity. ONLY the fields indicated in the $fields array are |
||
| 71 | * updated. The entity is passed by reference and its values are updated |
||
| 72 | * with the values from the database. This means all changes not updated |
||
| 73 | * are lost. The update is made upon a condition on the primary key. If the |
||
| 74 | * primary key is not fully set, an exception is thrown. |
||
| 75 | * |
||
| 76 | * @access public |
||
| 77 | * @param FlexibleEntityInterface $entity |
||
| 78 | * @param array $fields |
||
| 79 | * @return Model $this |
||
| 80 | */ |
||
| 81 | public function updateOne(FlexibleEntityInterface &$entity, array $fields) |
||
| 90 | |||
| 91 | /** |
||
| 92 | * updateByPk |
||
| 93 | * |
||
| 94 | * Update a record and fetch it with its new values. If no records match |
||
| 95 | * the given key, null is returned. |
||
| 96 | * |
||
| 97 | * @access public |
||
| 98 | * @param array $primary_key |
||
| 99 | * @param array $updates |
||
| 100 | * @throws ModelException |
||
| 101 | * @return FlexibleEntityInterface |
||
| 102 | */ |
||
| 103 | public function updateByPk(array $primary_key, array $updates) |
||
| 138 | |||
| 139 | /** |
||
| 140 | * updateWhere |
||
| 141 | * |
||
| 142 | * Update records by a given condition. A collection of all deleted entries is returned. |
||
| 143 | * |
||
| 144 | * @param $where |
||
| 145 | * @param array $values |
||
| 146 | * @param array $updates |
||
| 147 | * @return Model $this |
||
| 148 | */ |
||
| 149 | public function updateWhere($where, array $values = [], array $updates) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * deleteOne |
||
| 183 | * |
||
| 184 | * Delete an entity from a table. Entity is passed by reference and is |
||
| 185 | * updated with the values fetched from the deleted record. |
||
| 186 | * |
||
| 187 | * @access public |
||
| 188 | * @param FlexibleEntityInterface $entity |
||
| 189 | * @return Model $this |
||
| 190 | */ |
||
| 191 | public function deleteOne(FlexibleEntityInterface &$entity) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * deleteByPK |
||
| 200 | * |
||
| 201 | * Delete a record from its primary key. The deleted entity is returned or |
||
| 202 | * null if not found. |
||
| 203 | * |
||
| 204 | * @access public |
||
| 205 | * @param array $primary_key |
||
| 206 | * @throws ModelException |
||
| 207 | * @return FlexibleEntityInterface |
||
| 208 | */ |
||
| 209 | public function deleteByPK(array $primary_key) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * deleteWhere |
||
| 221 | * |
||
| 222 | * Delete records by a given condition. A collection of all deleted entries is returned. |
||
| 223 | * |
||
| 224 | * @param $where |
||
| 225 | * @param array $values |
||
| 226 | * @return CollectionIterator |
||
| 227 | */ |
||
| 228 | public function deleteWhere($where, array $values = []) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * createAndSave |
||
| 254 | * |
||
| 255 | * Create a new entity from given values and save it in the database. |
||
| 256 | * |
||
| 257 | * @access public |
||
| 258 | * @param array $values |
||
| 259 | * @return FlexibleEntityInterface |
||
| 260 | */ |
||
| 261 | public function createAndSave(array $values) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * getEscapedFieldList |
||
| 271 | * |
||
| 272 | * Return a comma separated list with the given escaped field names. |
||
| 273 | * |
||
| 274 | * @access protected |
||
| 275 | * @param array $fields |
||
| 276 | * @return string |
||
| 277 | */ |
||
| 278 | public function getEscapedFieldList(array $fields) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * getParametersList |
||
| 290 | * |
||
| 291 | * Create a parameters list from values. |
||
| 292 | * |
||
| 293 | * @access protected |
||
| 294 | * @param array $values |
||
| 295 | * @return array $escape codes |
||
| 296 | */ |
||
| 297 | protected function getParametersList(array $values) |
||
| 310 | } |
||
| 311 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.