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( |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Join a HasOne relation which is ordered |
||
| 210 | * |
||
| 211 | * @param Relation $relation |
||
| 212 | * @param JoinClause $join |
||
| 213 | * @return JoinClause |
||
| 214 | */ |
||
| 215 | private function hasOneJoin($relation, $join) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Get join sql for a HasOne relation |
||
| 225 | * |
||
| 226 | * @param Relation $relation |
||
| 227 | * @param array $order |
||
| 228 | * @return Expression |
||
| 229 | */ |
||
| 230 | public function hasOneJoinSql($relation, $order) |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Join a HasMany Relation |
||
| 247 | * |
||
| 248 | * @param Relation $relation |
||
| 249 | * @param JoinClause $join |
||
| 250 | * @param \stdClass $table |
||
| 251 | * @param string $operator |
||
| 252 | * @param string $direction |
||
| 253 | * @return Builder|JoinClause |
||
| 254 | */ |
||
| 255 | protected function hasManyJoin($relation, $join, $table, $operator, $direction) |
||
| 278 | |||
| 279 | /** |
||
| 280 | * If the relation is one-to-many, just get the first related record |
||
| 281 | * |
||
| 282 | * @param JoinClause $joinClause |
||
| 283 | * @param string $column |
||
| 284 | * @param HasMany|Relation $relation |
||
| 285 | * @param string $table |
||
| 286 | * @param string $direction |
||
| 287 | * |
||
| 288 | * @return JoinClause |
||
| 289 | */ |
||
| 290 | public function hasManyJoinWhere(JoinClause $joinClause, $column, $relation, $table, $direction) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Adds a where for a relation's join columns and and min/max for a given column |
||
| 313 | * |
||
| 314 | * @param Builder $query |
||
| 315 | * @param Relation $relation |
||
| 316 | * @param string $column |
||
| 317 | * @param string $direction |
||
| 318 | * @return Builder |
||
| 319 | */ |
||
| 320 | public function joinOne($query, $relation, $column, $direction) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Adds a select for a min or max on the given column, depending on direction given |
||
| 334 | * |
||
| 335 | * @param Builder $query |
||
| 336 | * @param string $column |
||
| 337 | * @param string $direction |
||
| 338 | * @return Builder |
||
| 339 | */ |
||
| 340 | public function selectMinMax($query, $column, $direction) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Add wheres if they exist for a relation |
||
| 354 | * |
||
| 355 | * @param Builder|JoinClause $builder |
||
| 356 | * @param Relation|BelongsTo|HasOneOrMany $relation |
||
| 357 | * @param string $table |
||
| 358 | * @return Builder|JoinClause $builder |
||
| 359 | */ |
||
| 360 | protected function addRelatedWhereConstraints($builder, $relation, $table) |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Add orderBy if orders exist for a relation |
||
| 379 | * |
||
| 380 | * @param Builder|JoinClause $builder |
||
| 381 | * @param Relation|BelongsTo|HasOneOrMany $relation |
||
| 382 | * @param string $table |
||
| 383 | * @return Builder|JoinClause $builder |
||
| 384 | */ |
||
| 385 | protected function addOrder($builder, $relation, $table) |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Add where statements for the model search fields |
||
| 400 | * |
||
| 401 | * @param Builder $query |
||
| 402 | * @param string $searchText |
||
| 403 | * @return Builder |
||
| 404 | */ |
||
| 405 | public function scopeSearch(Builder $query, $searchText = '') |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Switch a query to be a subquery of a model |
||
| 423 | * |
||
| 424 | * @param Builder $query |
||
| 425 | * @param Builder $model |
||
| 426 | * @return Builder |
||
| 427 | */ |
||
| 428 | public function scopeSetSubquery(Builder $query, $model) |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Set the model order |
||
| 440 | * |
||
| 441 | * @param Builder $query |
||
| 442 | * @param string $column |
||
| 443 | * @param string $direction |
||
| 444 | * @return Builder |
||
| 445 | */ |
||
| 446 | public function scopeSetCustomOrder(Builder $query, $column, $direction) |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Check if column being sorted by is from a related model |
||
| 458 | * |
||
| 459 | * @param Builder $query |
||
| 460 | * @param string $column |
||
| 461 | * @param string $direction |
||
| 462 | * @return Builder |
||
| 463 | */ |
||
| 464 | public function scopeOrderByCheckModel(Builder $query, $column, $direction) |
||
| 477 | } |
||
| 478 |
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.