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 | 11 | 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 | * Get an array with the values of a given column. |
||
| 119 | * |
||
| 120 | * @param string $column String Column |
||
| 121 | * @param string $key String Key |
||
| 122 | * |
||
| 123 | * @return \Illuminate\Support\Collection |
||
| 124 | */ |
||
| 125 | 1 | public function pluck($column, $key = null) |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Add an "order by" clause to the query. |
||
| 138 | * |
||
| 139 | * @param string $columns String Columns |
||
| 140 | * @param string $direction String Direction |
||
| 141 | * |
||
| 142 | * @return RepositoryInterface |
||
| 143 | */ |
||
| 144 | 3 | public function orderBy($columns, $direction = 'asc') |
|
| 164 | |||
| 165 | /** |
||
| 166 | * Random |
||
| 167 | * |
||
| 168 | * @return RepositoryInterface |
||
| 169 | */ |
||
| 170 | 4 | public function random() |
|
| 188 | |||
| 189 | /** |
||
| 190 | * Count |
||
| 191 | * |
||
| 192 | * @param array $input Array Input |
||
| 193 | * |
||
| 194 | * @return int |
||
| 195 | */ |
||
| 196 | 1 | public function count(array $input = array()) |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Max |
||
| 211 | * |
||
| 212 | * @param mixed $field Mixed Field |
||
| 213 | * @param array $input Array Input |
||
| 214 | * |
||
| 215 | * @return mixed |
||
| 216 | */ |
||
| 217 | 1 | public function max($field, array $input = array()) |
|
| 229 | |||
| 230 | /** |
||
| 231 | * Min |
||
| 232 | * |
||
| 233 | * @param mixed $field Mixed Field |
||
| 234 | * @param array $input Array Input |
||
| 235 | * |
||
| 236 | * @return mixed |
||
| 237 | */ |
||
| 238 | 1 | public function min($field, array $input = array()) |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Sum |
||
| 253 | * |
||
| 254 | * @param mixed $field Mixed Field |
||
| 255 | * @param array $input Array Input |
||
| 256 | * |
||
| 257 | * @return float |
||
| 258 | */ |
||
| 259 | 1 | public function sum($field, array $input = array()) |
|
| 271 | |||
| 272 | /** |
||
| 273 | * Average |
||
| 274 | * |
||
| 275 | * @param mixed $field Mixed Field |
||
| 276 | * @param array $input Array Input |
||
| 277 | * |
||
| 278 | * @return int |
||
| 279 | */ |
||
| 280 | 1 | public function avg($field, array $input = array()) |
|
| 292 | |||
| 293 | /** |
||
| 294 | * Order Up |
||
| 295 | * |
||
| 296 | * @param Model $model |
||
| 297 | * @param string $field Field Order |
||
| 298 | * @param array $input Array Where |
||
| 299 | * |
||
| 300 | * @return boolean |
||
| 301 | */ |
||
| 302 | public function orderUp($model, $field, array $input = []) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Order Down |
||
| 311 | * |
||
| 312 | * @param Model $model |
||
| 313 | * @param string $field Field Order |
||
| 314 | * @param array $input Array Where |
||
| 315 | * |
||
| 316 | * @return boolean |
||
| 317 | */ |
||
| 318 | public function orderDown($model, $field, array $input = []) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Reorder |
||
| 327 | * |
||
| 328 | * @param Model $model |
||
| 329 | * @param string $field Field Order |
||
| 330 | * @param array $input Array Where |
||
| 331 | * @param string $sort Sort |
||
| 332 | * |
||
| 333 | * @return boolean |
||
| 334 | */ |
||
| 335 | protected function reorder($model, $field, array $input, $sort) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Where InputCriteria |
||
| 358 | * |
||
| 359 | * @param array $input Array Input |
||
| 360 | * |
||
| 361 | * @return RepositoryInterface |
||
| 362 | */ |
||
| 363 | 7 | public function whereInputCriteria(array $input = array()) |
|
| 372 | |||
| 373 | /** |
||
| 374 | * Validar |
||
| 375 | * |
||
| 376 | * @param array $attributes |
||
| 377 | * @param string $action |
||
| 378 | * @param string $id |
||
| 379 | * |
||
| 380 | * @return bool |
||
| 381 | */ |
||
| 382 | 2 | public function validar(array $attributes, $action, $id = null) |
|
| 404 | |||
| 405 | /** |
||
| 406 | * Save a new model in repository |
||
| 407 | * |
||
| 408 | * @throws ValidatorException |
||
| 409 | * @param array $attributes Array Attributes |
||
| 410 | * @return mixed |
||
| 411 | */ |
||
| 412 | 1 | public function create(array $attributes) |
|
| 424 | |||
| 425 | /** |
||
| 426 | * Update a model in repository by id |
||
| 427 | * |
||
| 428 | * @throws ValidatorException |
||
| 429 | * @param array $attributes Array Attributes |
||
| 430 | * @param int $id Integer Id |
||
| 431 | * @return mixed |
||
| 432 | */ |
||
| 433 | 1 | public function update(array $attributes, $id) |
|
| 454 | |||
| 455 | /** |
||
| 456 | * Delete multiple entities by given criteria. |
||
| 457 | * |
||
| 458 | * @param array $where |
||
| 459 | * |
||
| 460 | * @return boolean|null |
||
| 461 | */ |
||
| 462 | 1 | public function deleteWhere(array $where) |
|
| 481 | |||
| 482 | /** |
||
| 483 | * Update multiple entities by given criteria. |
||
| 484 | * |
||
| 485 | * @param array $where |
||
| 486 | * |
||
| 487 | * @return boolean|null |
||
| 488 | */ |
||
| 489 | 1 | public function updateWhere(array $attributes, array $where) |
|
| 508 | |||
| 509 | /** |
||
| 510 | * Handle dynamic method calls into the method. |
||
| 511 | * |
||
| 512 | * @param string $method |
||
| 513 | * @param array $parameters |
||
| 514 | * |
||
| 515 | * @return AbstractRepository |
||
| 516 | * |
||
| 517 | * @throws BadMethodCallException |
||
| 518 | */ |
||
| 519 | 7 | public function __call($method, $parameters) |
|
| 535 | } |
||
| 536 |
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..