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 |
||
28 | trait RelatedPlusTrait |
||
29 | { |
||
30 | /** |
||
31 | * Boot method for trait |
||
32 | * |
||
33 | */ |
||
34 | public static function bootRelatedPlusTrait() |
||
46 | |||
47 | /** |
||
48 | * Add joins for one or more relations |
||
49 | * This determines the foreign key relations automatically to prevent the need to figure out the columns. |
||
50 | * Usages: |
||
51 | * $query->modelJoin('customers') |
||
52 | * $query->modelJoin('customer.client') |
||
53 | * |
||
54 | * @param Builder $query |
||
55 | * @param string $relationName |
||
56 | * @param string $operator |
||
57 | * @param string $type |
||
58 | * @param bool $where |
||
59 | * @param bool $relatedSelect |
||
60 | * @param string|null $direction |
||
61 | * |
||
62 | * @return Builder |
||
63 | */ |
||
64 | public function scopeModelJoin( |
||
96 | |||
97 | /** |
||
98 | * Get the relations from a relation name |
||
99 | * $relationName can be a single relation |
||
100 | * Usage for User model: |
||
101 | * parseRelationNames('customer') returns [$user->customer()] |
||
102 | * parseRelationNames('customer.contact') returns [$user->customer(), $user->customer->contact()] |
||
103 | * |
||
104 | * @param string $relationName |
||
105 | * @return Relation[] |
||
106 | */ |
||
107 | protected function parseRelationNames($relationName) |
||
124 | |||
125 | /** |
||
126 | * Join a model |
||
127 | * |
||
128 | * @param Builder $query |
||
129 | * @param string $tableName |
||
130 | * @param string $tableAlias |
||
131 | * @param Relation $relation |
||
132 | * @param string $operator |
||
133 | * @param string $type |
||
134 | * @param boolean $where |
||
135 | * @param null $direction |
||
136 | * @return Builder |
||
137 | */ |
||
138 | public function scopeRelationJoin( |
||
169 | |||
170 | /** |
||
171 | * Join a HasOne relation which is ordered |
||
172 | * |
||
173 | * @param Relation $relation |
||
174 | * @param JoinClause $join |
||
175 | * @return JoinClause |
||
176 | */ |
||
177 | private function hasOneJoin($relation, $join) |
||
184 | |||
185 | /** |
||
186 | * Get join sql for a HasOne relation |
||
187 | * |
||
188 | * @param Relation $relation |
||
189 | * @param array $order |
||
190 | * @return Expression |
||
191 | */ |
||
192 | public function hasOneJoinSql($relation, $order) |
||
206 | |||
207 | /** |
||
208 | * Adds a where for a relation's join columns and and min/max for a given column |
||
209 | * |
||
210 | * @param Builder $query |
||
211 | * @param Relation $relation |
||
212 | * @param string $column |
||
213 | * @param string $direction |
||
214 | * @return Builder |
||
215 | */ |
||
216 | public function joinOne($query, $relation, $column, $direction) |
||
227 | |||
228 | /** |
||
229 | * Get the join columns for a relation |
||
230 | * |
||
231 | * @param Relation|BelongsTo|HasOneOrMany $relation |
||
232 | * @return \stdClass |
||
233 | */ |
||
234 | protected function getJoinColumns($relation) |
||
247 | |||
248 | /** |
||
249 | * Adds a select for a min or max on the given column, depending on direction given |
||
250 | * |
||
251 | * @param Builder $query |
||
252 | * @param string $column |
||
253 | * @param string $direction |
||
254 | * @return Builder |
||
255 | */ |
||
256 | public function selectMinMax($query, $column, $direction) |
||
266 | |||
267 | /** |
||
268 | * Add backticks to a table/column |
||
269 | * |
||
270 | * @param string $column |
||
271 | * @return string |
||
272 | */ |
||
273 | private function addBackticks($column) |
||
278 | |||
279 | /** |
||
280 | * Return the sql for a query with the bindings replaced with the binding values |
||
281 | * |
||
282 | * @param Builder $builder |
||
283 | * @return string |
||
284 | */ |
||
285 | private function toSqlWithBindings(Builder $builder) |
||
298 | |||
299 | /** |
||
300 | * Join a HasMany Relation |
||
301 | * |
||
302 | * @param Relation $relation |
||
303 | * @param JoinClause $join |
||
304 | * @param string $tableName |
||
305 | * @param string $tableAlias |
||
306 | * @param string $operator |
||
307 | * @param string $direction |
||
308 | * @return Builder|JoinClause |
||
309 | */ |
||
310 | private function hasManyJoin($relation, $join, $tableName, $tableAlias, $operator, $direction) |
||
333 | |||
334 | /** |
||
335 | * Add wheres if they exist for a relation |
||
336 | * |
||
337 | * @param Builder|JoinClause $builder |
||
338 | * @param Relation|BelongsTo|HasOneOrMany $relation |
||
339 | * @param string $table |
||
340 | * @return Builder|JoinClause |
||
341 | */ |
||
342 | protected function addWhereConstraints($builder, $relation, $table) |
||
358 | |||
359 | /** |
||
360 | * Add table name to column name if table name not already included in column name |
||
361 | * |
||
362 | * @param string $table |
||
363 | * @param string $column |
||
364 | * @return string |
||
365 | */ |
||
366 | private function columnWithTableName($table, $column) |
||
370 | |||
371 | /** |
||
372 | * If the relation is one-to-many, just get the first related record |
||
373 | * |
||
374 | * @param Builder|JoinClause $joinClause |
||
375 | * @param string $column |
||
376 | * @param HasMany|Relation $relation |
||
377 | * @param string $table |
||
378 | * @param string $direction |
||
379 | * |
||
380 | * @return JoinClause |
||
381 | */ |
||
382 | public function hasManyJoinWhere(JoinClause $joinClause, $column, $relation, $table, $direction) |
||
402 | |||
403 | /** |
||
404 | * Add orderBy if orders exist for a relation |
||
405 | * |
||
406 | * @param Builder|JoinClause $builder |
||
407 | * @param Relation|BelongsTo|HasOneOrMany $relation |
||
408 | * @param string $table |
||
409 | * @return Builder |
||
410 | */ |
||
411 | protected function addOrder($builder, $relation, $table) |
||
422 | |||
423 | /** |
||
424 | * Set the order of a model |
||
425 | * |
||
426 | * @param Builder $query |
||
427 | * @param string $orderField |
||
428 | * @param string $direction |
||
429 | * @return Builder |
||
430 | */ |
||
431 | public function scopeOrderByCustom(Builder $query, $orderField, $direction) |
||
439 | |||
440 | /** |
||
441 | * Check $order_fields and $order_defaults are set |
||
442 | * |
||
443 | * @param $orderField |
||
444 | * @param $direction |
||
445 | * @return bool |
||
446 | */ |
||
447 | private function fieldsCheck($orderField, $direction) |
||
460 | |||
461 | /** |
||
462 | * Remove order global scope if it exists |
||
463 | * |
||
464 | * @param Builder $query |
||
465 | * @return Builder |
||
466 | */ |
||
467 | private function removeOrderGlobalScope($query) |
||
477 | |||
478 | /** |
||
479 | * Check if column being sorted by is from a related model |
||
480 | * |
||
481 | * @param Builder $query |
||
482 | * @param string $column |
||
483 | * @param string $direction |
||
484 | * @return Builder |
||
485 | */ |
||
486 | public function scopeOrderByCheckModel(Builder $query, $column, $direction) |
||
498 | |||
499 | /** |
||
500 | * Join a related table if not already joined |
||
501 | * |
||
502 | * @param Builder $query |
||
503 | * @param string $table |
||
504 | * @return Builder |
||
505 | */ |
||
506 | private function joinRelatedTable($query, $table) |
||
523 | |||
524 | /** |
||
525 | * Check if this model has already been joined to a table or relation |
||
526 | * |
||
527 | * @param Builder $builder |
||
528 | * @param string $table |
||
529 | * @param \Illuminate\Database\Eloquent\Relations\Relation $relation |
||
530 | * @return bool |
||
531 | */ |
||
532 | protected function hasJoin(Builder $builder, $table, $relation) |
||
540 | |||
541 | /** |
||
542 | * Check if model is currently joined to $table |
||
543 | * |
||
544 | * @param Builder $builder |
||
545 | * @param string $table |
||
546 | * @return bool |
||
547 | */ |
||
548 | private function isJoined(Builder $builder, $table) |
||
561 | |||
562 | /** |
||
563 | * Check if relation exists in eager loads |
||
564 | * |
||
565 | * @param Builder $builder |
||
566 | * @param \Illuminate\Database\Eloquent\Relations\Relation $relation |
||
567 | * @return bool |
||
568 | */ |
||
569 | private function isEagerLoaded(Builder $builder, $relation) |
||
575 | |||
576 | /** |
||
577 | * Set the model order |
||
578 | * |
||
579 | * @param Builder $query |
||
580 | * @param string $column |
||
581 | * @param string $direction |
||
582 | * @return Builder |
||
583 | */ |
||
584 | public function scopeSetCustomOrder(Builder $query, $column, $direction) |
||
593 | |||
594 | /** |
||
595 | * Override column if provided column not valid |
||
596 | * |
||
597 | * @param $column |
||
598 | * @return string |
||
599 | */ |
||
600 | private function setColumn($column) |
||
609 | |||
610 | /** |
||
611 | * Override direction if provided direction not valid |
||
612 | * |
||
613 | * @param string $direction |
||
614 | * @return string |
||
615 | */ |
||
616 | private function setDirection($direction) |
||
625 | |||
626 | /** |
||
627 | * Set order based on order_fields |
||
628 | * |
||
629 | * @param Builder $query |
||
630 | * @param string $column |
||
631 | * @param string $direction |
||
632 | * @return Builder |
||
633 | */ |
||
634 | private function setOrder($query, $column, $direction) |
||
646 | |||
647 | /** |
||
648 | * Switch a query to be a subquery of a model |
||
649 | * |
||
650 | * @param Builder $query |
||
651 | * @param Builder $model |
||
652 | * @return Builder |
||
653 | */ |
||
654 | public function scopeSetSubquery(Builder $query, $model) |
||
663 | |||
664 | /** |
||
665 | * Use a model method to add columns or joins if in the order options |
||
666 | * |
||
667 | * @param Builder $query |
||
668 | * @param string $order |
||
669 | * @return Builder |
||
670 | */ |
||
671 | public function scopeOrderByWith(Builder $query, $order) |
||
695 | |||
696 | /** |
||
697 | * Add where statements for the model search fields |
||
698 | * |
||
699 | * @param Builder $query |
||
700 | * @param string $searchText |
||
701 | * @return Builder |
||
702 | */ |
||
703 | public function scopeSearch(Builder $query, $searchText = '') |
||
718 | |||
719 | /** |
||
720 | * Add where statements for search fields to search for searchText |
||
721 | * |
||
722 | * @param Builder $query |
||
723 | * @param string $searchText |
||
724 | * @return Builder |
||
725 | */ |
||
726 | private function checkSearchFields($query, $searchText) |
||
740 | |||
741 | /** |
||
742 | * Add where statement for a search field |
||
743 | * |
||
744 | * @param Builder $query |
||
745 | * @param string $table |
||
746 | * @param string $searchField |
||
747 | * @param array $searchFieldParameters |
||
748 | * @param string $searchText |
||
749 | * @return Builder |
||
750 | */ |
||
751 | private function checkSearchField($query, $table, $searchField, $searchFieldParameters, $searchText) |
||
765 | |||
766 | /** |
||
767 | * Add where condition to search a relation |
||
768 | * |
||
769 | * @param Builder $query |
||
770 | * @param array $searchFieldParameters |
||
771 | * @param string $searchColumn |
||
772 | * @param string $searchText |
||
773 | * @return Builder |
||
774 | */ |
||
775 | private function searchRelation(Builder $query, $searchFieldParameters, $searchColumn, $searchText) |
||
797 | |||
798 | /** |
||
799 | * Add where condition to search current model |
||
800 | * |
||
801 | * @param Builder $query |
||
802 | * @param array $searchFieldParameters |
||
803 | * @param string $table |
||
804 | * @param string $searchColumn |
||
805 | * @param string $searchText |
||
806 | * @return Builder |
||
807 | */ |
||
808 | public function searchThis(Builder $query, $searchFieldParameters, $table, $searchColumn, $searchText) |
||
819 | } |
||
820 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: