| Conditions | 5 |
| Paths | 6 |
| Total Lines | 90 |
| Code Lines | 40 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 112 | $table = ''; |
||
| 113 | $lastQuery = $this->query; |
||
| 114 | foreach (explode('.', $relation) as $eachRelation) { |
||
| 115 | $model = $lastQuery->getRelation($eachRelation); |
||
| 116 | switch (true) { |
||
| 117 | case $model instanceof BelongsToMany: |
||
| 118 | $pivot = $model->getTable(); |
||
| 119 | $pivotPK = $model->getExistenceCompareKey(); |
||
| 120 | $pivotFK = $model->getQualifiedParentKeyName(); |
||
| 121 | $this->performJoin($pivot, $pivotPK, $pivotFK); |
||
| 122 | |||
| 123 | $related = $model->getRelated(); |
||
| 124 | $table = $related->getTable(); |
||
| 125 | $tablePK = $related->getForeignKey(); |
||
| 126 | $foreign = $pivot . '.' . $tablePK; |
||
| 127 | $other = $related->getQualifiedKeyName(); |
||
| 128 | |||
| 129 | $lastQuery->addSelect($table . '.' . $relationColumn); |
||
| 130 | $this->performJoin($table, $foreign, $other); |
||
| 131 | |||
| 132 | break; |
||
| 133 | |||
| 134 | case $model instanceof HasOneOrMany: |
||
| 135 | $table = $model->getRelated()->getTable(); |
||
| 136 | $foreign = $model->getQualifiedForeignKeyName(); |
||
| 137 | $other = $model->getQualifiedParentKeyName(); |
||
| 138 | break; |
||
| 139 | |||
| 140 | case $model instanceof BelongsTo: |
||
| 141 | $table = $model->getRelated()->getTable(); |
||
| 142 | $foreign = $model->getQualifiedForeignKey(); |
||
| 143 | $other = $model->getQualifiedOwnerKeyName(); |
||
| 144 | break; |
||
| 145 | |||
| 146 | default: |
||
| 147 | throw new Exception('Relation ' . get_class($model) . ' is not yet supported.'); |
||
| 148 | } |
||
| 149 | $this->performJoin($table, $foreign, $other); |
||
| 150 | $lastQuery = $model->getQuery(); |
||
| 151 | } |
||
| 152 | |||
| 153 | return $table . '.' . $relationColumn; |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Perform join query. |
||
| 158 | * |
||
| 159 | * @param string $table |
||
| 160 | * @param string $foreign |
||
| 161 | * @param string $other |
||
| 162 | * @param string $type |
||
| 163 | */ |
||
| 164 | protected function performJoin($table, $foreign, $other, $type = 'left') |
||
| 165 | { |
||
| 166 | $joins = []; |
||
| 167 | foreach ((array) $this->getBaseQueryBuilder()->joins as $key => $join) { |
||
| 168 | $joins[] = $join->table; |
||
| 169 | } |
||
| 170 | |||
| 171 | if (!in_array($table, $joins)) { |
||
| 172 | $this->getBaseQueryBuilder()->join($table, $foreign, '=', $other, $type); |
||
| 173 | } |
||
| 174 | } |
||
| 175 | } |
||
| 176 |