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 | 9 | public function resetModel() |
|
| 57 | |||
| 58 | /** |
||
| 59 | * Get Query |
||
| 60 | * |
||
| 61 | * @return Builder |
||
| 62 | */ |
||
| 63 | 2 | public function getQuery() |
|
| 70 | |||
| 71 | /** |
||
| 72 | * Search All |
||
| 73 | * |
||
| 74 | * @param array $input Array Imput |
||
| 75 | * @param string $orderBy String Order By |
||
| 76 | * @param int $limit Integer Limit |
||
| 77 | * @param bool $skipPresenter Boolean Skip Presenter |
||
| 78 | * |
||
| 79 | * @return BuilderResultset |
||
| 80 | */ |
||
| 81 | 1 | public function searchAll(array $input, $orderBy = '', $limit = null, $skipPresenter = true) |
|
| 95 | |||
| 96 | /** |
||
| 97 | * Search Paginator |
||
| 98 | * |
||
| 99 | * @param array $input Array Input |
||
| 100 | * @param string $orderBy String Order By |
||
| 101 | * @param int|null $limit Integer Limit |
||
| 102 | * @param bool $skipPresenter Boolean Skip Presenter |
||
| 103 | * |
||
| 104 | * @return Paginator |
||
| 105 | */ |
||
| 106 | 1 | public function search(array $input, $orderBy = '', $limit = null, $skipPresenter = true) |
|
| 116 | |||
| 117 | /** |
||
| 118 | * ResultSet |
||
| 119 | * |
||
| 120 | * @param int $limit Integer Limit |
||
| 121 | * |
||
| 122 | * @return BuilderResultset |
||
| 123 | */ |
||
| 124 | public function resultset($limit = null) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Get an array with the values of a given column. |
||
| 134 | * |
||
| 135 | * @param string $column String Column |
||
| 136 | * @param string $key String Key |
||
| 137 | * |
||
| 138 | * @return \Illuminate\Support\Collection |
||
| 139 | */ |
||
| 140 | 1 | public function pluck($column, $key = null) |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Add an "order by" clause to the query. |
||
| 153 | * |
||
| 154 | * @param string $columns String Columns |
||
| 155 | * @param string $direction String Direction |
||
| 156 | * |
||
| 157 | * @return RepositoryInterface |
||
| 158 | */ |
||
| 159 | 3 | public function orderBy($columns, $direction = 'asc') |
|
| 179 | |||
| 180 | /** |
||
| 181 | * Random |
||
| 182 | * |
||
| 183 | * @return RepositoryInterface |
||
| 184 | */ |
||
| 185 | 4 | public function random() |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Count |
||
| 206 | * |
||
| 207 | * @param array $input Array Input |
||
| 208 | * |
||
| 209 | * @return int |
||
| 210 | */ |
||
| 211 | 1 | public function count(array $input = array()) |
|
| 223 | |||
| 224 | /** |
||
| 225 | * Max |
||
| 226 | * |
||
| 227 | * @param mixed $field Mixed Field |
||
| 228 | * @param array $input Array Input |
||
| 229 | * |
||
| 230 | * @return mixed |
||
| 231 | */ |
||
| 232 | 1 | public function max($field, array $input = array()) |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Min |
||
| 247 | * |
||
| 248 | * @param mixed $field Mixed Field |
||
| 249 | * @param array $input Array Input |
||
| 250 | * |
||
| 251 | * @return mixed |
||
| 252 | */ |
||
| 253 | 1 | public function min($field, array $input = array()) |
|
| 265 | |||
| 266 | /** |
||
| 267 | * Sum |
||
| 268 | * |
||
| 269 | * @param mixed $field Mixed Field |
||
| 270 | * @param array $input Array Input |
||
| 271 | * |
||
| 272 | * @return float |
||
| 273 | */ |
||
| 274 | 1 | public function sum($field, array $input = array()) |
|
| 286 | |||
| 287 | /** |
||
| 288 | * Average |
||
| 289 | * |
||
| 290 | * @param mixed $field Mixed Field |
||
| 291 | * @param array $input Array Input |
||
| 292 | * |
||
| 293 | * @return int |
||
| 294 | */ |
||
| 295 | 1 | public function avg($field, array $input = array()) |
|
| 307 | |||
| 308 | /** |
||
| 309 | * Order Up |
||
| 310 | * |
||
| 311 | * @param Model $model |
||
| 312 | * @param string $field Field Order |
||
| 313 | * @param array $input Array Where |
||
| 314 | * |
||
| 315 | * @return boolean |
||
| 316 | */ |
||
| 317 | public function orderUp($model, $field, array $input = []) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Order Top |
||
| 326 | * |
||
| 327 | * @param Model $model |
||
| 328 | * @param string $field Field Order |
||
| 329 | * @param array $input Array Where |
||
| 330 | * |
||
| 331 | * @return boolean |
||
| 332 | */ |
||
| 333 | public function orderTop($model, $field, array $input = []) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Order Down |
||
| 340 | * |
||
| 341 | * @param Model $model |
||
| 342 | * @param string $field Field Order |
||
| 343 | * @param array $input Array Where |
||
| 344 | * |
||
| 345 | * @return boolean |
||
| 346 | */ |
||
| 347 | public function orderDown($model, $field, array $input = []) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Order Bottom |
||
| 356 | * |
||
| 357 | * @param Model $model |
||
| 358 | * @param string $field Field Order |
||
| 359 | * @param array $input Array Where |
||
| 360 | * |
||
| 361 | * @return boolean |
||
| 362 | */ |
||
| 363 | public function orderBottom($model, $field, array $input = []) |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Reorder |
||
| 370 | * |
||
| 371 | * @param Model $model |
||
| 372 | * @param string $field Field Order |
||
| 373 | * @param array $input Array Where |
||
| 374 | * @param string $sort Sort |
||
| 375 | * |
||
| 376 | * @return boolean |
||
| 377 | */ |
||
| 378 | protected function reorder($model, $field, array $input, $sort) |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Where InputCriteria |
||
| 403 | * |
||
| 404 | * @param array $input Array Input |
||
| 405 | * |
||
| 406 | * @return RepositoryInterface |
||
| 407 | */ |
||
| 408 | 7 | public function whereInputCriteria(array $input = array()) |
|
| 417 | |||
| 418 | /** |
||
| 419 | * Validar |
||
| 420 | * |
||
| 421 | * @param array $attributes |
||
| 422 | * @param string $action |
||
| 423 | * @param string $id |
||
| 424 | * |
||
| 425 | * @return bool |
||
| 426 | */ |
||
| 427 | 2 | public function validar(array $attributes, $action, $id = null) |
|
| 449 | |||
| 450 | /** |
||
| 451 | * Save a new model in repository |
||
| 452 | * |
||
| 453 | * @throws ValidatorException |
||
| 454 | * @param array $attributes Array Attributes |
||
| 455 | * @return mixed |
||
| 456 | */ |
||
| 457 | 1 | public function create(array $attributes) |
|
| 469 | |||
| 470 | /** |
||
| 471 | * Update a model in repository by id |
||
| 472 | * |
||
| 473 | * @throws ValidatorException |
||
| 474 | * @param array $attributes Array Attributes |
||
| 475 | * @param int $id Integer Id |
||
| 476 | * @return mixed |
||
| 477 | */ |
||
| 478 | 1 | public function update(array $attributes, $id) |
|
| 499 | |||
| 500 | /** |
||
| 501 | * Delete multiple entities by given criteria. |
||
| 502 | * |
||
| 503 | * @param array $where |
||
| 504 | * |
||
| 505 | * @return boolean|null |
||
| 506 | */ |
||
| 507 | 1 | public function deleteWhere(array $where) |
|
| 527 | |||
| 528 | /** |
||
| 529 | * Update multiple entities by given criteria. |
||
| 530 | * |
||
| 531 | * @param array $where |
||
| 532 | * |
||
| 533 | * @return boolean|null |
||
| 534 | */ |
||
| 535 | 1 | public function updateWhere(array $attributes, array $where) |
|
| 555 | |||
| 556 | /** |
||
| 557 | * Handle dynamic method calls into the method. |
||
| 558 | * |
||
| 559 | * @param string $method |
||
| 560 | * @param array $parameters |
||
| 561 | * |
||
| 562 | * @return AbstractRepository |
||
| 563 | * |
||
| 564 | * @throws BadMethodCallException |
||
| 565 | */ |
||
| 566 | 7 | public function __call($method, $parameters) |
|
| 582 | } |
||
| 583 |
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..