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 | /** |
||
| 30 | * Boot method for trait |
||
| 31 | * |
||
| 32 | */ |
||
| 33 | public static function bootRelatedPlusTrait() |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Add joins for one or more relations |
||
| 48 | * This determines the foreign key relations automatically to prevent the need to figure out the columns. |
||
| 49 | * Usages: |
||
| 50 | * $query->modelJoin('customers') |
||
| 51 | * $query->modelJoin('customer.client') |
||
| 52 | * |
||
| 53 | * @param Builder|RelatedPlus $query |
||
|
|
|||
| 54 | * @param string $relation_name |
||
| 55 | * @param string $operator |
||
| 56 | * @param string $type |
||
| 57 | * @param bool $where |
||
| 58 | * @param bool $related_select |
||
| 59 | * @param string|null $direction |
||
| 60 | * |
||
| 61 | * @return Builder |
||
| 62 | */ |
||
| 63 | public function scopeModelJoin( |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Get the relations from a relation name |
||
| 100 | * $relation_name can be a single relation |
||
| 101 | * Usage for User model: |
||
| 102 | * parseRelationNames('customer') returns [$user->customer()] |
||
| 103 | * parseRelationNames('customer.contact') returns [$user->customer(), $user->customer->contact()] |
||
| 104 | * |
||
| 105 | * @param $relation_name |
||
| 106 | * @return Relation[] |
||
| 107 | */ |
||
| 108 | protected function parseRelationNames($relation_name) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Join a model |
||
| 128 | * |
||
| 129 | * @param Builder|RelatedPlus $query |
||
| 130 | * @param string $table_name |
||
| 131 | * @param string $table_alias |
||
| 132 | * @param Relation $relation |
||
| 133 | * @param string $operator |
||
| 134 | * @param string $type |
||
| 135 | * @param boolean $where |
||
| 136 | * @param null $direction |
||
| 137 | * @return Builder |
||
| 138 | */ |
||
| 139 | public function scopeRelationJoin( |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Get join sql for a HasOne relation |
||
| 195 | * |
||
| 196 | * @param Relation $relation |
||
| 197 | * @param array $order |
||
| 198 | * @return Expression |
||
| 199 | */ |
||
| 200 | public function hasOneJoinSql($relation, $order) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Adds a where for a relation's join columns and and min/max for a given column |
||
| 217 | * |
||
| 218 | * @param Builder|RelatedPlus $query |
||
| 219 | * @param Relation $relation |
||
| 220 | * @param string $column |
||
| 221 | * @param string $direction |
||
| 222 | * @return mixed |
||
| 223 | */ |
||
| 224 | public function joinOne($query, $relation, $column, $direction) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Get the join columns for a relation |
||
| 238 | * |
||
| 239 | * @param Relation|BelongsTo|HasOneOrMany $relation |
||
| 240 | * @return \stdClass |
||
| 241 | */ |
||
| 242 | protected function getJoinColumns($relation) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Adds a select for a min or max on the given column, depending on direction given |
||
| 258 | * |
||
| 259 | * @param Builder|RelatedPlus $query |
||
| 260 | * @param string $column |
||
| 261 | * @param string $direction |
||
| 262 | * @return Builder |
||
| 263 | */ |
||
| 264 | public function selectMinMax($query, $column, $direction) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Add backticks to a table/column |
||
| 277 | * |
||
| 278 | * @param $column |
||
| 279 | * @return string |
||
| 280 | */ |
||
| 281 | private function addBackticks($column) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Return the sql for a query with the bindings replaced with the binding values |
||
| 289 | * |
||
| 290 | * @param Builder $builder |
||
| 291 | * @return string |
||
| 292 | */ |
||
| 293 | private function toSqlWithBindings(Builder $builder) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Replace SQL placeholders with '%s' |
||
| 300 | * |
||
| 301 | * @param Builder $builder |
||
| 302 | * @return mixed |
||
| 303 | */ |
||
| 304 | private function replacePlaceholders(Builder $builder) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Add wheres if they exist for a relation |
||
| 311 | * |
||
| 312 | * @param Builder|JoinClause $builder |
||
| 313 | * @param Relation|BelongsTo|HasOneOrMany $relation |
||
| 314 | * @param string $table |
||
| 315 | * @return Builder|JoinClause |
||
| 316 | */ |
||
| 317 | protected function addWhereConstraints($builder, $relation, $table) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Add table name to column name if table name not already included in column name |
||
| 336 | * |
||
| 337 | * @param string $table |
||
| 338 | * @param string $column |
||
| 339 | * @return string |
||
| 340 | */ |
||
| 341 | private function columnWithTableName($table, $column) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * If the relation is one-to-many, just get the first related record |
||
| 348 | * |
||
| 349 | * @param JoinClause $joinClause |
||
| 350 | * @param string $column |
||
| 351 | * @param HasMany|Relation $relation |
||
| 352 | * @param string $table |
||
| 353 | * @param string $direction |
||
| 354 | * |
||
| 355 | * @return JoinClause |
||
| 356 | */ |
||
| 357 | public function hasManyJoin(JoinClause $joinClause, $column, $relation, $table, $direction) |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Add orderBy if orders exist for a relation |
||
| 380 | * |
||
| 381 | * @param Builder|JoinClause $builder |
||
| 382 | * @param Relation|BelongsTo|HasOneOrMany $relation |
||
| 383 | * @param string $table |
||
| 384 | * @return Builder |
||
| 385 | */ |
||
| 386 | protected function addOrder($builder, $relation, $table) |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Set the order of a model |
||
| 400 | * |
||
| 401 | * @param Builder|RelatedPlus $query |
||
| 402 | * @param string $order_field |
||
| 403 | * @param string $dir |
||
| 404 | * @return Builder |
||
| 405 | */ |
||
| 406 | public function scopeOrderByCustom(Builder $query, $order_field, $dir) |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Check if column being sorted by is from a related model |
||
| 431 | * |
||
| 432 | * @param Builder|RelatedPlus $query |
||
| 433 | * @param string $column |
||
| 434 | * @param string $direction |
||
| 435 | * @return Builder |
||
| 436 | */ |
||
| 437 | public function scopeOrderByCheckModel(Builder $query, $column, $direction) |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Check if this model has already been joined to a table or relation |
||
| 464 | * |
||
| 465 | * @param Builder $builder |
||
| 466 | * @param string $table |
||
| 467 | * @param \Illuminate\Database\Eloquent\Relations\Relation $relation |
||
| 468 | * @return bool |
||
| 469 | */ |
||
| 470 | protected function hasJoin(Builder $builder, $table, $relation) |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Set the model order |
||
| 488 | * |
||
| 489 | * @param Builder|RelatedPlus $query |
||
| 490 | * @param string $column |
||
| 491 | * @param string $direction |
||
| 492 | * @return Builder |
||
| 493 | */ |
||
| 494 | public function scopeSetCustomOrder(Builder $query, $column, $direction) |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Switch a query to be a subquery of a model |
||
| 521 | * |
||
| 522 | * @param Builder|RelatedPlus $query |
||
| 523 | * @param Builder $model |
||
| 524 | * @return Builder |
||
| 525 | */ |
||
| 526 | public function scopeSetSubquery(Builder $query, $model) |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Use a model method to add columns or joins if in the order options |
||
| 538 | * |
||
| 539 | * @param Builder|RelatedPlus $query |
||
| 540 | * @param string $order |
||
| 541 | * @return Builder |
||
| 542 | */ |
||
| 543 | public function scopeOrderByWith(Builder $query, $order) |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Add where statements for the model search fields |
||
| 570 | * |
||
| 571 | * @param Builder|RelatedPlus $query |
||
| 572 | * @param string $search |
||
| 573 | * @return Builder |
||
| 574 | */ |
||
| 575 | public function scopeSearch(Builder $query, $search = '') |
||
| 635 | } |
||
| 636 |