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 |
||
| 13 | abstract class EloquentBaseRepository implements BaseRepository |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var \Illuminate\Database\Eloquent\Model An instance of the Eloquent Model |
||
| 17 | */ |
||
| 18 | protected $model; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @param Model $model |
||
| 22 | */ |
||
| 23 | public function __construct($model) |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @param int $id |
||
| 30 | * @return object |
||
| 31 | */ |
||
| 32 | public function find($id) |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @return \Illuminate\Database\Eloquent\Collection |
||
| 43 | */ |
||
| 44 | View Code Duplication | public function all() |
|
| 52 | |||
| 53 | /** |
||
| 54 | * @return \Illuminate\Pagination\LengthAwarePaginator |
||
| 55 | */ |
||
| 56 | View Code Duplication | public function paginate($perPage = 15) |
|
| 64 | |||
| 65 | /** |
||
| 66 | * @param mixed $data |
||
| 67 | * @return object |
||
| 68 | */ |
||
| 69 | public function create($data) |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @param $model |
||
| 76 | * @param array $data |
||
| 77 | * @return object |
||
| 78 | */ |
||
| 79 | public function update($model, $data) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @param Model $model |
||
| 88 | * @return bool |
||
| 89 | */ |
||
| 90 | public function destroy($model) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Return all resources in the given language |
||
| 97 | * |
||
| 98 | * @param string $lang |
||
| 99 | * @return \Illuminate\Database\Eloquent\Collection |
||
| 100 | */ |
||
| 101 | public function allTranslatedIn($lang) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Find a resource by the given slug |
||
| 110 | * |
||
| 111 | * @param string $slug |
||
| 112 | * @return object |
||
| 113 | */ |
||
| 114 | public function findBySlug($slug) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Find a resource by an array of attributes |
||
| 127 | * @param array $attributes |
||
| 128 | * @return object |
||
| 129 | */ |
||
| 130 | public function findByAttributes(array $attributes) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Get resources by an array of attributes |
||
| 139 | * @param array $attributes |
||
| 140 | * @param null|string $orderBy |
||
| 141 | * @param string $sortOrder |
||
| 142 | * @return \Illuminate\Database\Eloquent\Collection |
||
| 143 | */ |
||
| 144 | public function getByAttributes(array $attributes, $orderBy = null, $sortOrder = 'asc') |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Build Query to catch resources by an array of attributes and params |
||
| 153 | * @param array $attributes |
||
| 154 | * @param null|string $orderBy |
||
| 155 | * @param string $sortOrder |
||
| 156 | * @return \Illuminate\Database\Query\Builder object |
||
| 157 | */ |
||
| 158 | private function buildQueryByAttributes(array $attributes, $orderBy = null, $sortOrder = 'asc') |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Return a collection of elements who's ids match |
||
| 179 | * @param array $ids |
||
| 180 | * @return mixed |
||
| 181 | */ |
||
| 182 | public function findByMany(array $ids) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Clear the cache for this Repositories' Entity |
||
| 195 | * @return bool |
||
| 196 | */ |
||
| 197 | public function clearCache() |
||
| 201 | } |
||
| 202 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..