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 | * Add orderBy if orders exist for a relation |
||
33 | * |
||
34 | * @param Builder|JoinClause $builder |
||
35 | * @param Relation|BelongsTo|HasOneOrMany $relation |
||
36 | * @param string $table |
||
37 | * @return Builder|JoinClause $builder |
||
38 | */ |
||
39 | protected function addOrder($builder, $relation, $table) |
||
51 | |||
52 | /** |
||
53 | * Add wheres if they exist for a relation |
||
54 | * |
||
55 | * @param Builder|JoinClause $builder |
||
56 | * @param Relation|BelongsTo|HasOneOrMany $relation |
||
57 | * @param string $table |
||
58 | * @return Builder|JoinClause $builder |
||
59 | */ |
||
60 | protected function addWhereConstraints($builder, $relation, $table) |
||
76 | |||
77 | /** |
||
78 | * Boot method for trait |
||
79 | * |
||
80 | */ |
||
81 | public static function bootRelatedPlusTrait() |
||
93 | |||
94 | /** |
||
95 | * Get the table associated with the model. |
||
96 | * |
||
97 | * @return string |
||
98 | */ |
||
99 | abstract public function getTable(); |
||
100 | |||
101 | /** |
||
102 | * If the relation is one-to-many, just get the first related record |
||
103 | * |
||
104 | * @param JoinClause $joinClause |
||
105 | * @param string $column |
||
106 | * @param HasMany|Relation $relation |
||
107 | * @param string $table |
||
108 | * @param string $direction |
||
109 | * |
||
110 | * @return JoinClause |
||
111 | */ |
||
112 | public function hasManyJoinWhere(JoinClause $joinClause, $column, $relation, $table, $direction) |
||
132 | |||
133 | /** |
||
134 | * Get join sql for a HasOne relation |
||
135 | * |
||
136 | * @param Relation $relation |
||
137 | * @param array $order |
||
138 | * @return Expression |
||
139 | */ |
||
140 | public function hasOneJoinSql($relation, $order) |
||
154 | |||
155 | /** |
||
156 | * Adds a where for a relation's join columns and and min/max for a given column |
||
157 | * |
||
158 | * @param Builder $query |
||
159 | * @param Relation $relation |
||
160 | * @param string $column |
||
161 | * @param string $direction |
||
162 | * @return Builder |
||
163 | */ |
||
164 | public function joinOne($query, $relation, $column, $direction) |
||
175 | |||
176 | /** |
||
177 | * Add joins for one or more relations |
||
178 | * This determines the foreign key relations automatically to prevent the need to figure out the columns. |
||
179 | * Usages: |
||
180 | * $query->modelJoin('customers') |
||
181 | * $query->modelJoin('customer.client') |
||
182 | * |
||
183 | * @param Builder $query |
||
184 | * @param string $relationName |
||
185 | * @param string $operator |
||
186 | * @param string $type |
||
187 | * @param bool $where |
||
188 | * @param bool $relatedSelect |
||
189 | * @param string|null $direction |
||
190 | * |
||
191 | * @return Builder |
||
192 | */ |
||
193 | public function scopeModelJoin( |
||
217 | |||
218 | /** |
||
219 | * Set the order of a model |
||
220 | * |
||
221 | * @param Builder $query |
||
222 | * @param string $orderField |
||
223 | * @param string $direction |
||
224 | * @return Builder |
||
225 | */ |
||
226 | public function scopeOrderByCustom(Builder $query, $orderField, $direction) |
||
234 | |||
235 | /** |
||
236 | * Use a model method to add columns or joins if in the order options |
||
237 | * |
||
238 | * @param Builder $query |
||
239 | * @param string $order |
||
240 | * @return Builder |
||
241 | */ |
||
242 | public function scopeOrderByWith(Builder $query, $order) |
||
266 | |||
267 | /** |
||
268 | * Join a model |
||
269 | * |
||
270 | * @param Builder $query |
||
271 | * @param \stdClass $table |
||
272 | * @param Relation $relation |
||
273 | * @param string $operator |
||
274 | * @param string $type |
||
275 | * @param boolean $where |
||
276 | * @param null $direction |
||
277 | * @return Builder |
||
278 | */ |
||
279 | public function scopeRelationJoin( |
||
308 | |||
309 | /** |
||
310 | * Add where statements for the model search fields |
||
311 | * |
||
312 | * @param Builder $query |
||
313 | * @param string $searchText |
||
314 | * @return Builder |
||
315 | */ |
||
316 | public function scopeSearch(Builder $query, $searchText = '') |
||
331 | |||
332 | /** |
||
333 | * Switch a query to be a subquery of a model |
||
334 | * |
||
335 | * @param Builder $query |
||
336 | * @param Builder $model |
||
337 | * @return Builder |
||
338 | */ |
||
339 | public function scopeSetSubquery(Builder $query, $model) |
||
348 | |||
349 | /** |
||
350 | * Adds a select for a min or max on the given column, depending on direction given |
||
351 | * |
||
352 | * @param Builder $query |
||
353 | * @param string $column |
||
354 | * @param string $direction |
||
355 | * @return Builder |
||
356 | */ |
||
357 | public function selectMinMax($query, $column, $direction) |
||
368 | |||
369 | /** |
||
370 | * Add select for related table fields |
||
371 | * |
||
372 | * @param Builder $query |
||
373 | * @param $table |
||
374 | * @return Builder |
||
375 | */ |
||
376 | public function selectRelated(Builder $query, $table) |
||
388 | |||
389 | /** |
||
390 | * Join a HasOne relation which is ordered |
||
391 | * |
||
392 | * @param Relation $relation |
||
393 | * @param JoinClause $join |
||
394 | * @return JoinClause |
||
395 | */ |
||
396 | private function hasOneJoin($relation, $join) |
||
403 | |||
404 | /** |
||
405 | * Join a HasMany Relation |
||
406 | * |
||
407 | * @param Relation $relation |
||
408 | * @param JoinClause $join |
||
409 | * @param \stdClass $table |
||
410 | * @param string $operator |
||
411 | * @param string $direction |
||
412 | * @return Builder|JoinClause |
||
413 | */ |
||
414 | private function hasManyJoin($relation, $join, $table, $operator, $direction) |
||
437 | |||
438 | } |
||
439 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.