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 todos los inputs searchable del Modelo. |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | private $searchableInputs = []; |
||
32 | |||
33 | /** |
||
34 | * Define el array con todos los inputs filterable del Modelo. |
||
35 | * |
||
36 | * @var array |
||
37 | */ |
||
38 | private $filterableInputs = []; |
||
39 | |||
40 | /** |
||
41 | * Define el array con todos los inputs adicionales del Modelo. |
||
42 | * |
||
43 | * @var array |
||
44 | */ |
||
45 | private $aditionableInputs = []; |
||
46 | |||
47 | /** |
||
48 | * Define el valor de la 'keyword' de searchzy. |
||
49 | * |
||
50 | * @var array |
||
51 | */ |
||
52 | private $searchableKeyword; |
||
53 | |||
54 | /** |
||
55 | * Define el request usado por searchzy. |
||
56 | * |
||
57 | * @var Request |
||
58 | */ |
||
59 | private $currentRequest; |
||
60 | |||
61 | /** |
||
62 | * Scope que realiza una búsqueda searchzy. |
||
63 | * |
||
64 | * @param Illuminate\Database\Eloquent\Builder $query |
||
65 | * @return Illuminate\Database\Eloquent\Builder |
||
66 | */ |
||
67 | public function scopeSearchzy($query, $keyword = null, $request = null): Builder |
||
89 | |||
90 | /** |
||
91 | * Agrupa las relaciones del Modelo. Retorna un array 'arbol' de las relaciones y sus columnas. |
||
92 | * |
||
93 | * @param array $arrInputs |
||
94 | * @return array |
||
95 | */ |
||
96 | private function parseRelationInputs($arrInputs): array |
||
112 | |||
113 | /** |
||
114 | * Agrupas las columnas propias del Modelo. |
||
115 | * |
||
116 | * @param array $arrInputs |
||
117 | * @return array |
||
118 | */ |
||
119 | private function parseModelInputs($arrInputs): array |
||
133 | |||
134 | /** |
||
135 | * Parsea los inputs de búsqueda del Modelo. |
||
136 | * |
||
137 | * @param Builder $query |
||
138 | * @return Builder |
||
139 | */ |
||
140 | private function parseInputsKeywordConstraints($query): Builder |
||
270 | |||
271 | /** |
||
272 | * Agrupa las closures por cada relación en {relationConstraints}. |
||
273 | * |
||
274 | * @param array $relations |
||
275 | * @return void |
||
276 | */ |
||
277 | private function addRelationConstraints(array $relations): void |
||
284 | |||
285 | /** |
||
286 | * Sí hay closures en las relaciones, aplica al query y agrupalas |
||
287 | * por cada la relación en {eagerRelationConstraints}. |
||
288 | * |
||
289 | * @param Builder $query |
||
290 | * @return Builder |
||
291 | */ |
||
292 | private function parseRelationConstraints($query): Builder |
||
310 | |||
311 | /** |
||
312 | * Aplica los 'closures' que estan en {eagerRelationConstraints} por cada relación vía whereHas. |
||
313 | * |
||
314 | * @param Builder $query |
||
315 | * @return Builder |
||
316 | */ |
||
317 | private function loadRelationContraints($query): Builder |
||
329 | |||
330 | /** |
||
331 | * Retorna un array con los inputs 'searchables' cuyo valor será el ingresado en la 'keyword'. |
||
332 | * |
||
333 | * @return array |
||
334 | */ |
||
335 | private function getInputsKeyword(): array |
||
353 | |||
354 | /** |
||
355 | * Obtiene los inputs definidos en el Modelo y que se encuentran en el Request. |
||
356 | * |
||
357 | * @param string $property |
||
358 | * @param string $method |
||
359 | * @return array |
||
360 | */ |
||
361 | private function getInputsFromRequest($property, $method): array |
||
373 | |||
374 | /** |
||
375 | * Obtiene los inputs definidos en el Model, tanto en la propiedad como método. |
||
376 | * |
||
377 | * @param string $property |
||
378 | * @param string $method |
||
379 | * @param bool $keys |
||
380 | * @return array |
||
381 | */ |
||
382 | private function getInputsFromModel($property, $method, $keys = false): array |
||
394 | |||
395 | /** |
||
396 | * Obtiene los inputs de searchzy (keyword, searchzy, extra) cuyo valor será el de Request o el definido por defecto. |
||
397 | * |
||
398 | * @link (https://timacdonald.me/query-scopes-meet-action-scopes/) |
||
399 | * @param Builder $query |
||
400 | * @param array $extra |
||
401 | * @param string $default |
||
402 | * @return array |
||
403 | */ |
||
404 | public function scopeSearchzyInputs($query, $extra = [], $default = '', $request = null): array |
||
424 | } |
||
425 |
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.