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; |
||
| 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( |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Set the order of a model |
||
| 229 | * |
||
| 230 | * @param Builder $query |
||
| 231 | * @param string $orderField |
||
| 232 | * @param string $direction |
||
| 233 | * @return Builder |
||
| 234 | */ |
||
| 235 | public function scopeOrderByCustom(Builder $query, $orderField, $direction) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Use a model method to add columns or joins if in the order options |
||
| 246 | * |
||
| 247 | * @param Builder $query |
||
| 248 | * @param string $order |
||
| 249 | * @return Builder |
||
| 250 | */ |
||
| 251 | public function scopeOrderByWith(Builder $query, $order) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Join a model |
||
| 278 | * |
||
| 279 | * @param Builder $query |
||
| 280 | * @param string $tableName |
||
| 281 | * @param string $tableAlias |
||
| 282 | * @param Relation $relation |
||
| 283 | * @param string $operator |
||
| 284 | * @param string $type |
||
| 285 | * @param boolean $where |
||
| 286 | * @param null $direction |
||
| 287 | * @return Builder |
||
| 288 | */ |
||
| 289 | public function scopeRelationJoin( |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Add where statements for the model search fields |
||
| 323 | * |
||
| 324 | * @param Builder $query |
||
| 325 | * @param string $searchText |
||
| 326 | * @return Builder |
||
| 327 | */ |
||
| 328 | public function scopeSearch(Builder $query, $searchText = '') |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Switch a query to be a subquery of a model |
||
| 346 | * |
||
| 347 | * @param Builder $query |
||
| 348 | * @param Builder $model |
||
| 349 | * @return Builder |
||
| 350 | */ |
||
| 351 | public function scopeSetSubquery(Builder $query, $model) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Add where condition to search current model |
||
| 363 | * |
||
| 364 | * @param Builder $query |
||
| 365 | * @param array $searchFieldParameters |
||
| 366 | * @param string $table |
||
| 367 | * @param string $searchColumn |
||
| 368 | * @param string $searchText |
||
| 369 | * @return Builder |
||
| 370 | */ |
||
| 371 | public function searchThis(Builder $query, $searchFieldParameters, $table, $searchColumn, $searchText) |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Adds a select for a min or max on the given column, depending on direction given |
||
| 385 | * |
||
| 386 | * @param Builder $query |
||
| 387 | * @param string $column |
||
| 388 | * @param string $direction |
||
| 389 | * @return Builder |
||
| 390 | */ |
||
| 391 | public function selectMinMax($query, $column, $direction) |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Join a HasOne relation which is ordered |
||
| 405 | * |
||
| 406 | * @param Relation $relation |
||
| 407 | * @param JoinClause $join |
||
| 408 | * @return JoinClause |
||
| 409 | */ |
||
| 410 | private function hasOneJoin($relation, $join) |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Join a HasMany Relation |
||
| 420 | * |
||
| 421 | * @param Relation $relation |
||
| 422 | * @param JoinClause $join |
||
| 423 | * @param string $tableName |
||
| 424 | * @param string $tableAlias |
||
| 425 | * @param string $operator |
||
| 426 | * @param string $direction |
||
| 427 | * @return Builder|JoinClause |
||
| 428 | */ |
||
| 429 | private function hasManyJoin($relation, $join, $tableName, $tableAlias, $operator, $direction) |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Check $order_fields and $order_defaults are set |
||
| 455 | * |
||
| 456 | * @param string $orderField |
||
| 457 | * @param string $direction |
||
| 458 | * @return bool |
||
| 459 | */ |
||
| 460 | private function hasFieldsAndDefaults($orderField, $direction) |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Add where statements for search fields to search for searchText |
||
| 476 | * |
||
| 477 | * @param Builder $query |
||
| 478 | * @param string $searchText |
||
| 479 | * @return Builder |
||
| 480 | */ |
||
| 481 | private function checkSearchFields($query, $searchText) |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Add where statement for a search field |
||
| 498 | * |
||
| 499 | * @param Builder $query |
||
| 500 | * @param string $table |
||
| 501 | * @param string $searchField |
||
| 502 | * @param array $searchFieldParameters |
||
| 503 | * @param string $searchText |
||
| 504 | * @return Builder |
||
| 505 | */ |
||
| 506 | private function checkSearchField($query, $table, $searchField, $searchFieldParameters, $searchText) |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Add where condition to search a relation |
||
| 523 | * |
||
| 524 | * @param Builder $query |
||
| 525 | * @param array $searchFieldParameters |
||
| 526 | * @param string $searchColumn |
||
| 527 | * @param string $searchText |
||
| 528 | * @return Builder |
||
| 529 | */ |
||
| 530 | private function searchRelation(Builder $query, $searchFieldParameters, $searchColumn, $searchText) |
||
| 552 | } |
||
| 553 |