| 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 | private function compileRelationSearch($query, $relation, $column, $keyword) |
||
| 113 | { |
||
| 114 | $myQuery = clone $this->query; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * For compile nested relation, we need store all nested relation as array |
||
| 118 | * and reverse order to apply where query. |
||
| 119 | * With this method we can create nested sub query with properly relation. |
||
| 120 | */ |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Store all relation data that require in next step |
||
| 124 | */ |
||
| 125 | $relationChunk = []; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Store last eloquent query builder for get next relation. |
||
| 129 | */ |
||
| 130 | $lastQuery = $query; |
||
| 131 | |||
| 132 | $relations = explode('.', $relation); |
||
| 133 | $lastRelation = end($relations); |
||
| 134 | foreach ($relations as $relation) { |
||
| 135 | $relationType = $myQuery->getModel()->{$relation}(); |
||
| 136 | $myQuery->orWhereHas($relation, function ($builder) use ( |
||
| 137 | $column, |
||
| 138 | $keyword, |
||
| 139 | $query, |
||
| 140 | $relationType, |
||
| 141 | $relation, |
||
| 142 | $lastRelation, |
||
| 143 | &$relationChunk, |
||
| 144 | &$lastQuery |
||
| 145 | ) { |
||
| 146 | $builder->select($this->connection->raw('count(1)')); |
||
| 147 | |||
| 148 | // We will perform search on last relation only. |
||
| 149 | if ($relation == $lastRelation) { |
||
| 150 | $this->compileQuerySearch($builder, $column, $keyword, ''); |
||
| 151 | } |
||
| 152 | |||
| 153 | // Put require object to next step!! |
||
| 154 | $relationChunk[$relation] = [ |
||
| 155 | 'builder' => $builder, |
||
| 156 | 'relationType' => $relationType, |
||
| 157 | 'query' => $lastQuery, |
||
| 158 | ]; |
||
| 159 | |||
| 160 | // This is trick make sub query. |
||
| 161 | $lastQuery = $builder; |
||
| 162 | }); |
||
| 163 | |||
| 164 | // This is trick to make nested relation by pass previous relation to be next query eloquent builder |
||
| 165 | $myQuery = $relationType; |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Reverse them all |
||
| 170 | */ |
||
| 171 | $relationChunk = array_reverse($relationChunk, true); |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Create valuable for use in check last relation |
||
| 175 | */ |
||
| 176 | end($relationChunk); |
||
| 177 | $lastRelation = key($relationChunk); |
||
| 178 | reset($relationChunk); |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Walking ... |
||
| 182 | */ |
||
| 183 | foreach ($relationChunk as $relation => $chunk) { |
||
| 184 | /** @var Builder $builder */ |
||
| 185 | $builder = $chunk['builder']; |
||
| 186 | $query = $chunk['query']; |
||
| 187 | $bindings = $builder->getBindings(); |
||
| 188 | $builder = "({$builder->toSql()}) >= 1"; |
||
| 189 | |||
| 190 | // Check if it last relation we will use orWhereRaw |
||
| 191 | if ($lastRelation == $relation) { |
||
| 192 | $relationMethod = "orWhereRaw"; |
||
| 193 | } else { |
||
| 194 | // For case parent relation of nested relation. |
||
| 195 | // We must use and for properly query and get correct result |
||
| 196 | $relationMethod = "whereRaw"; |
||
| 197 | } |
||
| 198 | |||
| 199 | $query->{$relationMethod}($builder, $bindings); |
||
| 200 | } |
||
| 201 | } |
||
| 202 | |||
| 299 |