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 array */ |
||
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->initializeFromBuilder($builder); |
||
48 | |||
49 | $this->request = $request ?? request(); |
||
50 | |||
51 | $this->parseSelectedFields(); |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Add the model, scopes, eager loaded relationships, local macro's and onDelete callback |
||
56 | * from the $builder to this query builder. |
||
57 | * |
||
58 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
59 | */ |
||
60 | protected function initializeFromBuilder(Builder $builder) |
||
75 | |||
76 | /** |
||
77 | * Create a new QueryBuilder for a request and model. |
||
78 | * |
||
79 | * @param string|\Illuminate\Database\Query\Builder $baseQuery Model class or base query builder |
||
80 | * @param Request $request |
||
81 | * |
||
82 | * @return \Spatie\QueryBuilder\QueryBuilder |
||
83 | */ |
||
84 | public static function for($baseQuery, ? Request $request = null) : self |
||
92 | |||
93 | public function allowedFilters($filters) : self |
||
110 | |||
111 | public function allowedFields($fields) : self |
||
112 | { |
||
113 | $fields = is_array($fields) ? $fields : func_get_args(); |
||
114 | |||
115 | $this->allowedFields = collect($fields) |
||
116 | ->map(function (string $fieldName) { |
||
117 | if (! str_contains($fieldName, '.')) { |
||
118 | $modelTableName = $this->getModel()->getTable(); |
||
119 | |||
120 | return "{$modelTableName}.{$fieldName}"; |
||
121 | } |
||
122 | |||
123 | return $fieldName; |
||
124 | }); |
||
125 | |||
126 | if (! $this->allowedFields->contains('*')) { |
||
127 | $this->guardAgainstUnknownFields(); |
||
128 | } |
||
129 | |||
130 | return $this; |
||
131 | } |
||
132 | |||
133 | public function defaultSort($sort) : self |
||
134 | { |
||
135 | $this->defaultSort = $sort; |
||
136 | |||
137 | $this->addSortsToQuery($this->request->sorts($this->defaultSort)); |
||
138 | |||
139 | return $this; |
||
140 | } |
||
141 | |||
142 | public function allowedSorts($sorts) : self |
||
143 | { |
||
144 | $sorts = is_array($sorts) ? $sorts : func_get_args(); |
||
145 | if (! $this->request->sorts()) { |
||
146 | return $this; |
||
147 | } |
||
148 | |||
149 | $this->allowedSorts = collect($sorts)->map(function ($sort) { |
||
150 | if ($sort instanceof Sort) { |
||
151 | return $sort; |
||
152 | } |
||
153 | |||
154 | return Sort::field(ltrim($sort, '-')); |
||
155 | }); |
||
156 | |||
157 | $this->guardAgainstUnknownSorts(); |
||
158 | |||
159 | $this->addSortsToQuery($this->request->sorts($this->defaultSort)); |
||
160 | |||
161 | return $this; |
||
162 | } |
||
163 | |||
164 | public function allowedIncludes($includes) : self |
||
186 | |||
187 | public function allowedAppends($appends) : self |
||
199 | |||
200 | protected function parseSelectedFields() |
||
201 | { |
||
202 | $this->fields = $this->request->fields(); |
||
203 | |||
204 | $modelTableName = $this->getModel()->getTable(); |
||
205 | $modelFields = $this->fields->get($modelTableName, ['*']); |
||
206 | |||
207 | $this->select($this->prependFieldsWithTableName($modelFields, $modelTableName)); |
||
208 | } |
||
209 | |||
210 | protected function prependFieldsWithTableName(array $fields, string $tableName): array |
||
211 | { |
||
212 | return array_map(function ($field) use ($tableName) { |
||
213 | return "{$tableName}.{$field}"; |
||
214 | }, $fields); |
||
215 | } |
||
216 | |||
217 | protected function getFieldsForRelatedTable(string $relation): array |
||
218 | { |
||
219 | if (! $this->fields) { |
||
220 | return ['*']; |
||
221 | } |
||
222 | |||
223 | return $this->fields->get($relation, []); |
||
224 | } |
||
225 | |||
226 | protected function addFiltersToQuery(Collection $filters) |
||
227 | { |
||
228 | $filters->each(function ($value, $property) { |
||
229 | $filter = $this->findFilter($property); |
||
230 | |||
231 | $filter->filter($this, $value); |
||
232 | }); |
||
233 | } |
||
234 | |||
235 | protected function findFilter(string $property) : ? Filter |
||
236 | { |
||
237 | return $this->allowedFilters |
||
238 | ->first(function (Filter $filter) use ($property) { |
||
239 | return $filter->isForProperty($property); |
||
240 | }); |
||
241 | } |
||
242 | |||
243 | protected function addSortsToQuery(Collection $sorts) |
||
244 | { |
||
245 | $this->filterDuplicates($sorts) |
||
246 | ->each(function (string $property) { |
||
247 | $descending = $property[0] === '-'; |
||
248 | |||
249 | $key = ltrim($property, '-'); |
||
250 | |||
251 | $sort = $this->findSort($key); |
||
252 | |||
253 | $sort->sort($this, $descending); |
||
254 | }); |
||
255 | } |
||
256 | |||
257 | protected function filterDuplicates(Collection $sorts): Collection |
||
275 | |||
276 | protected function findSort(string $property) : ? Sort |
||
277 | { |
||
278 | return $this->allowedSorts |
||
279 | ->first(function (Sort $sort) use ($property) { |
||
280 | return $sort->isForProperty($property); |
||
281 | }); |
||
282 | } |
||
283 | |||
284 | protected function addDefaultSorts() |
||
285 | { |
||
286 | $this->allowedSorts = collect($this->request->sorts($this->defaultSort))->map(function ($sort) { |
||
296 | |||
297 | protected function addIncludesToQuery(Collection $includes) |
||
324 | |||
325 | public function setAppendsToResult($result) |
||
335 | |||
336 | protected function guardAgainstUnknownFilters() |
||
348 | |||
349 | protected function guardAgainstUnknownFields() |
||
368 | |||
369 | protected function guardAgainstUnknownSorts() |
||
383 | |||
384 | protected function guardAgainstUnknownIncludes() |
||
394 | |||
395 | protected function guardAgainstUnknownAppends() |
||
405 | |||
406 | public function getQuery() |
||
414 | |||
415 | public function get($columns = ['*']) |
||
429 | |||
430 | public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null) |
||
438 | |||
439 | public function simplePaginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null) |
||
447 | } |
||
448 |
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.