Complex classes like RelatedPlusTrait 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 RelatedPlusTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | trait RelatedPlusTrait |
||
28 | { |
||
29 | use CustomOrderTrait, HelperMethodTrait, JoinsTrait, SearchTrait; |
||
30 | |||
31 | /** |
||
32 | * Boot method for trait |
||
33 | * |
||
34 | */ |
||
35 | public static function bootRelatedPlusTrait() |
||
44 | |||
45 | /** |
||
46 | * Set empty fields to null |
||
47 | */ |
||
48 | protected function setAttributesNull() |
||
56 | |||
57 | /** |
||
58 | * Get the table associated with the model. |
||
59 | * |
||
60 | * @return string |
||
61 | */ |
||
62 | abstract public function getTable(); |
||
63 | |||
64 | /** |
||
65 | * Add joins for one or more relations |
||
66 | * This determines the foreign key relations automatically to prevent the need to figure out the columns. |
||
67 | * Usages: |
||
68 | * $query->modelJoin('customers') |
||
69 | * $query->modelJoin('customer.client') |
||
70 | * |
||
71 | * @param Builder $query |
||
72 | * @param string $relationName |
||
73 | * @param string $operator |
||
74 | * @param string $type |
||
75 | * @param bool $where |
||
76 | * @param bool $relatedSelect |
||
77 | * @param string|null $direction |
||
78 | * |
||
79 | * @return Builder |
||
80 | */ |
||
81 | public function scopeModelJoin( |
||
101 | |||
102 | /** |
||
103 | * Add selects for model join |
||
104 | * |
||
105 | * @param Builder $query |
||
106 | * @param \stdClass $table |
||
107 | * @param bool $relatedSelect |
||
108 | * @return mixed |
||
109 | */ |
||
110 | protected function modelJoinSelects($query, $table, $relatedSelect) |
||
122 | |||
123 | /** |
||
124 | * Add select for related table fields |
||
125 | * |
||
126 | * @param Builder $query |
||
127 | * @param \stdClass $table |
||
128 | * @return Builder |
||
129 | */ |
||
130 | public function selectRelated(Builder $query, $table) |
||
142 | |||
143 | /** |
||
144 | * Set the order of a model |
||
145 | * |
||
146 | * @param Builder $query |
||
147 | * @param string $orderField |
||
148 | * @param string $direction |
||
149 | * @return Builder |
||
150 | */ |
||
151 | public function scopeOrderByCustom(Builder $query, $orderField, $direction) |
||
159 | |||
160 | /** |
||
161 | * Use a model method to add columns or joins if in the order options |
||
162 | * |
||
163 | * @param Builder $query |
||
164 | * @param string $order |
||
165 | * @return Builder |
||
166 | */ |
||
167 | public function scopeOrderByWith(Builder $query, $order) |
||
179 | |||
180 | /** |
||
181 | * Execute a scope in the order_width settings |
||
182 | * |
||
183 | * @param Builder $query |
||
184 | * @param string $order |
||
185 | * @return Builder |
||
186 | */ |
||
187 | protected function addOrderWith(Builder $query, $order) |
||
193 | |||
194 | /** |
||
195 | * Add join from order_fields |
||
196 | * |
||
197 | * @param Builder $query |
||
198 | * @param string $order |
||
199 | * @return Builder |
||
200 | */ |
||
201 | protected function addOrderJoin(Builder $query, $order) |
||
217 | |||
218 | /** |
||
219 | * Join a model |
||
220 | * |
||
221 | * @param Builder $query |
||
222 | * @param \stdClass $table |
||
223 | * @param Relation $relation |
||
224 | * @param string $operator |
||
225 | * @param string $type |
||
226 | * @param boolean $where |
||
227 | * @param string $direction |
||
228 | * @return Builder |
||
229 | */ |
||
230 | public function scopeRelationJoin( |
||
250 | |||
251 | /** |
||
252 | * If the relation is one-to-many, just get the first related record |
||
253 | * |
||
254 | * @param JoinClause $joinClause |
||
255 | * @param string $column |
||
256 | * @param HasMany|Relation $relation |
||
257 | * @param string $table |
||
258 | * @param string $direction |
||
259 | * |
||
260 | * @return JoinClause |
||
261 | */ |
||
262 | public function hasManyJoinWhere(JoinClause $joinClause, $column, $relation, $table, $direction) |
||
282 | |||
283 | /** |
||
284 | * Adds a where for a relation's join columns and and min/max for a given column |
||
285 | * |
||
286 | * @param Builder $query |
||
287 | * @param Relation $relation |
||
288 | * @param string $column |
||
289 | * @param string $direction |
||
290 | * @return Builder |
||
291 | */ |
||
292 | public function joinOne($query, $relation, $column, $direction) |
||
303 | |||
304 | /** |
||
305 | * Adds a select for a min or max on the given column, depending on direction given |
||
306 | * |
||
307 | * @param Builder $query |
||
308 | * @param string $column |
||
309 | * @param string $direction |
||
310 | * @return Builder |
||
311 | */ |
||
312 | public function selectMinMax($query, $column, $direction) |
||
323 | |||
324 | /** |
||
325 | * Add wheres if they exist for a relation |
||
326 | * |
||
327 | * @param Builder|JoinClause $builder |
||
328 | * @param Relation|BelongsTo|HasOneOrMany $relation |
||
329 | * @param string $table |
||
330 | * @return Builder|JoinClause $builder |
||
331 | */ |
||
332 | protected function addRelatedWhereConstraints($builder, $relation, $table) |
||
348 | |||
349 | /** |
||
350 | * Add orderBy if orders exist for a relation |
||
351 | * |
||
352 | * @param Builder|JoinClause $builder |
||
353 | * @param Relation|BelongsTo|HasOneOrMany $relation |
||
354 | * @param string $table |
||
355 | * @return Builder|JoinClause $builder |
||
356 | */ |
||
357 | protected function addOrder($builder, $relation, $table) |
||
369 | |||
370 | /** |
||
371 | * Add where statements for the model search fields |
||
372 | * |
||
373 | * @param Builder $query |
||
374 | * @param string $searchText |
||
375 | * @return Builder |
||
376 | */ |
||
377 | public function scopeSearch(Builder $query, $searchText = '') |
||
388 | |||
389 | /** |
||
390 | * Switch a query to be a subquery of a model |
||
391 | * |
||
392 | * @param Builder $query |
||
393 | * @param Builder $model |
||
394 | * @return Builder |
||
395 | */ |
||
396 | public function scopeSetSubquery(Builder $query, $model) |
||
405 | |||
406 | /** |
||
407 | * Set the model order |
||
408 | * |
||
409 | * @param Builder $query |
||
410 | * @param string $column |
||
411 | * @param string $direction |
||
412 | * @return Builder |
||
413 | */ |
||
414 | public function scopeSetCustomOrder(Builder $query, $column, $direction) |
||
423 | |||
424 | /** |
||
425 | * Check if column being sorted by is from a related model |
||
426 | * |
||
427 | * @param Builder $query |
||
428 | * @param string $column |
||
429 | * @param string $direction |
||
430 | * @return Builder |
||
431 | */ |
||
432 | public function scopeOrderByCheckModel(Builder $query, $column, $direction) |
||
444 | } |
||
445 |
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: