| Conditions | 10 |
| Paths | 1 |
| Total Lines | 119 |
| Code Lines | 69 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 8 | ||
| Bugs | 3 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 19 | public function getRelationExistenceInQuery(): Closure |
||
| 20 | { |
||
| 21 | return function (Builder $query, Builder $parentQuery, $columns = ['*']): Builder { |
||
|
|
|||
| 22 | $relation = function (Builder $query, Builder $parentQuery, $columns = ['*']): Builder { |
||
| 23 | return $query->select($columns); |
||
| 24 | }; |
||
| 25 | // basic builder |
||
| 26 | $belongsTo = function (Builder $query, Builder $parentQuery) use ($relation): Builder { |
||
| 27 | $columns = $query->qualifyColumn($this->ownerKey); |
||
| 28 | |||
| 29 | $relationQuery = $relation($query, $parentQuery, $columns); |
||
| 30 | |||
| 31 | if ($parentQuery->getQuery()->from == $query->getQuery()->from) { |
||
| 32 | $relationQuery->from( |
||
| 33 | $query->getModel()->getTable().' as '.$hash = $this->getRelationCountHash() |
||
| 34 | ); |
||
| 35 | |||
| 36 | $relationQuery->getModel()->setTable($hash); |
||
| 37 | } |
||
| 38 | |||
| 39 | return $relationQuery; |
||
| 40 | }; |
||
| 41 | $belongsToMany = function (Builder $query, Builder $parentQuery) use ($relation): Builder { |
||
| 42 | $columns = $this->getExistenceCompareKey(); |
||
| 43 | if ($parentQuery->getQuery()->from == $query->getQuery()->from) { |
||
| 44 | $query->select($columns); |
||
| 45 | |||
| 46 | $query->from($this->related->getTable().' as '.$hash = $this->getRelationCountHash()); |
||
| 47 | |||
| 48 | $this->related->setTable($hash); |
||
| 49 | } |
||
| 50 | |||
| 51 | $this->performJoin($query); |
||
| 52 | |||
| 53 | return $relation($query, $parentQuery, $columns); |
||
| 54 | }; |
||
| 55 | $hasOneOrMany = function (Builder $query, Builder $parentQuery) use ($relation): Builder { |
||
| 56 | $columns = $this->getExistenceCompareKey(); |
||
| 57 | if ($query->getQuery()->from == $parentQuery->getQuery()->from) { |
||
| 58 | $query->from($query->getModel()->getTable().' as '.$hash = $this->getRelationCountHash()); |
||
| 59 | |||
| 60 | $query->getModel()->setTable($hash); |
||
| 61 | } |
||
| 62 | |||
| 63 | return $relation($query, $parentQuery, $columns); |
||
| 64 | }; |
||
| 65 | $hasManyThrough = function (Builder $query, Builder $parentQuery) use ($relation): Builder { |
||
| 66 | $columns = $this->getQualifiedFirstKeyName(); |
||
| 67 | if ($parentQuery->getQuery()->from === $query->getQuery()->from) { |
||
| 68 | $query->from($query->getModel()->getTable().' as '.$hash = $this->getRelationCountHash()); |
||
| 69 | |||
| 70 | $query->join($this->throughParent->getTable(), $this->getQualifiedParentKeyName(), '=', $hash.'.'.$this->secondKey); |
||
| 71 | |||
| 72 | if ($this->throughParentSoftDeletes()) { |
||
| 73 | $query->whereNull($this->throughParent->getQualifiedDeletedAtColumn()); |
||
| 74 | } |
||
| 75 | |||
| 76 | $query->getModel()->setTable($hash); |
||
| 77 | |||
| 78 | return $relation($query, $parentQuery, $columns); |
||
| 79 | } |
||
| 80 | |||
| 81 | if ($parentQuery->getQuery()->from === $this->throughParent->getTable()) { |
||
| 82 | $table = $this->throughParent->getTable().' as '.$hash = $this->getRelationCountHash(); |
||
| 83 | |||
| 84 | $query->join($table, $hash.'.'.$this->secondLocalKey, '=', $this->getQualifiedFarKeyName()); |
||
| 85 | |||
| 86 | if ($this->throughParentSoftDeletes()) { |
||
| 87 | $query->whereNull($hash.'.'.$this->throughParent->getDeletedAtColumn()); |
||
| 88 | } |
||
| 89 | |||
| 90 | return $relation($query, $parentQuery, $columns); |
||
| 91 | } |
||
| 92 | |||
| 93 | $this->performJoin($query); |
||
| 94 | |||
| 95 | return $relation($query, $parentQuery, $columns); |
||
| 96 | }; |
||
| 97 | // extended builder |
||
| 98 | $hasOne = function (Builder $query, Builder $parentQuery) use ($hasOneOrMany): Builder { |
||
| 99 | if ($this->isOneOfMany()) { |
||
| 100 | $this->mergeOneOfManyJoinsTo($query); |
||
| 101 | } |
||
| 102 | |||
| 103 | return $hasOneOrMany($query, $parentQuery); |
||
| 104 | }; |
||
| 105 | $morphOneOrMany = function (Builder $query, Builder $parentQuery) use ($hasOneOrMany): Builder { |
||
| 106 | return $hasOneOrMany($query, $parentQuery)->where( |
||
| 107 | $query->qualifyColumn($this->getMorphType()), |
||
| 108 | $this->morphClass |
||
| 109 | ); |
||
| 110 | }; |
||
| 111 | $morphOne = function (Builder $query, Builder $parentQuery) use ($morphOneOrMany): Builder { |
||
| 112 | if ($this->isOneOfMany()) { |
||
| 113 | $this->mergeOneOfManyJoinsTo($query); |
||
| 114 | } |
||
| 115 | |||
| 116 | return $morphOneOrMany($query, $parentQuery); |
||
| 117 | }; |
||
| 118 | $morphToMany = function (Builder $query, Builder $parentQuery) use ($belongsToMany): Builder { |
||
| 119 | return $belongsToMany($query, $parentQuery)->where( |
||
| 120 | $this->qualifyPivotColumn($this->morphType), |
||
| 121 | $this->morphClass |
||
| 122 | ); |
||
| 123 | }; |
||
| 124 | |||
| 125 | return match (true) { |
||
| 126 | // extended relation |
||
| 127 | $this instanceof HasOne => $hasOne($query, $parentQuery), |
||
| 128 | $this instanceof MorphOne => $morphOne($query, $parentQuery), |
||
| 129 | $this instanceof MorphOneOrMany => $morphOneOrMany($query, $parentQuery), |
||
| 130 | $this instanceof MorphToMany => $morphToMany($query, $parentQuery), |
||
| 131 | // basic relation |
||
| 132 | $this instanceof BelongsTo => $belongsTo($query, $parentQuery), |
||
| 133 | $this instanceof BelongsToMany => $belongsToMany($query, $parentQuery), |
||
| 134 | $this instanceof HasOneOrMany => $hasOneOrMany($query, $parentQuery), |
||
| 135 | $this instanceof HasManyThrough => $hasManyThrough($query, $parentQuery), |
||
| 136 | default => throw new LogicException( |
||
| 137 | sprintf('%s must be a relationship instance.', $this::class) |
||
| 138 | ) |
||
| 155 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.