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 | * Join a HasMany Relation |
||
209 | * |
||
210 | * @param Relation $relation |
||
211 | * @param JoinClause $join |
||
212 | * @param string $tableName |
||
213 | * @param string $tableAlias |
||
214 | * @param string $operator |
||
215 | * @param string $direction |
||
216 | * @return Builder|JoinClause |
||
217 | */ |
||
218 | private function hasManyJoin($relation, $join, $tableName, $tableAlias, $operator, $direction) |
||
241 | |||
242 | /** |
||
243 | * Adds a where for a relation's join columns and and min/max for a given column |
||
244 | * |
||
245 | * @param Builder $query |
||
246 | * @param Relation $relation |
||
247 | * @param string $column |
||
248 | * @param string $direction |
||
249 | * @return Builder |
||
250 | */ |
||
251 | public function joinOne($query, $relation, $column, $direction) |
||
262 | |||
263 | /** |
||
264 | * Get the join columns for a relation |
||
265 | * |
||
266 | * @param Relation|BelongsTo|HasOneOrMany $relation |
||
267 | * @return \stdClass |
||
268 | */ |
||
269 | protected function getJoinColumns($relation) |
||
282 | |||
283 | /** |
||
284 | * Adds a select for a min or max on the given column, depending on direction given |
||
285 | * |
||
286 | * @param Builder $query |
||
287 | * @param string $column |
||
288 | * @param string $direction |
||
289 | * @return Builder |
||
290 | */ |
||
291 | public function selectMinMax($query, $column, $direction) |
||
301 | |||
302 | /** |
||
303 | * Add backticks to a table/column |
||
304 | * |
||
305 | * @param string $column |
||
306 | * @return string |
||
307 | */ |
||
308 | private function addBackticks($column) |
||
313 | |||
314 | /** |
||
315 | * Return the sql for a query with the bindings replaced with the binding values |
||
316 | * |
||
317 | * @param Builder $builder |
||
318 | * @return string |
||
319 | */ |
||
320 | private function toSqlWithBindings(Builder $builder) |
||
324 | |||
325 | /** |
||
326 | * Replace SQL placeholders with '%s' |
||
327 | * |
||
328 | * @param Builder $builder |
||
329 | * @return string |
||
330 | */ |
||
331 | private function replacePlaceholders(Builder $builder) |
||
335 | |||
336 | /** |
||
337 | * Add wheres if they exist for a relation |
||
338 | * |
||
339 | * @param Builder|JoinClause $builder |
||
340 | * @param Relation|BelongsTo|HasOneOrMany $relation |
||
341 | * @param string $table |
||
342 | * @return Builder|JoinClause |
||
343 | */ |
||
344 | protected function addWhereConstraints($builder, $relation, $table) |
||
360 | |||
361 | /** |
||
362 | * Add table name to column name if table name not already included in column name |
||
363 | * |
||
364 | * @param string $table |
||
365 | * @param string $column |
||
366 | * @return string |
||
367 | */ |
||
368 | private function columnWithTableName($table, $column) |
||
372 | |||
373 | /** |
||
374 | * If the relation is one-to-many, just get the first related record |
||
375 | * |
||
376 | * @param JoinClause $joinClause |
||
377 | * @param string $column |
||
378 | * @param HasMany|Relation $relation |
||
379 | * @param string $table |
||
380 | * @param string $direction |
||
381 | * |
||
382 | * @return JoinClause |
||
383 | */ |
||
384 | public function hasManyJoinWhere(JoinClause $joinClause, $column, $relation, $table, $direction) |
||
404 | |||
405 | /** |
||
406 | * Add orderBy if orders exist for a relation |
||
407 | * |
||
408 | * @param Builder|JoinClause $builder |
||
409 | * @param Relation|BelongsTo|HasOneOrMany $relation |
||
410 | * @param string $table |
||
411 | * @return Builder |
||
412 | */ |
||
413 | protected function addOrder($builder, $relation, $table) |
||
424 | |||
425 | /** |
||
426 | * Set the order of a model |
||
427 | * |
||
428 | * @param Builder $query |
||
429 | * @param string $orderField |
||
430 | * @param string $dir |
||
431 | * @return Builder |
||
432 | */ |
||
433 | public function scopeOrderByCustom(Builder $query, $orderField, $dir) |
||
453 | |||
454 | /** |
||
455 | * Check if column being sorted by is from a related model |
||
456 | * |
||
457 | * @param Builder $query |
||
458 | * @param string $column |
||
459 | * @param string $direction |
||
460 | * @return Builder |
||
461 | */ |
||
462 | public function scopeOrderByCheckModel(Builder $query, $column, $direction) |
||
486 | |||
487 | /** |
||
488 | * Check if this model has already been joined to a table or relation |
||
489 | * |
||
490 | * @param Builder $builder |
||
491 | * @param string $table |
||
492 | * @param \Illuminate\Database\Eloquent\Relations\Relation $relation |
||
493 | * @return bool |
||
494 | */ |
||
495 | protected function hasJoin(Builder $builder, $table, $relation) |
||
503 | |||
504 | /** |
||
505 | * Check if model is currently joined to $table |
||
506 | * |
||
507 | * @param Builder $builder |
||
508 | * @param string $table |
||
509 | * @return bool |
||
510 | */ |
||
511 | private function checkJoins(Builder $builder, $table) |
||
524 | |||
525 | /** |
||
526 | * Check if relation exists in eager loads |
||
527 | * |
||
528 | * @param Builder $builder |
||
529 | * @param \Illuminate\Database\Eloquent\Relations\Relation $relation |
||
530 | * @return bool |
||
531 | */ |
||
532 | private function checkEagerLoads(Builder $builder, $relation) |
||
538 | |||
539 | /** |
||
540 | * Set the model order |
||
541 | * |
||
542 | * @param Builder $query |
||
543 | * @param string $column |
||
544 | * @param string $direction |
||
545 | * @return Builder |
||
546 | */ |
||
547 | public function scopeSetCustomOrder(Builder $query, $column, $direction) |
||
571 | |||
572 | /** |
||
573 | * Switch a query to be a subquery of a model |
||
574 | * |
||
575 | * @param Builder $query |
||
576 | * @param Builder $model |
||
577 | * @return Builder |
||
578 | */ |
||
579 | public function scopeSetSubquery(Builder $query, $model) |
||
588 | |||
589 | /** |
||
590 | * Use a model method to add columns or joins if in the order options |
||
591 | * |
||
592 | * @param Builder $query |
||
593 | * @param string $order |
||
594 | * @return Builder |
||
595 | */ |
||
596 | public function scopeOrderByWith(Builder $query, $order) |
||
620 | |||
621 | /** |
||
622 | * Add where statements for the model search fields |
||
623 | * |
||
624 | * @param Builder $query |
||
625 | * @param string $searchText |
||
626 | * @return Builder |
||
627 | */ |
||
628 | public function scopeSearch(Builder $query, $searchText = '') |
||
643 | |||
644 | /** |
||
645 | * Add where statements for search fields to search for searchText |
||
646 | * |
||
647 | * @param Builder $query |
||
648 | * @param string $searchText |
||
649 | * @return Builder |
||
650 | */ |
||
651 | private function checkSearchFields($query, $searchText) |
||
665 | |||
666 | /** |
||
667 | * Add where statement for a search field |
||
668 | * |
||
669 | * @param Builder $query |
||
670 | * @param string $table |
||
671 | * @param string $searchField |
||
672 | * @param array $searchFieldParameters |
||
673 | * @param string $searchText |
||
674 | * @return Builder |
||
675 | */ |
||
676 | private function checkSearchField($query, $table, $searchField, $searchFieldParameters, $searchText) |
||
690 | |||
691 | /** |
||
692 | * Add where condition to search current model |
||
693 | * |
||
694 | * @param Builder $query |
||
695 | * @param array $searchFieldParameters |
||
696 | * @param string $table |
||
697 | * @param string $searchColumn |
||
698 | * @param string $searchText |
||
699 | * @return Builder |
||
700 | */ |
||
701 | public function searchThis(Builder $query, $searchFieldParameters, $table, $searchColumn, $searchText) |
||
712 | |||
713 | /** |
||
714 | * Add where condition to search a relation |
||
715 | * |
||
716 | * @param Builder $query |
||
717 | * @param array $searchFieldParameters |
||
718 | * @param string $searchColumn |
||
719 | * @param string $searchText |
||
720 | * @return Builder |
||
721 | */ |
||
722 | private function searchRelation(Builder $query, $searchFieldParameters, $searchColumn, $searchText) |
||
744 | } |
||
745 |
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: