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 |
||
29 | trait RelatedPlusTrait |
||
30 | { |
||
31 | use HelperMethodTrait; |
||
32 | |||
33 | /** |
||
34 | * Get the table associated with the model. |
||
35 | * |
||
36 | * @return string |
||
37 | */ |
||
38 | abstract function getTable(); |
||
39 | |||
40 | /** |
||
41 | * Boot method for trait |
||
42 | * |
||
43 | */ |
||
44 | public static function bootRelatedPlusTrait() |
||
56 | |||
57 | /** |
||
58 | * Add joins for one or more relations |
||
59 | * This determines the foreign key relations automatically to prevent the need to figure out the columns. |
||
60 | * Usages: |
||
61 | * $query->modelJoin('customers') |
||
62 | * $query->modelJoin('customer.client') |
||
63 | * |
||
64 | * @param Builder $query |
||
65 | * @param string $relationName |
||
66 | * @param string $operator |
||
67 | * @param string $type |
||
68 | * @param bool $where |
||
69 | * @param bool $relatedSelect |
||
70 | * @param string|null $direction |
||
71 | * |
||
72 | * @return Builder |
||
73 | */ |
||
74 | public function scopeModelJoin( |
||
106 | |||
107 | /** |
||
108 | * Join a model |
||
109 | * |
||
110 | * @param Builder $query |
||
111 | * @param string $tableName |
||
112 | * @param string $tableAlias |
||
113 | * @param Relation $relation |
||
114 | * @param string $operator |
||
115 | * @param string $type |
||
116 | * @param boolean $where |
||
117 | * @param null $direction |
||
118 | * @return Builder |
||
119 | */ |
||
120 | public function scopeRelationJoin( |
||
151 | |||
152 | /** |
||
153 | * Join a HasOne relation which is ordered |
||
154 | * |
||
155 | * @param Relation $relation |
||
156 | * @param JoinClause $join |
||
157 | * @return JoinClause |
||
158 | */ |
||
159 | private function hasOneJoin($relation, $join) |
||
166 | |||
167 | /** |
||
168 | * Get join sql for a HasOne relation |
||
169 | * |
||
170 | * @param Relation $relation |
||
171 | * @param array $order |
||
172 | * @return Expression |
||
173 | */ |
||
174 | public function hasOneJoinSql($relation, $order) |
||
188 | |||
189 | /** |
||
190 | * Adds a where for a relation's join columns and and min/max for a given column |
||
191 | * |
||
192 | * @param Builder $query |
||
193 | * @param Relation $relation |
||
194 | * @param string $column |
||
195 | * @param string $direction |
||
196 | * @return Builder |
||
197 | */ |
||
198 | public function joinOne($query, $relation, $column, $direction) |
||
209 | |||
210 | /** |
||
211 | * Get the join columns for a relation |
||
212 | * |
||
213 | * @param Relation|BelongsTo|HasOneOrMany $relation |
||
214 | * @return \stdClass |
||
215 | */ |
||
216 | protected function getJoinColumns($relation) |
||
229 | |||
230 | /** |
||
231 | * Adds a select for a min or max on the given column, depending on direction given |
||
232 | * |
||
233 | * @param Builder $query |
||
234 | * @param string $column |
||
235 | * @param string $direction |
||
236 | * @return Builder |
||
237 | */ |
||
238 | public function selectMinMax($query, $column, $direction) |
||
249 | |||
250 | /** |
||
251 | * Join a HasMany Relation |
||
252 | * |
||
253 | * @param Relation $relation |
||
254 | * @param JoinClause $join |
||
255 | * @param string $tableName |
||
256 | * @param string $tableAlias |
||
257 | * @param string $operator |
||
258 | * @param string $direction |
||
259 | * @return Builder|JoinClause |
||
260 | */ |
||
261 | private function hasManyJoin($relation, $join, $tableName, $tableAlias, $operator, $direction) |
||
284 | |||
285 | /** |
||
286 | * Add wheres if they exist for a relation |
||
287 | * |
||
288 | * @param Builder|JoinClause $builder |
||
289 | * @param Relation|BelongsTo|HasOneOrMany $relation |
||
290 | * @param string $table |
||
291 | * @return Builder|JoinClause |
||
292 | */ |
||
293 | protected function addWhereConstraints($builder, $relation, $table) |
||
309 | |||
310 | /** |
||
311 | * If the relation is one-to-many, just get the first related record |
||
312 | * |
||
313 | * @param JoinClause $joinClause |
||
314 | * @param string $column |
||
315 | * @param HasMany|Relation $relation |
||
316 | * @param string $table |
||
317 | * @param string $direction |
||
318 | * |
||
319 | * @return JoinClause |
||
320 | */ |
||
321 | public function hasManyJoinWhere(JoinClause $joinClause, $column, $relation, $table, $direction) |
||
341 | |||
342 | /** |
||
343 | * Add orderBy if orders exist for a relation |
||
344 | * |
||
345 | * @param Builder|JoinClause $builder |
||
346 | * @param Relation|BelongsTo|HasOneOrMany $relation |
||
347 | * @param string $table |
||
348 | * @return Builder |
||
349 | */ |
||
350 | protected function addOrder($builder, $relation, $table) |
||
361 | |||
362 | /** |
||
363 | * Set the order of a model |
||
364 | * |
||
365 | * @param Builder $query |
||
366 | * @param string $orderField |
||
367 | * @param string $direction |
||
368 | * @return Builder |
||
369 | */ |
||
370 | public function scopeOrderByCustom(Builder $query, $orderField, $direction) |
||
378 | |||
379 | /** |
||
380 | * Check $order_fields and $order_defaults are set |
||
381 | * |
||
382 | * @param string $orderField |
||
383 | * @param string $direction |
||
384 | * @return bool |
||
385 | */ |
||
386 | private function hasFieldsAndDefaults($orderField, $direction) |
||
399 | |||
400 | /** |
||
401 | * Check if column being sorted by is from a related model |
||
402 | * |
||
403 | * @param Builder $query |
||
404 | * @param string $column |
||
405 | * @param string $direction |
||
406 | * @return Builder |
||
407 | */ |
||
408 | public function scopeOrderByCheckModel(Builder $query, $column, $direction) |
||
421 | |||
422 | /** |
||
423 | * Join a related table if not already joined |
||
424 | * |
||
425 | * @param Builder $query |
||
426 | * @param string $table |
||
427 | * @return Builder |
||
428 | */ |
||
429 | private function joinRelatedTable($query, $table) |
||
446 | |||
447 | /** |
||
448 | * Set the model order |
||
449 | * |
||
450 | * @param Builder $query |
||
451 | * @param string $column |
||
452 | * @param string $direction |
||
453 | * @return Builder |
||
454 | */ |
||
455 | public function scopeSetCustomOrder(Builder $query, $column, $direction) |
||
464 | |||
465 | /** |
||
466 | * Override column if provided column not valid |
||
467 | * |
||
468 | * @param string $column |
||
469 | * @return string |
||
470 | */ |
||
471 | private function setColumn($column) |
||
480 | |||
481 | /** |
||
482 | * Override direction if provided direction not valid |
||
483 | * |
||
484 | * @param string $direction |
||
485 | * @return string |
||
486 | */ |
||
487 | private function setDirection($direction) |
||
496 | |||
497 | /** |
||
498 | * Set order based on order_fields |
||
499 | * |
||
500 | * @param Builder $query |
||
501 | * @param string $column |
||
502 | * @param string $direction |
||
503 | * @return Builder |
||
504 | */ |
||
505 | private function setOrder($query, $column, $direction) |
||
517 | |||
518 | /** |
||
519 | * Switch a query to be a subquery of a model |
||
520 | * |
||
521 | * @param Builder $query |
||
522 | * @param Builder $model |
||
523 | * @return Builder |
||
524 | */ |
||
525 | public function scopeSetSubquery(Builder $query, $model) |
||
534 | |||
535 | /** |
||
536 | * Use a model method to add columns or joins if in the order options |
||
537 | * |
||
538 | * @param Builder $query |
||
539 | * @param string $order |
||
540 | * @return Builder |
||
541 | */ |
||
542 | public function scopeOrderByWith(Builder $query, $order) |
||
566 | |||
567 | /** |
||
568 | * Add where statements for the model search fields |
||
569 | * |
||
570 | * @param Builder $query |
||
571 | * @param string $searchText |
||
572 | * @return Builder |
||
573 | */ |
||
574 | public function scopeSearch(Builder $query, $searchText = '') |
||
589 | |||
590 | /** |
||
591 | * Add where statements for search fields to search for searchText |
||
592 | * |
||
593 | * @param Builder $query |
||
594 | * @param string $searchText |
||
595 | * @return Builder |
||
596 | */ |
||
597 | private function checkSearchFields($query, $searchText) |
||
611 | |||
612 | /** |
||
613 | * Add where statement for a search field |
||
614 | * |
||
615 | * @param Builder $query |
||
616 | * @param string $table |
||
617 | * @param string $searchField |
||
618 | * @param array $searchFieldParameters |
||
619 | * @param string $searchText |
||
620 | * @return Builder |
||
621 | */ |
||
622 | private function checkSearchField($query, $table, $searchField, $searchFieldParameters, $searchText) |
||
636 | |||
637 | /** |
||
638 | * Add where condition to search a relation |
||
639 | * |
||
640 | * @param Builder $query |
||
641 | * @param array $searchFieldParameters |
||
642 | * @param string $searchColumn |
||
643 | * @param string $searchText |
||
644 | * @return Builder |
||
645 | */ |
||
646 | private function searchRelation(Builder $query, $searchFieldParameters, $searchColumn, $searchText) |
||
668 | |||
669 | /** |
||
670 | * Add where condition to search current model |
||
671 | * |
||
672 | * @param Builder $query |
||
673 | * @param array $searchFieldParameters |
||
674 | * @param string $table |
||
675 | * @param string $searchColumn |
||
676 | * @param string $searchText |
||
677 | * @return Builder |
||
678 | */ |
||
679 | public function searchThis(Builder $query, $searchFieldParameters, $table, $searchColumn, $searchText) |
||
690 | } |
||
691 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.