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 |
||
| 26 | trait RelatedPlusTrait |
||
| 27 | { |
||
| 28 | use CustomOrderTrait, HelperMethodTrait, SearchTrait; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Boot method for trait |
||
| 32 | * |
||
| 33 | */ |
||
| 34 | public static function bootRelatedPlusTrait() |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Get the table associated with the model. |
||
| 49 | * |
||
| 50 | * @return string |
||
| 51 | */ |
||
| 52 | abstract public function getTable(); |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Add joins for one or more relations |
||
| 56 | * This determines the foreign key relations automatically to prevent the need to figure out the columns. |
||
| 57 | * Usages: |
||
| 58 | * $query->modelJoin('customers') |
||
| 59 | * $query->modelJoin('customer.client') |
||
| 60 | * |
||
| 61 | * @param Builder $query |
||
| 62 | * @param string $relationName |
||
| 63 | * @param string $operator |
||
| 64 | * @param string $type |
||
| 65 | * @param bool $where |
||
| 66 | * @param bool $relatedSelect |
||
| 67 | * @param string|null $direction |
||
| 68 | * |
||
| 69 | * @return Builder |
||
| 70 | */ |
||
| 71 | public function scopeModelJoin( |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Add select for related table fields |
||
| 98 | * |
||
| 99 | * @param Builder $query |
||
| 100 | * @param \stdClass $table |
||
| 101 | * @return Builder |
||
| 102 | */ |
||
| 103 | public function selectRelated(Builder $query, $table) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Set the order of a model |
||
| 118 | * |
||
| 119 | * @param Builder $query |
||
| 120 | * @param string $orderField |
||
| 121 | * @param string $direction |
||
| 122 | * @return Builder |
||
| 123 | */ |
||
| 124 | public function scopeOrderByCustom(Builder $query, $orderField, $direction) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Use a model method to add columns or joins if in the order options |
||
| 135 | * |
||
| 136 | * @param Builder $query |
||
| 137 | * @param string $order |
||
| 138 | * @return Builder |
||
| 139 | */ |
||
| 140 | public function scopeOrderByWith(Builder $query, $order) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Execute a scope in the order_width settings |
||
| 155 | * |
||
| 156 | * @param Builder $query |
||
| 157 | * @param string $order |
||
| 158 | * @return Builder |
||
| 159 | */ |
||
| 160 | protected function addOrderWith(Builder $query, $order) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Add join from order_fields |
||
| 169 | * |
||
| 170 | * @param Builder $query |
||
| 171 | * @param string $order |
||
| 172 | * @return Builder |
||
| 173 | */ |
||
| 174 | protected function addOrderJoin(Builder $query, $order) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Join a model |
||
| 193 | * |
||
| 194 | * @param Builder $query |
||
| 195 | * @param \stdClass $table |
||
| 196 | * @param Relation $relation |
||
| 197 | * @param string $operator |
||
| 198 | * @param string $type |
||
| 199 | * @param boolean $where |
||
| 200 | * @param null $direction |
||
| 201 | * @return Builder |
||
| 202 | */ |
||
| 203 | public function scopeRelationJoin( |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Join a HasOne relation which is ordered |
||
| 231 | * |
||
| 232 | * @param Relation $relation |
||
| 233 | * @param JoinClause $join |
||
| 234 | * @return JoinClause |
||
| 235 | */ |
||
| 236 | private function hasOneJoin($relation, $join) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Get join sql for a HasOne relation |
||
| 246 | * |
||
| 247 | * @param Relation $relation |
||
| 248 | * @param array $order |
||
| 249 | * @return Expression |
||
| 250 | */ |
||
| 251 | public function hasOneJoinSql($relation, $order) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Adds a where for a relation's join columns and and min/max for a given column |
||
| 268 | * |
||
| 269 | * @param Builder $query |
||
| 270 | * @param Relation $relation |
||
| 271 | * @param string $column |
||
| 272 | * @param string $direction |
||
| 273 | * @return Builder |
||
| 274 | */ |
||
| 275 | public function joinOne($query, $relation, $column, $direction) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Adds a select for a min or max on the given column, depending on direction given |
||
| 289 | * |
||
| 290 | * @param Builder $query |
||
| 291 | * @param string $column |
||
| 292 | * @param string $direction |
||
| 293 | * @return Builder |
||
| 294 | */ |
||
| 295 | public function selectMinMax($query, $column, $direction) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Join a HasMany Relation |
||
| 309 | * |
||
| 310 | * @param Relation $relation |
||
| 311 | * @param JoinClause $join |
||
| 312 | * @param \stdClass $table |
||
| 313 | * @param string $operator |
||
| 314 | * @param string $direction |
||
| 315 | * @return Builder|JoinClause |
||
| 316 | */ |
||
| 317 | protected function hasManyJoin($relation, $join, $table, $operator, $direction) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Add wheres if they exist for a relation |
||
| 343 | * |
||
| 344 | * @param Builder|JoinClause $builder |
||
| 345 | * @param Relation|BelongsTo|HasOneOrMany $relation |
||
| 346 | * @param string $table |
||
| 347 | * @return Builder|JoinClause $builder |
||
| 348 | */ |
||
| 349 | protected function addRelatedWhereConstraints($builder, $relation, $table) |
||
| 365 | |||
| 366 | /** |
||
| 367 | * If the relation is one-to-many, just get the first related record |
||
| 368 | * |
||
| 369 | * @param JoinClause $joinClause |
||
| 370 | * @param string $column |
||
| 371 | * @param HasMany|Relation $relation |
||
| 372 | * @param string $table |
||
| 373 | * @param string $direction |
||
| 374 | * |
||
| 375 | * @return JoinClause |
||
| 376 | */ |
||
| 377 | public function hasManyJoinWhere(JoinClause $joinClause, $column, $relation, $table, $direction) |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Add orderBy if orders exist for a relation |
||
| 400 | * |
||
| 401 | * @param Builder|JoinClause $builder |
||
| 402 | * @param Relation|BelongsTo|HasOneOrMany $relation |
||
| 403 | * @param string $table |
||
| 404 | * @return Builder|JoinClause $builder |
||
| 405 | */ |
||
| 406 | protected function addOrder($builder, $relation, $table) |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Add where statements for the model search fields |
||
| 421 | * |
||
| 422 | * @param Builder $query |
||
| 423 | * @param string $searchText |
||
| 424 | * @return Builder |
||
| 425 | */ |
||
| 426 | public function scopeSearch(Builder $query, $searchText = '') |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Switch a query to be a subquery of a model |
||
| 440 | * |
||
| 441 | * @param Builder $query |
||
| 442 | * @param Builder $model |
||
| 443 | * @return Builder |
||
| 444 | */ |
||
| 445 | public function scopeSetSubquery(Builder $query, $model) |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Set the model order |
||
| 457 | * |
||
| 458 | * @param Builder $query |
||
| 459 | * @param string $column |
||
| 460 | * @param string $direction |
||
| 461 | * @return Builder |
||
| 462 | */ |
||
| 463 | public function scopeSetCustomOrder(Builder $query, $column, $direction) |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Check if column being sorted by is from a related model |
||
| 475 | * |
||
| 476 | * @param Builder $query |
||
| 477 | * @param string $column |
||
| 478 | * @param string $direction |
||
| 479 | * @return Builder |
||
| 480 | */ |
||
| 481 | public function scopeOrderByCheckModel(Builder $query, $column, $direction) |
||
| 493 | } |
||
| 494 |
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.