| Conditions | 37 |
| Paths | 434 |
| Total Lines | 166 |
| Code Lines | 131 |
| 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 |
||
| 198 | protected function parserSearch() |
||
| 199 | { |
||
| 200 | if ($this->search && is_array($this->fieldsSearchable) && count($this->fieldsSearchable)) { |
||
| 201 | |||
| 202 | $this->parserFieldsSearch(); |
||
| 203 | $this->parserSearchData(); |
||
| 204 | $this->parserSearchValue(); |
||
| 205 | |||
| 206 | $this->modelForceAndWhere = strtolower($this->searchJoin) === 'and'; |
||
| 207 | |||
| 208 | foreach ($this->fields as $field => $condition) { |
||
| 209 | |||
| 210 | if (is_numeric($field)) { |
||
| 211 | $field = $condition; |
||
| 212 | $condition = "="; |
||
| 213 | } |
||
| 214 | |||
| 215 | $value = null; |
||
| 216 | |||
| 217 | $condition = trim(strtolower($condition)); |
||
| 218 | |||
| 219 | if (isset($this->searchData[$field])) { |
||
| 220 | $value = $this->searchData[$field]; |
||
| 221 | if ($condition == "like" || $condition == "ilike") { |
||
| 222 | $value = "%{$value}%"; |
||
| 223 | } |
||
| 224 | if ($condition == "in" || $condition == "between" || $condition == "cross") { |
||
| 225 | $value = explode(',', $value); |
||
| 226 | } |
||
| 227 | } else { |
||
| 228 | if (!is_null($this->search)) { |
||
| 229 | $value = $this->search; |
||
| 230 | if ($condition == "like" || $condition == "ilike") { |
||
| 231 | $value = "%{$value}%"; |
||
| 232 | } |
||
| 233 | if ($condition == "in" || $condition == "between" || $condition == "cross") { |
||
| 234 | $value = explode(',', $value); |
||
| 235 | } |
||
| 236 | } |
||
| 237 | } |
||
| 238 | |||
| 239 | $relation = null; |
||
| 240 | if (stripos($field, '.')) { |
||
| 241 | $explode = explode('.', $field); |
||
| 242 | $field = array_pop($explode); |
||
| 243 | $relation = implode('.', $explode); |
||
| 244 | } |
||
| 245 | $modelTableName = $this->model->getModel()->getTable(); |
||
| 246 | if ($this->isFirstField || $this->modelForceAndWhere) { |
||
| 247 | if (!is_null($value)) { |
||
| 248 | if (!is_null($relation)) { |
||
| 249 | $this->model = $this->model->whereHas($relation, function (Builder $query) use ( |
||
| 250 | $field, |
||
| 251 | $condition, |
||
| 252 | $value |
||
| 253 | ) { |
||
| 254 | if ($condition == 'in') { |
||
| 255 | $query->whereIn($field, $value); |
||
| 256 | } elseif ($condition == 'between') { |
||
| 257 | $query->whereBetween($field, $value); |
||
| 258 | } elseif ($condition == 'cross') { |
||
| 259 | $query->where(function (Builder $query) use ($field, $value) { |
||
| 260 | $query->where(function (Builder $query) use ($field, $value) { |
||
| 261 | $query->where("{$field}_min", '<=', $value[0]) |
||
| 262 | ->where("{$field}_max", '>=', $value[1]); |
||
| 263 | })->orWhere(function (Builder $query) use ($field, $value) { |
||
| 264 | $query->where("{$field}_min", '<=', $value[0]) |
||
| 265 | ->where("{$field}_max", '>=', $value[0]); |
||
| 266 | })->orWhere(function (Builder $query) use ($field, $value) { |
||
| 267 | $query->where("{$field}_min", '>=', $value[0]) |
||
| 268 | ->where("{$field}_max", '<=', $value[1]); |
||
| 269 | })->orWhere(function (Builder $query) use ($field, $value) { |
||
| 270 | $query->where("{$field}_min", '>=', $value[0]) |
||
| 271 | ->where("{$field}_max", '>=', $value[1]) |
||
| 272 | ->where("{$field}_min", '<=', $value[1]); |
||
| 273 | }); |
||
| 274 | }); |
||
| 275 | } else { |
||
| 276 | $query->where($field, $condition, $value); |
||
| 277 | } |
||
| 278 | }); |
||
| 279 | } else { |
||
| 280 | if ($condition == 'in') { |
||
| 281 | $this->model = $this->model->whereIn($field, $value); |
||
| 282 | } elseif ($condition == 'between') { |
||
| 283 | $this->model = $this->model->whereBetween($field, $value); |
||
| 284 | } elseif ($condition == 'cross') { |
||
| 285 | $this->model = $this->model->where(function (Builder $query) use ($field, $value) { |
||
| 286 | $query->where(function (Builder $query) use ($field, $value) { |
||
| 287 | $query->where("{$field}_min", '<=', $value[0]) |
||
| 288 | ->where("{$field}_max", '>=', $value[1]); |
||
| 289 | })->orWhere(function (Builder $query) use ($field, $value) { |
||
| 290 | $query->where("{$field}_min", '<=', $value[0]) |
||
| 291 | ->where("{$field}_max", '>=', $value[0]); |
||
| 292 | })->orWhere(function (Builder $query) use ($field, $value) { |
||
| 293 | $query->where("{$field}_min", '>=', $value[0]) |
||
| 294 | ->where("{$field}_max", '<=', $value[1]); |
||
| 295 | })->orWhere(function (Builder $query) use ($field, $value) { |
||
| 296 | $query->where("{$field}_min", '>=', $value[0]) |
||
| 297 | ->where("{$field}_max", '>=', $value[1]) |
||
| 298 | ->where("{$field}_min", '<=', $value[1]); |
||
| 299 | }); |
||
| 300 | }); |
||
| 301 | } else { |
||
| 302 | $this->model = $this->model->where($field, $condition, $value); |
||
| 303 | } |
||
| 304 | } |
||
| 305 | $this->isFirstField = false; |
||
| 306 | } |
||
| 307 | } else { |
||
| 308 | if (!is_null($value)) { |
||
| 309 | if (!is_null($relation)) { |
||
| 310 | $this->model = $this->model->orWhereHas($relation, function (Builder $query) use ( |
||
| 311 | $field, |
||
| 312 | $condition, |
||
| 313 | $value |
||
| 314 | ) { |
||
| 315 | if ($condition == 'in') { |
||
| 316 | $query->whereIn($field, $value); |
||
| 317 | } elseif ($condition == 'between') { |
||
| 318 | $query->whereBetween($field, $value); |
||
| 319 | } elseif ($condition == 'cross') { |
||
| 320 | $query->where(function (Builder $query) use ($field, $value) { |
||
| 321 | $query->where(function (Builder $query) use ($field, $value) { |
||
| 322 | $query->where("{$field}_min", '<=', $value[0]) |
||
| 323 | ->where("{$field}_max", '>=', $value[1]); |
||
| 324 | })->orWhere(function (Builder $query) use ($field, $value) { |
||
| 325 | $query->where("{$field}_min", '<=', $value[0]) |
||
| 326 | ->where("{$field}_max", '>=', $value[0]); |
||
| 327 | })->orWhere(function (Builder $query) use ($field, $value) { |
||
| 328 | $query->where("{$field}_min", '>=', $value[0]) |
||
| 329 | ->where("{$field}_max", '<=', $value[1]); |
||
| 330 | })->orWhere(function (Builder $query) use ($field, $value) { |
||
| 331 | $query->where("{$field}_min", '>=', $value[0]) |
||
| 332 | ->where("{$field}_max", '>=', $value[1]) |
||
| 333 | ->where("{$field}_min", '<=', $value[1]); |
||
| 334 | }); |
||
| 335 | }); |
||
| 336 | } else { |
||
| 337 | $query->where($field, $condition, $value); |
||
| 338 | } |
||
| 339 | }); |
||
| 340 | } else { |
||
| 341 | if ($condition == 'in') { |
||
| 342 | $this->model = $this->model->orWhereIn($modelTableName.'.'.$field, $value); |
||
| 343 | } elseif ($condition == 'between') { |
||
| 344 | $this->model = $this->model->orWhereBetween($modelTableName.'.'.$field, $value); |
||
| 345 | } elseif ($condition == 'cross') { |
||
| 346 | $this->model = $this->model->orWhere(function (Builder $query) use ($field, $value) { |
||
| 347 | $query->where(function (Builder $query) use ($field, $value) { |
||
| 348 | $query->where("{$field}_min", '<=', $value[0]) |
||
| 349 | ->where("{$field}_max", '>=', $value[1]); |
||
| 350 | })->orWhere(function (Builder $query) use ($field, $value) { |
||
| 351 | $query->where("{$field}_min", '<=', $value[0]) |
||
| 352 | ->where("{$field}_max", '>=', $value[0]); |
||
| 353 | })->orWhere(function (Builder $query) use ($field, $value) { |
||
| 354 | $query->where("{$field}_min", '>=', $value[0]) |
||
| 355 | ->where("{$field}_max", '<=', $value[1]); |
||
| 356 | })->orWhere(function (Builder $query) use ($field, $value) { |
||
| 357 | $query->where("{$field}_min", '>=', $value[0]) |
||
| 358 | ->where("{$field}_max", '>=', $value[1]) |
||
| 359 | ->where("{$field}_min", '<=', $value[1]); |
||
| 360 | }); |
||
| 361 | }); |
||
| 362 | } else { |
||
| 363 | $this->model = $this->model->orWhere($modelTableName.'.'.$field, $condition, $value); |
||
| 364 | } |
||
| 399 |