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, SearchTrait; |
||
30 | |||
31 | /** |
||
32 | * Boot method for trait |
||
33 | * |
||
34 | */ |
||
35 | public static function bootRelatedPlusTrait() |
||
47 | |||
48 | /** |
||
49 | * Get the table associated with the model. |
||
50 | * |
||
51 | * @return string |
||
52 | */ |
||
53 | abstract public function getTable(); |
||
54 | |||
55 | /** |
||
56 | * Add joins for one or more relations |
||
57 | * This determines the foreign key relations automatically to prevent the need to figure out the columns. |
||
58 | * Usages: |
||
59 | * $query->modelJoin('customers') |
||
60 | * $query->modelJoin('customer.client') |
||
61 | * |
||
62 | * @param Builder $query |
||
63 | * @param string $relationName |
||
64 | * @param string $operator |
||
65 | * @param string $type |
||
66 | * @param bool $where |
||
67 | * @param bool $relatedSelect |
||
68 | * @param string|null $direction |
||
69 | * |
||
70 | * @return Builder |
||
71 | */ |
||
72 | public function scopeModelJoin( |
||
96 | |||
97 | /** |
||
98 | * Add select for related table fields |
||
99 | * |
||
100 | * @param Builder $query |
||
101 | * @param \stdClass $table |
||
102 | * @return Builder |
||
103 | */ |
||
104 | public function selectRelated(Builder $query, $table) |
||
116 | |||
117 | /** |
||
118 | * Set the order of a model |
||
119 | * |
||
120 | * @param Builder $query |
||
121 | * @param string $orderField |
||
122 | * @param string $direction |
||
123 | * @return Builder |
||
124 | */ |
||
125 | public function scopeOrderByCustom(Builder $query, $orderField, $direction) |
||
133 | |||
134 | /** |
||
135 | * Use a model method to add columns or joins if in the order options |
||
136 | * |
||
137 | * @param Builder $query |
||
138 | * @param string $order |
||
139 | * @return Builder |
||
140 | */ |
||
141 | public function scopeOrderByWith(Builder $query, $order) |
||
165 | |||
166 | /** |
||
167 | * Join a model |
||
168 | * |
||
169 | * @param Builder $query |
||
170 | * @param \stdClass $table |
||
171 | * @param Relation $relation |
||
172 | * @param string $operator |
||
173 | * @param string $type |
||
174 | * @param boolean $where |
||
175 | * @param null $direction |
||
176 | * @return Builder |
||
177 | */ |
||
178 | public function scopeRelationJoin( |
||
203 | |||
204 | /** |
||
205 | * Join a HasOne relation which is ordered |
||
206 | * |
||
207 | * @param Relation $relation |
||
208 | * @param JoinClause $join |
||
209 | * @return JoinClause |
||
210 | */ |
||
211 | private function hasOneJoin($relation, $join) |
||
218 | |||
219 | /** |
||
220 | * Get join sql for a HasOne relation |
||
221 | * |
||
222 | * @param Relation $relation |
||
223 | * @param array $order |
||
224 | * @return Expression |
||
225 | */ |
||
226 | public function hasOneJoinSql($relation, $order) |
||
240 | |||
241 | /** |
||
242 | * Adds a where for a relation's join columns and and min/max for a given column |
||
243 | * |
||
244 | * @param Builder $query |
||
245 | * @param Relation $relation |
||
246 | * @param string $column |
||
247 | * @param string $direction |
||
248 | * @return Builder |
||
249 | */ |
||
250 | public function joinOne($query, $relation, $column, $direction) |
||
261 | |||
262 | /** |
||
263 | * Adds a select for a min or max on the given column, depending on direction given |
||
264 | * |
||
265 | * @param Builder $query |
||
266 | * @param string $column |
||
267 | * @param string $direction |
||
268 | * @return Builder |
||
269 | */ |
||
270 | public function selectMinMax($query, $column, $direction) |
||
281 | |||
282 | /** |
||
283 | * Join a HasMany Relation |
||
284 | * |
||
285 | * @param Relation $relation |
||
286 | * @param JoinClause $join |
||
287 | * @param \stdClass $table |
||
288 | * @param string $operator |
||
289 | * @param string $direction |
||
290 | * @return Builder|JoinClause |
||
291 | */ |
||
292 | protected function hasManyJoin($relation, $join, $table, $operator, $direction) |
||
315 | |||
316 | /** |
||
317 | * Add wheres if they exist for a relation |
||
318 | * |
||
319 | * @param Builder|JoinClause $builder |
||
320 | * @param Relation|BelongsTo|HasOneOrMany $relation |
||
321 | * @param string $table |
||
322 | * @return Builder|JoinClause $builder |
||
323 | */ |
||
324 | protected function addRelatedWhereConstraints($builder, $relation, $table) |
||
340 | |||
341 | /** |
||
342 | * If the relation is one-to-many, just get the first related record |
||
343 | * |
||
344 | * @param JoinClause $joinClause |
||
345 | * @param string $column |
||
346 | * @param HasMany|Relation $relation |
||
347 | * @param string $table |
||
348 | * @param string $direction |
||
349 | * |
||
350 | * @return JoinClause |
||
351 | */ |
||
352 | public function hasManyJoinWhere(JoinClause $joinClause, $column, $relation, $table, $direction) |
||
372 | |||
373 | /** |
||
374 | * Add orderBy if orders exist for a relation |
||
375 | * |
||
376 | * @param Builder|JoinClause $builder |
||
377 | * @param Relation|BelongsTo|HasOneOrMany $relation |
||
378 | * @param string $table |
||
379 | * @return Builder|JoinClause $builder |
||
380 | */ |
||
381 | protected function addOrder($builder, $relation, $table) |
||
393 | |||
394 | /** |
||
395 | * Add where statements for the model search fields |
||
396 | * |
||
397 | * @param Builder $query |
||
398 | * @param string $searchText |
||
399 | * @return Builder |
||
400 | */ |
||
401 | public function scopeSearch(Builder $query, $searchText = '') |
||
416 | |||
417 | /** |
||
418 | * Switch a query to be a subquery of a model |
||
419 | * |
||
420 | * @param Builder $query |
||
421 | * @param Builder $model |
||
422 | * @return Builder |
||
423 | */ |
||
424 | public function scopeSetSubquery(Builder $query, $model) |
||
433 | |||
434 | /** |
||
435 | * Set the model order |
||
436 | * |
||
437 | * @param Builder $query |
||
438 | * @param string $column |
||
439 | * @param string $direction |
||
440 | * @return Builder |
||
441 | */ |
||
442 | public function scopeSetCustomOrder(Builder $query, $column, $direction) |
||
451 | |||
452 | /** |
||
453 | * Check if column being sorted by is from a related model |
||
454 | * |
||
455 | * @param Builder $query |
||
456 | * @param string $column |
||
457 | * @param string $direction |
||
458 | * @return Builder |
||
459 | */ |
||
460 | public function scopeOrderByCheckModel(Builder $query, $column, $direction) |
||
472 | } |
||
473 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.