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 $query |
||
| 54 | * @param string $relationName |
||
| 55 | * @param string $operator |
||
| 56 | * @param string $type |
||
| 57 | * @param bool $where |
||
| 58 | * @param bool $relatedSelect |
||
| 59 | * @param string|null $direction |
||
| 60 | * |
||
| 61 | * @return Builder |
||
| 62 | */ |
||
| 63 | public function scopeModelJoin( |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Get the relations from a relation name |
||
| 98 | * $relationName can be a single relation |
||
| 99 | * Usage for User model: |
||
| 100 | * parseRelationNames('customer') returns [$user->customer()] |
||
| 101 | * parseRelationNames('customer.contact') returns [$user->customer(), $user->customer->contact()] |
||
| 102 | * |
||
| 103 | * @param $relationName |
||
| 104 | * @return Relation[] |
||
| 105 | */ |
||
| 106 | protected function parseRelationNames($relationName) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Join a model |
||
| 126 | * |
||
| 127 | * @param Builder $query |
||
| 128 | * @param string $tableName |
||
| 129 | * @param string $tableAlias |
||
| 130 | * @param Relation $relation |
||
| 131 | * @param string $operator |
||
| 132 | * @param string $type |
||
| 133 | * @param boolean $where |
||
| 134 | * @param null $direction |
||
| 135 | * @return Builder |
||
| 136 | */ |
||
| 137 | public function scopeRelationJoin( |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Get join sql for a HasOne relation |
||
| 193 | * |
||
| 194 | * @param Relation $relation |
||
| 195 | * @param array $order |
||
| 196 | * @return Expression |
||
| 197 | */ |
||
| 198 | public function hasOneJoinSql($relation, $order) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Adds a where for a relation's join columns and and min/max for a given column |
||
| 215 | * |
||
| 216 | * @param Builder $query |
||
| 217 | * @param Relation $relation |
||
| 218 | * @param string $column |
||
| 219 | * @param string $direction |
||
| 220 | * @return Builder |
||
| 221 | */ |
||
| 222 | public function joinOne($query, $relation, $column, $direction) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Get the join columns for a relation |
||
| 236 | * |
||
| 237 | * @param Relation|BelongsTo|HasOneOrMany $relation |
||
| 238 | * @return \stdClass |
||
| 239 | */ |
||
| 240 | protected function getJoinColumns($relation) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Adds a select for a min or max on the given column, depending on direction given |
||
| 256 | * |
||
| 257 | * @param Builder $query |
||
| 258 | * @param string $column |
||
| 259 | * @param string $direction |
||
| 260 | * @return Builder |
||
| 261 | */ |
||
| 262 | public function selectMinMax($query, $column, $direction) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Add backticks to a table/column |
||
| 275 | * |
||
| 276 | * @param $column |
||
| 277 | * @return string |
||
| 278 | */ |
||
| 279 | private function addBackticks($column) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Return the sql for a query with the bindings replaced with the binding values |
||
| 287 | * |
||
| 288 | * @param Builder $builder |
||
| 289 | * @return string |
||
| 290 | */ |
||
| 291 | private function toSqlWithBindings(Builder $builder) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Replace SQL placeholders with '%s' |
||
| 298 | * |
||
| 299 | * @param Builder $builder |
||
| 300 | * @return mixed |
||
| 301 | */ |
||
| 302 | private function replacePlaceholders(Builder $builder) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Add wheres if they exist for a relation |
||
| 309 | * |
||
| 310 | * @param Builder|JoinClause $builder |
||
| 311 | * @param Relation|BelongsTo|HasOneOrMany $relation |
||
| 312 | * @param string $table |
||
| 313 | * @return Builder|JoinClause |
||
| 314 | */ |
||
| 315 | protected function addWhereConstraints($builder, $relation, $table) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Add table name to column name if table name not already included in column name |
||
| 334 | * |
||
| 335 | * @param string $table |
||
| 336 | * @param string $column |
||
| 337 | * @return string |
||
| 338 | */ |
||
| 339 | private function columnWithTableName($table, $column) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * If the relation is one-to-many, just get the first related record |
||
| 346 | * |
||
| 347 | * @param Builder|JoinClause $joinClause |
||
| 348 | * @param string $column |
||
| 349 | * @param HasMany|Relation $relation |
||
| 350 | * @param string $table |
||
| 351 | * @param string $direction |
||
| 352 | * |
||
| 353 | * @return JoinClause |
||
| 354 | */ |
||
| 355 | public function hasManyJoin(JoinClause $joinClause, $column, $relation, $table, $direction) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Add orderBy if orders exist for a relation |
||
| 378 | * |
||
| 379 | * @param Builder|JoinClause $builder |
||
| 380 | * @param Relation|BelongsTo|HasOneOrMany $relation |
||
| 381 | * @param string $table |
||
| 382 | * @return Builder |
||
| 383 | */ |
||
| 384 | protected function addOrder($builder, $relation, $table) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Set the order of a model |
||
| 398 | * |
||
| 399 | * @param Builder $query |
||
| 400 | * @param string $orderField |
||
| 401 | * @param string $dir |
||
| 402 | * @return Builder |
||
| 403 | */ |
||
| 404 | public function scopeOrderByCustom(Builder $query, $orderField, $dir) |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Check if column being sorted by is from a related model |
||
| 427 | * |
||
| 428 | * @param Builder $query |
||
| 429 | * @param string $column |
||
| 430 | * @param string $direction |
||
| 431 | * @return Builder |
||
| 432 | */ |
||
| 433 | public function scopeOrderByCheckModel(Builder $query, $column, $direction) |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Check if this model has already been joined to a table or relation |
||
| 460 | * |
||
| 461 | * @param Builder $builder |
||
| 462 | * @param string $table |
||
| 463 | * @param \Illuminate\Database\Eloquent\Relations\Relation $relation |
||
| 464 | * @return bool |
||
| 465 | */ |
||
| 466 | protected function hasJoin(Builder $builder, $table, $relation) |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Set the model order |
||
| 484 | * |
||
| 485 | * @param Builder $query |
||
| 486 | * @param string $column |
||
| 487 | * @param string $direction |
||
| 488 | * @return Builder |
||
| 489 | */ |
||
| 490 | public function scopeSetCustomOrder(Builder $query, $column, $direction) |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Switch a query to be a subquery of a model |
||
| 517 | * |
||
| 518 | * @param Builder $query |
||
| 519 | * @param Builder $model |
||
| 520 | * @return Builder |
||
| 521 | */ |
||
| 522 | public function scopeSetSubquery(Builder $query, $model) |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Use a model method to add columns or joins if in the order options |
||
| 534 | * |
||
| 535 | * @param Builder $query |
||
| 536 | * @param string $order |
||
| 537 | * @return Builder |
||
| 538 | */ |
||
| 539 | public function scopeOrderByWith(Builder $query, $order) |
||
| 563 | |||
| 564 | /** |
||
| 565 | * Add where statements for the model search fields |
||
| 566 | * |
||
| 567 | * @param Builder $query |
||
| 568 | * @param string $search |
||
| 569 | * @return Builder |
||
| 570 | */ |
||
| 571 | public function scopeSearch(Builder $query, $search = '') |
||
| 631 | } |
||
| 632 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: