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 | 10 | 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) |
|
| 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) |
|
| 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) |
|
| 464 | |||
| 465 | /** |
||
| 466 | * Delete multiple entities by given criteria. |
||
| 467 | * |
||
| 468 | * @param array $where |
||
| 469 | * |
||
| 470 | * @return boolean|null |
||
| 471 | */ |
||
| 472 | 1 | public function deleteWhere(array $where) |
|
| 492 | |||
| 493 | /** |
||
| 494 | * Update multiple entities by given criteria. |
||
| 495 | * |
||
| 496 | * @param array $where |
||
| 497 | * |
||
| 498 | * @return boolean|null |
||
| 499 | */ |
||
| 500 | 1 | public function updateWhere(array $attributes, array $where) |
|
| 520 | |||
| 521 | /** |
||
| 522 | * Add a subquery join clause to the query. |
||
| 523 | * |
||
| 524 | * @param \Closure|\Illuminate\Database\Query\Builder|string $query |
||
| 525 | * @param string $as |
||
| 526 | * @param string $first |
||
| 527 | * @param string|null $operator |
||
| 528 | * @param string|null $second |
||
| 529 | * @param string $type |
||
| 530 | * @param bool $where |
||
| 531 | 7 | * @return \Illuminate\Database\Query\Builder|static |
|
| 532 | * |
||
| 533 | 7 | * @throws \InvalidArgumentException |
|
| 534 | 7 | */ |
|
| 535 | 6 | public function joinSub($query, $as, $first, $operator = null, $second = null, $type = 'inner', $where = false) |
|
| 544 | 1 | ||
| 545 | 1 | /** |
|
| 546 | * Add a subquery left join to the query. |
||
| 547 | * |
||
| 548 | * @param \Closure|\Illuminate\Database\Query\Builder|string $query |
||
| 549 | * @param string $as |
||
| 550 | * @param string $first |
||
| 551 | * @param string|null $operator |
||
| 552 | * @param string|null $second |
||
| 553 | * @return \Illuminate\Database\Query\Builder|static |
||
| 554 | */ |
||
| 555 | public function leftJoinSub($query, $as, $first, $operator = null, $second = null) |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Add a subquery right join to the query. |
||
| 562 | * |
||
| 563 | * @param \Closure|\Illuminate\Database\Query\Builder|string $query |
||
| 564 | * @param string $as |
||
| 565 | * @param string $first |
||
| 566 | * @param string|null $operator |
||
| 567 | * @param string|null $second |
||
| 568 | * @return \Illuminate\Database\Query\Builder|static |
||
| 569 | */ |
||
| 570 | public function rightJoinSub($query, $as, $first, $operator = null, $second = null) |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Handle dynamic method calls into the method. |
||
| 577 | * |
||
| 578 | * @param string $method |
||
| 579 | * @param array $parameters |
||
| 580 | * |
||
| 581 | * @return AbstractRepository |
||
| 582 | * |
||
| 583 | * @throws BadMethodCallException |
||
| 584 | */ |
||
| 585 | public function __call($method, $parameters) |
||
| 601 | } |
||
| 602 |
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..