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 results of repository |
||
201 | * |
||
202 | * @param array $where |
||
203 | * @param string $columns |
||
204 | * |
||
205 | * @return int |
||
206 | */ |
||
207 | 1 | public function count(array $where = [], $columns = '*') |
|
213 | |||
214 | /** |
||
215 | * Max |
||
216 | * |
||
217 | * @param mixed $field Mixed Field |
||
218 | * @param array $input Array Input |
||
219 | * |
||
220 | * @return mixed |
||
221 | */ |
||
222 | 1 | public function max($field, array $input = array()) |
|
228 | |||
229 | /** |
||
230 | * Min |
||
231 | * |
||
232 | * @param mixed $field Mixed Field |
||
233 | * @param array $input Array Input |
||
234 | * |
||
235 | * @return mixed |
||
236 | */ |
||
237 | 1 | public function min($field, array $input = array()) |
|
243 | |||
244 | /** |
||
245 | * Sum |
||
246 | * |
||
247 | * @param mixed $field Mixed Field |
||
248 | * @param array $input Array Input |
||
249 | * |
||
250 | * @return float |
||
251 | */ |
||
252 | 1 | public function sum($field, array $input = array()) |
|
258 | |||
259 | /** |
||
260 | * Average |
||
261 | * |
||
262 | * @param mixed $field Mixed Field |
||
263 | * @param array $input Array Input |
||
264 | * |
||
265 | * @return int |
||
266 | */ |
||
267 | 1 | public function avg($field, array $input = array()) |
|
273 | |||
274 | /** |
||
275 | * Order Up |
||
276 | * |
||
277 | * @param Model $model |
||
278 | * @param string $field Field Order |
||
279 | * @param array $input Array Where |
||
280 | * |
||
281 | * @return boolean |
||
282 | */ |
||
283 | public function orderUp($model, $field, array $input = []) |
||
289 | |||
290 | /** |
||
291 | * Order Top |
||
292 | * |
||
293 | * @param Model $model |
||
294 | * @param string $field Field Order |
||
295 | * @param array $input Array Where |
||
296 | * |
||
297 | * @return boolean |
||
298 | */ |
||
299 | public function orderTop($model, $field, array $input = []) |
||
303 | |||
304 | /** |
||
305 | * Order Down |
||
306 | * |
||
307 | * @param Model $model |
||
308 | * @param string $field Field Order |
||
309 | * @param array $input Array Where |
||
310 | * |
||
311 | * @return boolean |
||
312 | */ |
||
313 | public function orderDown($model, $field, array $input = []) |
||
319 | |||
320 | /** |
||
321 | * Order Bottom |
||
322 | * |
||
323 | * @param Model $model |
||
324 | * @param string $field Field Order |
||
325 | * @param array $input Array Where |
||
326 | * |
||
327 | * @return boolean |
||
328 | */ |
||
329 | public function orderBottom($model, $field, array $input = []) |
||
333 | |||
334 | /** |
||
335 | * Reorder |
||
336 | * |
||
337 | * @param Model $model |
||
338 | * @param string $field Field Order |
||
339 | * @param array $input Array Where |
||
340 | * @param string $sort Sort |
||
341 | * |
||
342 | * @return boolean |
||
343 | */ |
||
344 | protected function reorder($model, $field, array $input, $sort) |
||
366 | |||
367 | /** |
||
368 | * Where InputCriteria |
||
369 | * |
||
370 | * @param array $input Array Input |
||
371 | * |
||
372 | * @return RepositoryInterface |
||
373 | */ |
||
374 | 7 | public function whereInputCriteria(array $input = array()) |
|
383 | |||
384 | /** |
||
385 | * Validar |
||
386 | * |
||
387 | * @param array $attributes |
||
388 | * @param string $action |
||
389 | * @param string $id |
||
390 | * |
||
391 | * @return bool |
||
392 | */ |
||
393 | 2 | public function validar(array $attributes, $action, $id = null) |
|
415 | |||
416 | /** |
||
417 | * Save a new model in repository |
||
418 | * |
||
419 | * @throws ValidatorException |
||
420 | * @param array $attributes Array Attributes |
||
421 | * @return mixed |
||
422 | */ |
||
423 | 1 | public function create(array $attributes) |
|
435 | |||
436 | /** |
||
437 | * Update a model in repository by id |
||
438 | * |
||
439 | * @throws ValidatorException |
||
440 | * @param array $attributes Array Attributes |
||
441 | * @param int $id Integer Id |
||
442 | * @return mixed |
||
443 | */ |
||
444 | 1 | public function update(array $attributes, $id) |
|
465 | |||
466 | /** |
||
467 | * Delete Force a entity in repository by id |
||
468 | * |
||
469 | * @param $id |
||
470 | * |
||
471 | * @return int |
||
472 | */ |
||
473 | public function deleteForce($id) |
||
492 | |||
493 | /** |
||
494 | * Delete multiple entities by given criteria. |
||
495 | * |
||
496 | * @param array $where |
||
497 | * |
||
498 | * @return boolean|null |
||
499 | */ |
||
500 | 1 | public function deleteWhere(array $where) |
|
520 | |||
521 | /** |
||
522 | * Update multiple entities by given criteria. |
||
523 | * |
||
524 | * @param array $where |
||
525 | * |
||
526 | * @return boolean|null |
||
527 | */ |
||
528 | 1 | public function updateWhere(array $attributes, array $where) |
|
548 | |||
549 | /** |
||
550 | * Add a subquery join clause to the query. |
||
551 | * |
||
552 | * @param \Closure|\Illuminate\Database\Query\Builder|string $query |
||
553 | * @param string $as |
||
554 | * @param string $first |
||
555 | * @param string|null $operator |
||
556 | * @param string|null $second |
||
557 | * @param string $type |
||
558 | * @param bool $where |
||
559 | * @return \Illuminate\Database\Query\Builder|static |
||
560 | * |
||
561 | * @throws \InvalidArgumentException |
||
562 | */ |
||
563 | public function joinSub($query, $as, $first, $operator = null, $second = null, $type = 'inner', $where = false) |
||
572 | |||
573 | /** |
||
574 | * Add a subquery left join to the query. |
||
575 | * |
||
576 | * @param \Closure|\Illuminate\Database\Query\Builder|string $query |
||
577 | * @param string $as |
||
578 | * @param string $first |
||
579 | * @param string|null $operator |
||
580 | * @param string|null $second |
||
581 | * @return \Illuminate\Database\Query\Builder|static |
||
582 | */ |
||
583 | public function leftJoinSub($query, $as, $first, $operator = null, $second = null) |
||
587 | |||
588 | /** |
||
589 | * Add a subquery right join to the query. |
||
590 | * |
||
591 | * @param \Closure|\Illuminate\Database\Query\Builder|string $query |
||
592 | * @param string $as |
||
593 | * @param string $first |
||
594 | * @param string|null $operator |
||
595 | * @param string|null $second |
||
596 | * @return \Illuminate\Database\Query\Builder|static |
||
597 | */ |
||
598 | public function rightJoinSub($query, $as, $first, $operator = null, $second = null) |
||
602 | |||
603 | /** |
||
604 | * Handle dynamic method calls into the method. |
||
605 | * |
||
606 | * @param string $method |
||
607 | * @param array $parameters |
||
608 | * |
||
609 | * @return AbstractRepository |
||
610 | * |
||
611 | * @throws BadMethodCallException |
||
612 | */ |
||
613 | 7 | public function __call($method, $parameters) |
|
629 | } |
||
630 |
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..