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 | * 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 Down |
||
| 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 orderDown($model, $field, array $input = []) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Reorder |
||
| 342 | * |
||
| 343 | * @param Model $model |
||
| 344 | * @param string $field Field Order |
||
| 345 | * @param array $input Array Where |
||
| 346 | * @param string $sort Sort |
||
| 347 | * |
||
| 348 | * @return boolean |
||
| 349 | */ |
||
| 350 | protected function reorder($model, $field, array $input, $sort) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Where InputCriteria |
||
| 375 | * |
||
| 376 | * @param array $input Array Input |
||
| 377 | * |
||
| 378 | * @return RepositoryInterface |
||
| 379 | */ |
||
| 380 | 7 | public function whereInputCriteria(array $input = array()) |
|
| 389 | |||
| 390 | /** |
||
| 391 | * Validar |
||
| 392 | * |
||
| 393 | * @param array $attributes |
||
| 394 | * @param string $action |
||
| 395 | * @param string $id |
||
| 396 | * |
||
| 397 | * @return bool |
||
| 398 | */ |
||
| 399 | 2 | public function validar(array $attributes, $action, $id = null) |
|
| 421 | |||
| 422 | /** |
||
| 423 | * Save a new model in repository |
||
| 424 | * |
||
| 425 | * @throws ValidatorException |
||
| 426 | * @param array $attributes Array Attributes |
||
| 427 | * @return mixed |
||
| 428 | */ |
||
| 429 | 1 | public function create(array $attributes) |
|
| 441 | |||
| 442 | /** |
||
| 443 | * Update a model in repository by id |
||
| 444 | * |
||
| 445 | * @throws ValidatorException |
||
| 446 | * @param array $attributes Array Attributes |
||
| 447 | * @param int $id Integer Id |
||
| 448 | * @return mixed |
||
| 449 | */ |
||
| 450 | 1 | public function update(array $attributes, $id) |
|
| 471 | |||
| 472 | /** |
||
| 473 | * Delete multiple entities by given criteria. |
||
| 474 | * |
||
| 475 | * @param array $where |
||
| 476 | * |
||
| 477 | * @return boolean|null |
||
| 478 | */ |
||
| 479 | 1 | public function deleteWhere(array $where) |
|
| 499 | |||
| 500 | /** |
||
| 501 | * Update multiple entities by given criteria. |
||
| 502 | * |
||
| 503 | * @param array $where |
||
| 504 | * |
||
| 505 | * @return boolean|null |
||
| 506 | */ |
||
| 507 | 1 | public function updateWhere(array $attributes, array $where) |
|
| 527 | |||
| 528 | /** |
||
| 529 | * Handle dynamic method calls into the method. |
||
| 530 | * |
||
| 531 | * @param string $method |
||
| 532 | * @param array $parameters |
||
| 533 | * |
||
| 534 | * @return AbstractRepository |
||
| 535 | * |
||
| 536 | * @throws BadMethodCallException |
||
| 537 | */ |
||
| 538 | 7 | public function __call($method, $parameters) |
|
| 554 | } |
||
| 555 |
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..