Complex classes like AbstractRepository often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AbstractRepository, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | abstract class AbstractRepository extends BaseRepository implements RepositoryInterface |
||
25 | { |
||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $orderBy; |
||
30 | |||
31 | /** |
||
32 | * @var bool |
||
33 | */ |
||
34 | protected $skipPresenter = true; |
||
35 | |||
36 | /** |
||
37 | * @return Model |
||
38 | * @throws RepositoryException |
||
39 | */ |
||
40 | 29 | public function makeModel() |
|
45 | |||
46 | /** |
||
47 | * Reset Model |
||
48 | * |
||
49 | * @return AbstractRepository |
||
50 | * @throws RepositoryException |
||
51 | */ |
||
52 | 12 | public function resetModel() |
|
57 | |||
58 | /** |
||
59 | * Get Query |
||
60 | * |
||
61 | * @return Builder |
||
62 | */ |
||
63 | 8 | public function getQuery() |
|
73 | |||
74 | /** |
||
75 | * Search All |
||
76 | * |
||
77 | * @param array $input Array Imput |
||
78 | * @param string $orderBy String Order By |
||
79 | * @param int $limit Integer Limit |
||
80 | * @param bool $skipPresenter Boolean Skip Presenter |
||
81 | * |
||
82 | * @return BuilderResultset |
||
83 | */ |
||
84 | 1 | public function searchAll(array $input, $orderBy = '', $limit = null, $skipPresenter = true) |
|
97 | |||
98 | /** |
||
99 | * Search Paginator |
||
100 | * |
||
101 | * @param array $input Array Input |
||
102 | * @param string $orderBy String Order By |
||
103 | * @param int|null $limit Integer Limit |
||
104 | * @param bool $skipPresenter Boolean Skip Presenter |
||
105 | * |
||
106 | * @return Paginator |
||
107 | */ |
||
108 | 1 | public function search(array $input, $orderBy = '', $limit = null, $skipPresenter = true) |
|
118 | |||
119 | /** |
||
120 | * ResultSet |
||
121 | * |
||
122 | * @param int $limit Integer Limit |
||
123 | * |
||
124 | * @return BuilderResultset |
||
125 | */ |
||
126 | public function resultset($limit = null) |
||
132 | |||
133 | /** |
||
134 | * Get an array with the values of a given column. |
||
135 | * |
||
136 | * @param string $column String Column |
||
137 | * @param string $key String Key |
||
138 | * |
||
139 | * @return \Illuminate\Support\Collection |
||
140 | */ |
||
141 | 1 | public function pluck($column, $key = null) |
|
145 | |||
146 | /** |
||
147 | * Add an "order by" clause to the query. |
||
148 | * |
||
149 | * @param string $columns String Columns |
||
150 | * @param string $direction String Direction |
||
151 | * |
||
152 | * @return RepositoryInterface |
||
153 | */ |
||
154 | 3 | public function orderBy($columns, $direction = 'asc') |
|
174 | |||
175 | /** |
||
176 | * Random |
||
177 | * |
||
178 | * @return RepositoryInterface |
||
179 | */ |
||
180 | 4 | public function random() |
|
198 | |||
199 | /** |
||
200 | * Count |
||
201 | * |
||
202 | * @param array $input Array Input |
||
203 | * |
||
204 | * @return int |
||
205 | */ |
||
206 | 1 | public function count(array $input = array()) |
|
212 | |||
213 | /** |
||
214 | * Max |
||
215 | * |
||
216 | * @param mixed $field Mixed Field |
||
217 | * @param array $input Array Input |
||
218 | * |
||
219 | * @return mixed |
||
220 | */ |
||
221 | 1 | public function max($field, array $input = array()) |
|
227 | |||
228 | /** |
||
229 | * Min |
||
230 | * |
||
231 | * @param mixed $field Mixed Field |
||
232 | * @param array $input Array Input |
||
233 | * |
||
234 | * @return mixed |
||
235 | */ |
||
236 | 1 | public function min($field, array $input = array()) |
|
242 | |||
243 | /** |
||
244 | * Sum |
||
245 | * |
||
246 | * @param mixed $field Mixed Field |
||
247 | * @param array $input Array Input |
||
248 | * |
||
249 | * @return float |
||
250 | */ |
||
251 | 1 | public function sum($field, array $input = array()) |
|
257 | |||
258 | /** |
||
259 | * Average |
||
260 | * |
||
261 | * @param mixed $field Mixed Field |
||
262 | * @param array $input Array Input |
||
263 | * |
||
264 | * @return int |
||
265 | */ |
||
266 | 1 | public function avg($field, array $input = array()) |
|
272 | |||
273 | /** |
||
274 | * Order Up |
||
275 | * |
||
276 | * @param Model $model |
||
277 | * @param string $field Field Order |
||
278 | * @param array $input Array Where |
||
279 | * |
||
280 | * @return boolean |
||
281 | */ |
||
282 | public function orderUp($model, $field, array $input = []) |
||
288 | |||
289 | /** |
||
290 | * Order Top |
||
291 | * |
||
292 | * @param Model $model |
||
293 | * @param string $field Field Order |
||
294 | * @param array $input Array Where |
||
295 | * |
||
296 | * @return boolean |
||
297 | */ |
||
298 | public function orderTop($model, $field, array $input = []) |
||
302 | |||
303 | /** |
||
304 | * Order Down |
||
305 | * |
||
306 | * @param Model $model |
||
307 | * @param string $field Field Order |
||
308 | * @param array $input Array Where |
||
309 | * |
||
310 | * @return boolean |
||
311 | */ |
||
312 | public function orderDown($model, $field, array $input = []) |
||
318 | |||
319 | /** |
||
320 | * Order Bottom |
||
321 | * |
||
322 | * @param Model $model |
||
323 | * @param string $field Field Order |
||
324 | * @param array $input Array Where |
||
325 | * |
||
326 | * @return boolean |
||
327 | */ |
||
328 | public function orderBottom($model, $field, array $input = []) |
||
332 | |||
333 | /** |
||
334 | * Reorder |
||
335 | * |
||
336 | * @param Model $model |
||
337 | * @param string $field Field Order |
||
338 | * @param array $input Array Where |
||
339 | * @param string $sort Sort |
||
340 | * |
||
341 | * @return boolean |
||
342 | */ |
||
343 | protected function reorder($model, $field, array $input, $sort) |
||
365 | |||
366 | /** |
||
367 | * Where InputCriteria |
||
368 | * |
||
369 | * @param array $input Array Input |
||
370 | * |
||
371 | * @return RepositoryInterface |
||
372 | */ |
||
373 | 7 | public function whereInputCriteria(array $input = array()) |
|
382 | |||
383 | /** |
||
384 | * Validar |
||
385 | * |
||
386 | * @param array $attributes |
||
387 | * @param string $action |
||
388 | * @param string $id |
||
389 | * |
||
390 | * @return bool |
||
391 | */ |
||
392 | 2 | public function validar(array $attributes, $action, $id = null) |
|
393 | { |
||
394 | 2 | $return = false; |
|
395 | |||
396 | 2 | if (!is_null($this->validator)) { |
|
397 | // we should pass data that has been casts by the model |
||
398 | // to make sure data type are same because validator may need to use |
||
399 | // this data to compare with data that fetch from database. |
||
400 | 2 | $model = $this->model->newModelInstance()->fill($attributes); |
|
401 | 2 | $attributes = array_merge($attributes, $model->toArray()); |
|
402 | |||
403 | 2 | $validator = $this->validator->with($attributes); |
|
404 | |||
405 | 2 | if ($id) { |
|
406 | 1 | $validator->setId($id); |
|
407 | 1 | } |
|
408 | |||
409 | 2 | $return = $validator->passesOrFail($action); |
|
410 | 2 | } |
|
411 | |||
412 | 2 | return $return; |
|
413 | } |
||
414 | |||
415 | /** |
||
416 | * Save a new model in repository |
||
417 | * |
||
418 | * @throws ValidatorException |
||
419 | * @param array $attributes Array Attributes |
||
420 | * @return mixed |
||
421 | */ |
||
422 | 1 | public function create(array $attributes) |
|
423 | { |
||
424 | 1 | $this->validar($attributes, ValidatorInterface::RULE_CREATE); |
|
425 | |||
426 | 1 | $model = $this->model->newModelInstance($attributes); |
|
427 | 1 | $model->save(); |
|
428 | 1 | $this->resetModel(); |
|
429 | |||
430 | 1 | event(new RepositoryEntityCreated($this, $model)); |
|
431 | |||
432 | 1 | return $this->parserResult($model); |
|
433 | } |
||
434 | |||
435 | /** |
||
436 | * Update a model in repository by id |
||
437 | * |
||
438 | * @throws ValidatorException |
||
439 | * @param array $attributes Array Attributes |
||
440 | * @param int $id Integer Id |
||
441 | * @return mixed |
||
442 | */ |
||
443 | 1 | public function update(array $attributes, $id) |
|
444 | { |
||
445 | 1 | $this->applyScope(); |
|
446 | |||
447 | 1 | $this->validar($attributes, ValidatorInterface::RULE_UPDATE, $id); |
|
448 | |||
449 | 1 | $temporarySkipPresenter = $this->skipPresenter; |
|
450 | |||
451 | 1 | $this->skipPresenter(true); |
|
452 | |||
453 | 1 | $model = $this->model->findOrFail($id); |
|
454 | 1 | $model->fill($attributes); |
|
455 | 1 | $model->save(); |
|
456 | |||
457 | 1 | $this->skipPresenter($temporarySkipPresenter); |
|
458 | 1 | $this->resetModel(); |
|
459 | |||
460 | 1 | event(new RepositoryEntityUpdated($this, $model)); |
|
461 | |||
462 | 1 | return $this->parserResult($model); |
|
463 | } |
||
464 | |||
465 | /** |
||
466 | * Delete Force a entity in repository by id |
||
467 | * |
||
468 | * @param $id |
||
469 | * |
||
470 | * @return int |
||
471 | */ |
||
472 | public function deleteForce($id) |
||
473 | { |
||
474 | $this->applyScope(); |
||
475 | |||
476 | $temporarySkipPresenter = $this->skipPresenter; |
||
477 | $this->skipPresenter(true); |
||
478 | |||
479 | $model = $this->find($id); |
||
480 | $originalModel = clone $model; |
||
481 | |||
482 | $this->skipPresenter($temporarySkipPresenter); |
||
483 | $this->resetModel(); |
||
484 | |||
485 | $deleted = $model->deleteForce(); |
||
486 | |||
487 | event(new RepositoryEntityDeleted($this, $originalModel)); |
||
488 | |||
489 | return $deleted; |
||
490 | } |
||
491 | |||
492 | /** |
||
493 | * Delete multiple entities by given criteria. |
||
494 | * |
||
495 | * @param array $where |
||
496 | * |
||
497 | * @return boolean|null |
||
498 | */ |
||
499 | 1 | public function deleteWhere(array $where) |
|
519 | |||
520 | /** |
||
521 | * Update multiple entities by given criteria. |
||
522 | * |
||
523 | * @param array $where |
||
524 | * |
||
525 | * @return boolean|null |
||
526 | */ |
||
527 | 1 | public function updateWhere(array $attributes, array $where) |
|
547 | |||
548 | /** |
||
549 | * Add a subquery join clause to the query. |
||
550 | * |
||
551 | * @param \Closure|\Illuminate\Database\Query\Builder|string $query |
||
552 | * @param string $as |
||
553 | * @param string $first |
||
554 | * @param string|null $operator |
||
555 | * @param string|null $second |
||
556 | * @param string $type |
||
557 | * @param bool $where |
||
558 | * @return \Illuminate\Database\Query\Builder|static |
||
559 | * |
||
560 | * @throws \InvalidArgumentException |
||
561 | */ |
||
562 | public function joinSub($query, $as, $first, $operator = null, $second = null, $type = 'inner', $where = false) |
||
571 | |||
572 | /** |
||
573 | * Add a subquery left join to the query. |
||
574 | * |
||
575 | * @param \Closure|\Illuminate\Database\Query\Builder|string $query |
||
576 | * @param string $as |
||
577 | * @param string $first |
||
578 | * @param string|null $operator |
||
579 | * @param string|null $second |
||
580 | * @return \Illuminate\Database\Query\Builder|static |
||
581 | */ |
||
582 | public function leftJoinSub($query, $as, $first, $operator = null, $second = null) |
||
586 | |||
587 | /** |
||
588 | * Add a subquery right join to the query. |
||
589 | * |
||
590 | * @param \Closure|\Illuminate\Database\Query\Builder|string $query |
||
591 | * @param string $as |
||
592 | * @param string $first |
||
593 | * @param string|null $operator |
||
594 | * @param string|null $second |
||
595 | * @return \Illuminate\Database\Query\Builder|static |
||
596 | */ |
||
597 | public function rightJoinSub($query, $as, $first, $operator = null, $second = null) |
||
601 | |||
602 | /** |
||
603 | * Handle dynamic method calls into the method. |
||
604 | * |
||
605 | * @param string $method |
||
606 | * @param array $parameters |
||
607 | * |
||
608 | * @return AbstractRepository |
||
609 | * |
||
610 | * @throws BadMethodCallException |
||
611 | */ |
||
612 | 7 | public function __call($method, $parameters) |
|
628 | } |
||
629 |
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..