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 | /** @var string */ |
||
44 | protected $modelTableName; |
||
45 | |||
46 | public function __construct(Builder $builder, ? Request $request = null) |
||
47 | { |
||
48 | parent::__construct(clone $builder->getQuery()); |
||
49 | |||
50 | $this->initializeFromBuilder($builder); |
||
51 | |||
52 | $this->request = $request ?? request(); |
||
53 | |||
54 | $this->modelTableName = $this->getModel()->getTable(); |
||
|
|||
55 | |||
56 | if ($this->request->fields()->isNotEmpty()) { |
||
57 | $this->parseSelectedFields(); |
||
58 | } |
||
59 | |||
60 | if ($this->request->sorts()->isNotEmpty()) { |
||
61 | $this->allowedSorts('*'); |
||
62 | } |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Add the model, scopes, eager loaded relationships, local macro's and onDelete callback |
||
67 | * from the $builder to this query builder. |
||
68 | * |
||
69 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
70 | */ |
||
71 | protected function initializeFromBuilder(Builder $builder) |
||
72 | { |
||
73 | $this->setModel($builder->getModel()) |
||
74 | ->setEagerLoads($builder->getEagerLoads()); |
||
75 | |||
76 | $builder->macro('getProtected', function (Builder $builder, string $property) { |
||
77 | return $builder->{$property}; |
||
78 | }); |
||
79 | |||
80 | $this->scopes = $builder->getProtected('scopes'); |
||
81 | |||
82 | $this->localMacros = $builder->getProtected('localMacros'); |
||
83 | |||
84 | $this->onDelete = $builder->getProtected('onDelete'); |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Create a new QueryBuilder for a request and model. |
||
89 | * |
||
90 | * @param string|\Illuminate\Database\Query\Builder $baseQuery Model class or base query builder |
||
91 | * @param Request $request |
||
92 | * |
||
93 | * @return \Spatie\QueryBuilder\QueryBuilder |
||
94 | */ |
||
95 | public static function for($baseQuery, ? Request $request = null) : self |
||
96 | { |
||
97 | if (is_string($baseQuery)) { |
||
98 | $baseQuery = ($baseQuery)::query(); |
||
99 | } |
||
100 | |||
101 | return new static($baseQuery, $request ?? request()); |
||
102 | } |
||
103 | |||
104 | public function allowedFilters($filters) : self |
||
105 | { |
||
106 | $filters = is_array($filters) ? $filters : func_get_args(); |
||
107 | $this->allowedFilters = collect($filters)->map(function ($filter) { |
||
108 | if ($filter instanceof Filter) { |
||
109 | return $filter; |
||
110 | } |
||
111 | |||
112 | return Filter::partial($filter); |
||
113 | }); |
||
114 | |||
115 | $this->guardAgainstUnknownFilters(); |
||
116 | |||
117 | $this->addFiltersToQuery($this->request->filters()); |
||
118 | |||
119 | return $this; |
||
120 | } |
||
121 | |||
122 | public function allowedFields($fields) : self |
||
123 | { |
||
124 | $fields = is_array($fields) ? $fields : func_get_args(); |
||
125 | |||
126 | $this->allowedFields = collect($fields) |
||
127 | ->map(function (string $fieldName) { |
||
128 | if (! str_contains($fieldName, '.')) { |
||
129 | return "{$this->modelTableName}.{$fieldName}"; |
||
130 | } |
||
131 | |||
132 | return $fieldName; |
||
133 | }); |
||
134 | |||
135 | if (! $this->allowedFields->contains('*')) { |
||
136 | $this->guardAgainstUnknownFields(); |
||
137 | } |
||
138 | |||
139 | return $this; |
||
140 | } |
||
141 | |||
142 | public function defaultSort($sort) : self |
||
143 | { |
||
144 | $this->defaultSort = $sort; |
||
145 | |||
146 | $this->addSortsToQuery($this->request->sorts($this->defaultSort)); |
||
147 | |||
148 | return $this; |
||
149 | } |
||
150 | |||
151 | public function allowedSorts($sorts) : self |
||
152 | { |
||
153 | $sorts = is_array($sorts) ? $sorts : func_get_args(); |
||
154 | if (! $this->request->sorts()) { |
||
155 | return $this; |
||
156 | } |
||
157 | |||
158 | $this->allowedSorts = collect($sorts); |
||
159 | |||
160 | if (! $this->allowedSorts->contains('*')) { |
||
161 | $this->guardAgainstUnknownSorts(); |
||
162 | } |
||
163 | |||
164 | $this->addSortsToQuery($this->request->sorts($this->defaultSort)); |
||
165 | |||
166 | return $this; |
||
167 | } |
||
168 | |||
169 | public function allowedIncludes($includes) : self |
||
170 | { |
||
171 | $includes = is_array($includes) ? $includes : func_get_args(); |
||
172 | |||
173 | $this->allowedIncludes = collect($includes) |
||
174 | ->flatMap(function ($include) { |
||
175 | return collect(explode('.', $include)) |
||
176 | ->reduce(function ($collection, $include) { |
||
177 | if ($collection->isEmpty()) { |
||
178 | return $collection->push($include); |
||
179 | } |
||
180 | |||
181 | return $collection->push("{$collection->last()}.{$include}"); |
||
182 | }, collect()); |
||
183 | }); |
||
184 | |||
185 | $this->guardAgainstUnknownIncludes(); |
||
186 | |||
187 | $this->addIncludesToQuery($this->request->includes()); |
||
188 | |||
189 | return $this; |
||
190 | } |
||
191 | |||
192 | public function allowedAppends($appends) : self |
||
193 | { |
||
194 | $appends = is_array($appends) ? $appends : func_get_args(); |
||
195 | |||
196 | $this->allowedAppends = collect($appends); |
||
197 | |||
198 | $this->guardAgainstUnknownAppends(); |
||
199 | |||
200 | $this->appends = $this->request->appends(); |
||
201 | |||
202 | return $this; |
||
203 | } |
||
204 | |||
205 | protected function parseSelectedFields() |
||
206 | { |
||
207 | $this->fields = $this->request->fields(); |
||
208 | |||
209 | $modelFields = $this->fields->get($this->modelTableName, ['*']); |
||
210 | |||
211 | $this->select($this->prependFieldsWithTableName($modelFields, $this->modelTableName)); |
||
212 | } |
||
213 | |||
214 | protected function prependFieldsWithTableName(array $fields, string $tableName): array |
||
220 | |||
221 | protected function getFieldsForRelatedTable(string $relation): array |
||
222 | { |
||
223 | if (! $this->fields) { |
||
224 | return ['*']; |
||
225 | } |
||
226 | |||
227 | return $this->fields->get($relation, []); |
||
228 | } |
||
229 | |||
230 | protected function addFiltersToQuery(Collection $filters) |
||
231 | { |
||
238 | |||
239 | protected function findFilter(string $property) : ? Filter |
||
246 | |||
247 | protected function addSortsToQuery(Collection $sorts) |
||
262 | |||
263 | protected function filterDuplicates(Collection $sorts): Collection |
||
287 | |||
288 | protected function addIncludesToQuery(Collection $includes) |
||
315 | |||
316 | public function setAppendsToResult($result) |
||
326 | |||
327 | protected function guardAgainstUnknownFilters() |
||
339 | |||
340 | protected function guardAgainstUnknownFields() |
||
359 | |||
360 | protected function guardAgainstUnknownSorts() |
||
372 | |||
373 | protected function guardAgainstUnknownIncludes() |
||
383 | |||
384 | protected function guardAgainstUnknownAppends() |
||
394 | |||
395 | public function get($columns = ['*']) |
||
405 | |||
406 | public function getDbColumns(string $tableName) |
||
410 | } |
||
411 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: