Complex classes like QueryBuilder 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 QueryBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class QueryBuilder extends Builder |
||
15 | { |
||
16 | /** @var \Illuminate\Support\Collection */ |
||
17 | protected $allowedFilters; |
||
18 | |||
19 | /** @var \Illuminate\Support\Collection */ |
||
20 | protected $allowedFields; |
||
21 | |||
22 | /** @var string|null */ |
||
23 | protected $defaultSort; |
||
24 | |||
25 | /** @var \Illuminate\Support\Collection */ |
||
26 | protected $allowedSorts; |
||
27 | |||
28 | /** @var \Illuminate\Support\Collection */ |
||
29 | protected $allowedIncludes; |
||
30 | |||
31 | /** @var \Illuminate\Support\Collection */ |
||
32 | protected $allowedAppends; |
||
33 | |||
34 | /** @var \Illuminate\Support\Collection */ |
||
35 | protected $fields; |
||
36 | |||
37 | /** @var \Illuminate\Support\Collection */ |
||
38 | protected $appends; |
||
39 | |||
40 | /** @var \Illuminate\Http\Request */ |
||
41 | protected $request; |
||
42 | |||
43 | public function __construct(Builder $builder, ? Request $request = null) |
||
44 | { |
||
45 | parent::__construct(clone $builder->getQuery()); |
||
46 | |||
47 | $this->appends = Collection::make(); |
||
48 | |||
49 | $this->initializeFromBuilder($builder); |
||
50 | |||
51 | $this->request = $request ?? request(); |
||
52 | |||
53 | if ($this->request->fields()->isNotEmpty()) { |
||
54 | $this->parseSelectedFields(); |
||
55 | } |
||
56 | |||
57 | if ($this->request->sorts()->isNotEmpty()) { |
||
58 | $this->allowedSorts('*'); |
||
59 | } |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Add the model, scopes, eager loaded relationships, local macro's and onDelete callback |
||
64 | * from the $builder to this query builder. |
||
65 | * |
||
66 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
67 | */ |
||
68 | protected function initializeFromBuilder(Builder $builder) |
||
69 | { |
||
70 | $this->setModel($builder->getModel()) |
||
|
|||
71 | ->setEagerLoads($builder->getEagerLoads()); |
||
72 | |||
73 | $builder->macro('getProtected', function (Builder $builder, string $property) { |
||
74 | return $builder->{$property}; |
||
75 | }); |
||
76 | |||
77 | $this->scopes = $builder->getProtected('scopes'); |
||
78 | |||
79 | $this->localMacros = $builder->getProtected('localMacros'); |
||
80 | |||
81 | $this->onDelete = $builder->getProtected('onDelete'); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Create a new QueryBuilder for a request and model. |
||
86 | * |
||
87 | * @param string|\Illuminate\Database\Query\Builder $baseQuery Model class or base query builder |
||
88 | * @param Request $request |
||
89 | * |
||
90 | * @return \Spatie\QueryBuilder\QueryBuilder |
||
91 | */ |
||
92 | public static function for($baseQuery, ? Request $request = null) : self |
||
93 | { |
||
94 | if (is_string($baseQuery)) { |
||
95 | $baseQuery = ($baseQuery)::query(); |
||
96 | } |
||
97 | |||
98 | return new static($baseQuery, $request ?? request()); |
||
99 | } |
||
100 | |||
101 | public function allowedFilters($filters) : self |
||
102 | { |
||
103 | $filters = is_array($filters) ? $filters : func_get_args(); |
||
104 | $this->allowedFilters = collect($filters)->map(function ($filter) { |
||
105 | if ($filter instanceof Filter) { |
||
106 | return $filter; |
||
107 | } |
||
108 | |||
109 | return Filter::partial($filter); |
||
110 | }); |
||
111 | |||
112 | $this->guardAgainstUnknownFilters(); |
||
113 | |||
114 | $this->addFiltersToQuery($this->request->filters()); |
||
115 | |||
116 | return $this; |
||
117 | } |
||
118 | |||
119 | public function allowedFields($fields) : self |
||
120 | { |
||
121 | $fields = is_array($fields) ? $fields : func_get_args(); |
||
122 | |||
123 | $this->allowedFields = collect($fields) |
||
124 | ->map(function (string $fieldName) { |
||
125 | if (! str_contains($fieldName, '.')) { |
||
126 | $modelTableName = $this->getModel()->getTable(); |
||
127 | |||
128 | return "{$modelTableName}.{$fieldName}"; |
||
129 | } |
||
130 | |||
131 | return $fieldName; |
||
132 | }); |
||
133 | |||
134 | if (! $this->allowedFields->contains('*')) { |
||
135 | $this->guardAgainstUnknownFields(); |
||
136 | } |
||
137 | |||
138 | return $this; |
||
139 | } |
||
140 | |||
141 | public function defaultSort($sort) : self |
||
142 | { |
||
143 | $this->defaultSort = $sort; |
||
144 | |||
145 | $this->addSortsToQuery($this->request->sorts($this->defaultSort)); |
||
146 | |||
147 | return $this; |
||
148 | } |
||
149 | |||
150 | public function allowedSorts($sorts) : self |
||
151 | { |
||
152 | $sorts = is_array($sorts) ? $sorts : func_get_args(); |
||
153 | if (! $this->request->sorts()) { |
||
154 | return $this; |
||
155 | } |
||
156 | |||
157 | $this->allowedSorts = collect($sorts); |
||
158 | |||
159 | if (! $this->allowedSorts->contains('*')) { |
||
160 | $this->guardAgainstUnknownSorts(); |
||
161 | } |
||
162 | |||
163 | $this->addSortsToQuery($this->request->sorts($this->defaultSort)); |
||
164 | |||
165 | return $this; |
||
166 | } |
||
167 | |||
168 | public function allowedIncludes($includes) : self |
||
169 | { |
||
170 | $includes = is_array($includes) ? $includes : func_get_args(); |
||
171 | |||
172 | $this->allowedIncludes = collect($includes) |
||
173 | ->flatMap(function ($include) { |
||
174 | return collect(explode('.', $include)) |
||
175 | ->reduce(function ($collection, $include) { |
||
176 | if ($collection->isEmpty()) { |
||
177 | return $collection->push($include); |
||
178 | } |
||
179 | |||
180 | return $collection->push("{$collection->last()}.{$include}"); |
||
181 | }, collect()); |
||
182 | }); |
||
183 | |||
184 | $this->guardAgainstUnknownIncludes(); |
||
185 | |||
186 | $this->addIncludesToQuery($this->request->includes()); |
||
187 | |||
188 | return $this; |
||
189 | } |
||
190 | |||
191 | public function allowedAppends($appends) : self |
||
192 | { |
||
193 | $appends = is_array($appends) ? $appends : func_get_args(); |
||
194 | |||
195 | $this->allowedAppends = collect($appends); |
||
196 | |||
197 | $this->guardAgainstUnknownAppends(); |
||
198 | |||
199 | $this->appends = $this->request->appends(); |
||
200 | |||
201 | return $this; |
||
202 | } |
||
203 | |||
204 | protected function parseSelectedFields() |
||
205 | { |
||
206 | $this->fields = $this->request->fields(); |
||
207 | |||
208 | $modelTableName = $this->getModel()->getTable(); |
||
209 | $modelFields = $this->fields->get($modelTableName, ['*']); |
||
210 | |||
211 | $this->select($this->prependFieldsWithTableName($modelFields, $modelTableName)); |
||
212 | } |
||
213 | |||
214 | protected function prependFieldsWithTableName(array $fields, string $tableName): array |
||
220 | |||
221 | protected function getFieldsForRelatedTable(string $relation): array |
||
229 | |||
230 | protected function addFiltersToQuery(Collection $filters) |
||
238 | |||
239 | protected function findFilter(string $property) : ? Filter |
||
246 | |||
247 | protected function addSortsToQuery(Collection $sorts) |
||
248 | { |
||
249 | $this->filterDuplicates($sorts) |
||
250 | ->each(function (string $sort) { |
||
251 | $descending = $sort[0] === '-'; |
||
252 | |||
253 | $key = ltrim($sort, '-'); |
||
254 | |||
255 | $this->orderBy($key, $descending ? 'desc' : 'asc'); |
||
256 | }); |
||
258 | |||
259 | protected function filterDuplicates(Collection $sorts): Collection |
||
277 | |||
278 | protected function addIncludesToQuery(Collection $includes) |
||
305 | |||
306 | public function setAppendsToResult($result) |
||
316 | |||
317 | protected function guardAgainstUnknownFilters() |
||
329 | |||
330 | protected function guardAgainstUnknownFields() |
||
349 | |||
350 | protected function guardAgainstUnknownSorts() |
||
362 | |||
363 | protected function guardAgainstUnknownIncludes() |
||
373 | |||
374 | protected function guardAgainstUnknownAppends() |
||
384 | |||
385 | protected function guardAgainstUnreachableCallback($requiredAppends) |
||
396 | |||
397 | /** |
||
398 | * @param string|string[] $appends |
||
399 | * @param callable $callback |
||
400 | * @return $this |
||
401 | */ |
||
402 | public function whenAppended($appends, callable $callback): self |
||
410 | |||
411 | private function shouldExecuteAppendedCallback($appends) |
||
427 | |||
428 | public function get($columns = ['*']) |
||
438 | } |
||
439 |
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.