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 |
||
21 | abstract class Repository extends BaseRepository implements RepositoryInterface, CacheableInterface, BaseRepositoryEventsInterface |
||
22 | { |
||
23 | use CacheableRepository; |
||
24 | /* Model Observers */ |
||
25 | use BaseRepositoryEventsTrait; |
||
26 | |||
27 | /** |
||
28 | * @var Model |
||
29 | */ |
||
30 | protected $relateModel; |
||
31 | |||
32 | /** |
||
33 | * @param array $attributes |
||
34 | */ |
||
35 | public function validateCreate(array $attributes) |
||
41 | |||
42 | /** |
||
43 | * @param array $attributes |
||
44 | */ |
||
45 | public function validateUpdate(array $attributes) |
||
51 | |||
52 | /** |
||
53 | * @param $validator |
||
54 | * |
||
55 | * @return $this |
||
56 | */ |
||
57 | public function setValidator($validator) |
||
63 | |||
64 | /** |
||
65 | * @param $results |
||
66 | * |
||
67 | * @return mixed |
||
68 | */ |
||
69 | public function present($results) |
||
73 | |||
74 | /** |
||
75 | * @param Model $relateModel |
||
76 | * |
||
77 | * @return $this |
||
78 | */ |
||
79 | public function setRelateModel(Model $relateModel) |
||
88 | |||
89 | /** |
||
90 | * @return Model |
||
91 | * @throws RepositoryException |
||
92 | */ |
||
93 | public function makeModel() |
||
106 | |||
107 | /** |
||
108 | * @return Relation |
||
109 | */ |
||
110 | public function relation() |
||
113 | |||
114 | /** |
||
115 | * Retrieve data array for populate field select. |
||
116 | * |
||
117 | * @param string $column |
||
118 | * @param string|null $key |
||
119 | * |
||
120 | * @return \Illuminate\Support\Collection|array |
||
121 | */ |
||
122 | public function lists($column, $key = null) |
||
133 | |||
134 | /** |
||
135 | * Retrieve all data of repository. |
||
136 | * |
||
137 | * @param array $columns |
||
138 | * |
||
139 | * @return mixed |
||
140 | */ |
||
141 | public function all($columns = ['*']) |
||
154 | |||
155 | /** |
||
156 | * @return \Prettus\Repository\Contracts\PresenterInterface |
||
157 | */ |
||
158 | public function getPresenter() |
||
162 | |||
163 | /** |
||
164 | * @param array $meta |
||
165 | */ |
||
166 | public function setPresenterMeta(array $meta) |
||
172 | |||
173 | /** |
||
174 | * Where first. |
||
175 | * |
||
176 | * @param array $where |
||
177 | * @param array $columns |
||
178 | * |
||
179 | * @return mixed |
||
180 | */ |
||
181 | public function whereFirst(array $where, $columns = ['*']) |
||
185 | |||
186 | /** |
||
187 | * Retrieve first data of repository with fail if not found. |
||
188 | * |
||
189 | * @param string[] $columns |
||
190 | * |
||
191 | * @return mixed |
||
192 | */ |
||
193 | public function firstOrFail($columns = ['*']) |
||
202 | |||
203 | /** |
||
204 | * Find data by where conditions. |
||
205 | * |
||
206 | * @param array $where |
||
207 | * |
||
208 | * @return $this |
||
209 | */ |
||
210 | public function where(array $where) |
||
218 | |||
219 | /** |
||
220 | * Add a basic where clause to the model. |
||
221 | * |
||
222 | * @param string|array|\Closure $column |
||
223 | * @param mixed $value |
||
224 | * |
||
225 | * @return $this |
||
226 | */ |
||
227 | protected function modelWhere($column, $value = null) |
||
233 | |||
234 | /** |
||
235 | * withTrashed. |
||
236 | * |
||
237 | * @return $this |
||
238 | */ |
||
239 | public function withTrashed() |
||
245 | |||
246 | /** |
||
247 | * without-trashed. |
||
248 | * |
||
249 | * @return $this |
||
250 | */ |
||
251 | public function withoutTrashed() |
||
257 | |||
258 | /** |
||
259 | * onlyTrashed. |
||
260 | * |
||
261 | * @return $this |
||
262 | */ |
||
263 | public function onlyTrashed() |
||
269 | |||
270 | /** |
||
271 | * Restore a entity in repository by id. |
||
272 | * |
||
273 | * @param $id |
||
274 | * |
||
275 | * @return mixed |
||
276 | */ |
||
277 | View Code Duplication | public function restore($id) |
|
299 | |||
300 | /** |
||
301 | * restore multiple entities by given criteria. |
||
302 | * |
||
303 | * @param array $where |
||
304 | * |
||
305 | * @return int |
||
306 | */ |
||
307 | public function restoreWhere(array $where) |
||
329 | |||
330 | /** |
||
331 | * ForceDelete a entity in repository by id. |
||
332 | * |
||
333 | * @param $id |
||
334 | * |
||
335 | * @return int |
||
336 | */ |
||
337 | View Code Duplication | public function ForceDelete($id) |
|
356 | |||
357 | /** |
||
358 | * ForceDelete multiple entities by given criteria. |
||
359 | * |
||
360 | * @param array $where |
||
361 | * |
||
362 | * @return null|bool |
||
363 | */ |
||
364 | public function forceDeleteWhere(array $where) |
||
382 | } |
||
383 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.