Complex classes like Searchzy 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 Searchzy, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | trait Searchzy |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * Agrupa todas los closures de las relaciones del Modelo, en forma de árbol. |
||
| 13 | * |
||
| 14 | * @var array |
||
| 15 | */ |
||
| 16 | private $relationConstraints = []; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Define el array de las relaciones y el query de las relaciones. En el query |
||
| 20 | * ya se aplicaron las closures de cada relación. |
||
| 21 | * |
||
| 22 | * @var array |
||
| 23 | */ |
||
| 24 | private $eagerRelationConstraints = []; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Define el array con los valores searchable con la 'keyword' de searchzy. |
||
| 28 | * |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | private $searchableInputsKeyword; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Define el array con todos los inputs searchable del Modelo. |
||
| 35 | * |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | private $searchableInputs = []; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Define el array con todos los inputs filterable del Modelo. |
||
| 42 | * |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | private $filterableInputs = []; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Define el array con todos los inputs adicionales del Modelo. |
||
| 49 | * |
||
| 50 | * @var array |
||
| 51 | */ |
||
| 52 | private $aditionableInputs = []; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Define el valor de la 'keyword' de searchzy. |
||
| 56 | * |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | private $searchableKeyword; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Define el request usado por searchzy. |
||
| 63 | * |
||
| 64 | * @var Request |
||
| 65 | */ |
||
| 66 | private $currentRequest; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Scope que realiza una búsqueda searchzy. |
||
| 70 | * |
||
| 71 | * @param Illuminate\Database\Eloquent\Builder $query |
||
| 72 | * @return Illuminate\Database\Eloquent\Builder |
||
| 73 | */ |
||
| 74 | public function scopeSearchzy($query, $keyword = null, $request = null): Builder |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Agrupa las relaciones del Modelo. Retorna un array 'arbol' de las relaciones y sus columnas. |
||
| 99 | * |
||
| 100 | * @param array $arrInputs |
||
| 101 | * @return array |
||
| 102 | */ |
||
| 103 | private function parseRelationInputs($arrInputs): array |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Agrupas las columnas propias del Modelo. |
||
| 122 | * |
||
| 123 | * @param array $arrInputs |
||
| 124 | * @return array |
||
| 125 | */ |
||
| 126 | private function parseModelInputs($arrInputs): array |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Aplica los 'wheres' que serán aplicado en la tabla del Modelo |
||
| 143 | * |
||
| 144 | * @param Builder $query |
||
| 145 | * @return Builder |
||
| 146 | */ |
||
| 147 | private function applySearchableConstraints($query): Builder |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Aplica los 'wheres' que serán aplicado en la relaciones del Modelo. |
||
| 174 | * |
||
| 175 | * @param Builder $query |
||
| 176 | * @return Builder |
||
| 177 | */ |
||
| 178 | private function applySearchableRelationConstraints($query): Builder |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Obtiene el operador del where usado en el armado de la consulta de filterable. |
||
| 218 | * |
||
| 219 | * @param mixed $value |
||
| 220 | * @return array |
||
| 221 | */ |
||
| 222 | private function getOperatorFilterable($value): array |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Aplicación del los where's de los atributos filterable propios del Modelo. |
||
| 233 | * |
||
| 234 | * @param Builder $query |
||
| 235 | * @return Builder |
||
| 236 | */ |
||
| 237 | private function applyFilterableConstraints($query): Builder |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Añade la relación y su callback en un array para despues ser llamada via whereHas. |
||
| 259 | * |
||
| 260 | * @param Builder $query |
||
| 261 | * @return Builder |
||
| 262 | */ |
||
| 263 | private function addRelationSearchableFromConstraint($query): void |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Añade la relación y su callback en un array para despues ser llamada via whereHas. |
||
| 296 | * |
||
| 297 | * @param Builder $query |
||
| 298 | * @return Builder |
||
| 299 | */ |
||
| 300 | private function addRelationFilterableFromConstraint($query): void |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Parsea los inputs de búsqueda del Modelo. |
||
| 331 | * |
||
| 332 | * @param Builder $query |
||
| 333 | * @return Builder |
||
| 334 | */ |
||
| 335 | private function parseInputsKeywordConstraints($query): Builder |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Agrupa las closures por cada relación en {relationConstraints}. |
||
| 355 | * |
||
| 356 | * @param array $relations |
||
| 357 | * @return void |
||
| 358 | */ |
||
| 359 | private function addRelationConstraints(array $relations): void |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Sí hay closures en las relaciones, aplica al query y agrupalas |
||
| 369 | * por cada la relación en {eagerRelationConstraints}. |
||
| 370 | * |
||
| 371 | * @param Builder $query |
||
| 372 | * @return Builder |
||
| 373 | */ |
||
| 374 | private function parseRelationConstraints($query): Builder |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Aplica los 'closures' que estan en {eagerRelationConstraints} por cada relación vía whereHas. |
||
| 395 | * |
||
| 396 | * @param Builder $query |
||
| 397 | * @return Builder |
||
| 398 | */ |
||
| 399 | private function loadRelationContraints($query): Builder |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Retorna un array con los inputs 'searchables' cuyo valor será el ingresado en la 'keyword'. |
||
| 414 | * |
||
| 415 | * @return array |
||
| 416 | */ |
||
| 417 | private function getInputsKeyword(): array |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Obtiene los inputs definidos en el Modelo y que se encuentran en el Request. |
||
| 438 | * |
||
| 439 | * @param string $property |
||
| 440 | * @param string $method |
||
| 441 | * @return array |
||
| 442 | */ |
||
| 443 | private function getInputsFromRequest($property, $method): array |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Obtiene los inputs definidos en el Model, tanto en la propiedad como método. |
||
| 458 | * |
||
| 459 | * @param string $property |
||
| 460 | * @param string $method |
||
| 461 | * @return array |
||
| 462 | */ |
||
| 463 | private function getInputsFromModel($property, $method, $associative = false): array |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Obtiene los inputs de searchzy (keyword, searchzy, extra) cuyo valor será el de Request o el definido por defecto. |
||
| 482 | * |
||
| 483 | * @link (https://timacdonald.me/query-scopes-meet-action-scopes/) |
||
| 484 | * @param Builder $query |
||
| 485 | * @param array $extra |
||
| 486 | * @param string $default |
||
| 487 | * @return array |
||
| 488 | */ |
||
| 489 | public function scopeSearchzyInputs($query, $extra = [], $default = '', $request = null): array |
||
| 509 | } |
||
| 510 |
This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.
The variable may have been renamed without also renaming all references.