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 namespace Anomaly\Streams\Platform\Model; | ||
| 19 | class EloquentRepository implements EloquentRepositoryInterface | ||
| 20 | { | ||
| 21 | |||
| 22 | use FiresCallbacks; | ||
| 23 | use Hookable; | ||
| 24 | |||
| 25 | /** | ||
| 26 | * Return all records. | ||
| 27 | * | ||
| 28 | * @return EloquentCollection | ||
| 29 | */ | ||
| 30 | public function all() | ||
| 34 | |||
| 35 | /** | ||
| 36 | * Find a record by it's ID. | ||
| 37 | * | ||
| 38 | * @param $id | ||
| 39 | * @return EloquentModel | ||
| 40 | */ | ||
| 41 | public function find($id) | ||
| 45 | |||
| 46 | /** | ||
| 47 | * Find all records by IDs. | ||
| 48 | * | ||
| 49 | * @param array $ids | ||
| 50 | * @return EloquentCollection | ||
| 51 | */ | ||
| 52 | public function findAll(array $ids) | ||
| 56 | |||
| 57 | /** | ||
| 58 | * Find a trashed record by it's ID. | ||
| 59 | * | ||
| 60 | * @param $id | ||
| 61 | * @return null|EloquentModel | ||
| 62 | */ | ||
| 63 | public function findTrashed($id) | ||
| 71 | |||
| 72 | /** | ||
| 73 | * Create a new record. | ||
| 74 | * | ||
| 75 | * @param array $attributes | ||
| 76 | * @return EloquentModel | ||
| 77 | */ | ||
| 78 | public function create(array $attributes) | ||
| 82 | |||
| 83 | /** | ||
| 84 | * Return a new query builder. | ||
| 85 | * | ||
| 86 | * @return Builder | ||
| 87 | */ | ||
| 88 | public function newQuery() | ||
| 92 | |||
| 93 | /** | ||
| 94 | * Return a new instance. | ||
| 95 | * | ||
| 96 | * @param array $attributes | ||
| 97 | * @return EloquentModel | ||
| 98 | */ | ||
| 99 | public function newInstance(array $attributes = []) | ||
| 103 | |||
| 104 | /** | ||
| 105 | * Count all records. | ||
| 106 | * | ||
| 107 | * @return int | ||
| 108 | */ | ||
| 109 | public function count() | ||
| 113 | |||
| 114 | /** | ||
| 115 | * Return a paginated collection. | ||
| 116 | * | ||
| 117 | * @param array $parameters | ||
| 118 | * @return LengthAwarePaginator | ||
| 119 | */ | ||
| 120 | public function paginate(array $parameters = []) | ||
| 162 | |||
| 163 | /** | ||
| 164 | * Save a record. | ||
| 165 | * | ||
| 166 | * @param EloquentModel $entry | ||
| 167 | * @return bool | ||
| 168 | */ | ||
| 169 | public function save(EloquentModel $entry) | ||
| 173 | |||
| 174 | /** | ||
| 175 | * Update multiple records. | ||
| 176 | * | ||
| 177 | * @param array $attributes | ||
| 178 | * @return bool | ||
| 179 | */ | ||
| 180 | public function update(array $attributes = []) | ||
| 184 | |||
| 185 | /** | ||
| 186 | * Delete a record. | ||
| 187 | * | ||
| 188 | * @param EloquentModel $entry | ||
| 189 | * @return bool | ||
| 190 | */ | ||
| 191 | public function delete(EloquentModel $entry) | ||
| 195 | |||
| 196 | /** | ||
| 197 | * Force delete a record. | ||
| 198 | * | ||
| 199 | * @param EloquentModel $entry | ||
| 200 | * @return bool | ||
| 201 | */ | ||
| 202 | public function forceDelete(EloquentModel $entry) | ||
| 212 | |||
| 213 | /** | ||
| 214 | * Restore a trashed record. | ||
| 215 | * | ||
| 216 | * @param EloquentModel $entry | ||
| 217 | * @return bool | ||
| 218 | */ | ||
| 219 | public function restore(EloquentModel $entry) | ||
| 223 | |||
| 224 | /** | ||
| 225 | * Truncate the entries. | ||
| 226 | * | ||
| 227 | * @return $this | ||
| 228 | */ | ||
| 229 | public function truncate() | ||
| 251 | |||
| 252 | /** | ||
| 253 | * Cache a value in the | ||
| 254 | * model's cache collection. | ||
| 255 | * | ||
| 256 | * @param $key | ||
| 257 | * @param $ttl | ||
| 258 | * @param $value | ||
| 259 | * @return mixed | ||
| 260 | */ | ||
| 261 | public function cache($key, $ttl, $value) | ||
| 265 | |||
| 266 | /** | ||
| 267 | * Guard the model. | ||
| 268 | * | ||
| 269 | * @return $this | ||
| 270 | */ | ||
| 271 | public function guard() | ||
| 277 | |||
| 278 | /** | ||
| 279 | * Unguard the model. | ||
| 280 | * | ||
| 281 | * @return $this | ||
| 282 | */ | ||
| 283 | public function unguard() | ||
| 289 | |||
| 290 | /** | ||
| 291 | * Set the model. | ||
| 292 | * | ||
| 293 | * @param EloquentModel $model | ||
| 294 | * @return $this | ||
| 295 | */ | ||
| 296 | public function setModel(EloquentModel $model) | ||
| 302 | |||
| 303 | /** | ||
| 304 | * Get the model. | ||
| 305 | * | ||
| 306 | * @return EloquentModel | ||
| 307 | */ | ||
| 308 | public function getModel() | ||
| 312 | |||
| 313 | /** | ||
| 314 | * Pipe non-existing calls through hooks. | ||
| 315 | * | ||
| 316 | * @param $method | ||
| 317 | * @param $parameters | ||
| 318 | * @return mixed|null | ||
| 319 | */ | ||
| 320 | View Code Duplication | public function __call($method, $parameters) | |
| 328 | } | ||
| 329 |