| Conditions | 37 |
| Paths | 432 |
| Total Lines | 154 |
| Code Lines | 98 |
| 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 |
||
| 33 | public function apply($model, RepositoryInterface $repository) |
||
| 34 | { |
||
| 35 | $fieldsSearchable = $repository->getFieldsSearchable(); |
||
| 36 | list($querySearch, $querySearchFields) = $this->parserQueryToSearch($fieldsSearchable); |
||
| 37 | $search = $this->request->get(config('repository.criteria.params.search', 'search'), $querySearch); |
||
| 38 | $searchFields = $this->request->get(config('repository.criteria.params.searchFields', 'searchFields'), $querySearchFields); |
||
| 39 | $filter = $this->request->get(config('repository.criteria.params.filter', 'filter'), null); |
||
| 40 | $orderBy = $this->request->get(config('repository.criteria.params.orderBy', 'orderBy'), null); |
||
| 41 | $sortedBy = $this->request->get(config('repository.criteria.params.sortedBy', 'sortedBy'), 'asc'); |
||
| 42 | $with = $this->request->get(config('repository.criteria.params.with', 'with'), null); |
||
| 43 | $sortedBy = !empty($sortedBy) ? $sortedBy : 'asc'; |
||
| 44 | |||
| 45 | if ($search && is_array($fieldsSearchable) && count($fieldsSearchable)) { |
||
| 46 | $searchFields = is_array($searchFields) || is_null($searchFields) ? $searchFields : explode(';', $searchFields); |
||
| 47 | $fields = $this->parserFieldsSearch($fieldsSearchable, $searchFields); |
||
| 48 | $isFirstField = true; |
||
| 49 | $searchData = $this->parserSearchData($search); |
||
| 50 | $search = $this->parserSearchValue($search); |
||
| 51 | $modelForceAndWhere = method_exists($repository, 'getIsSearchableForceAndWhere') ? $repository->getIsSearchableForceAndWhere() : false; |
||
| 52 | |||
| 53 | $model = $model->where(function ($query) use ($fields, $search, $searchData, $isFirstField, $modelForceAndWhere) { |
||
| 54 | /* @var Builder $query */ |
||
| 55 | |||
| 56 | foreach ($fields as $field => $condition) { |
||
| 57 | if (is_numeric($field)) { |
||
| 58 | $field = $condition; |
||
| 59 | $condition = '='; |
||
| 60 | } |
||
| 61 | |||
| 62 | $value = null; |
||
| 63 | |||
| 64 | $condition = trim(strtolower($condition)); |
||
| 65 | |||
| 66 | if (isset($searchData[$field])) { |
||
| 67 | if ($condition == 'like_raw') { |
||
| 68 | $condition = 'like'; |
||
| 69 | $value = $searchData[$field]; |
||
| 70 | } elseif ($condition == 'like_safe') { |
||
| 71 | $condition = 'like'; |
||
| 72 | if (str_contains_ascii_only($searchData[$field])) { |
||
| 73 | $value = "%{$searchData[$field]}%"; |
||
| 74 | } else { |
||
| 75 | $value = null; |
||
| 76 | } |
||
| 77 | } else { |
||
| 78 | $value = ($condition == 'like' || $condition == 'ilike') ? "%{$searchData[$field]}%" : $searchData[$field]; |
||
| 79 | } |
||
| 80 | } else { |
||
| 81 | if (!is_null($search)) { |
||
| 82 | if ($condition == 'like_raw') { |
||
| 83 | $condition = 'like'; |
||
| 84 | $value = $searchData[$field]; |
||
| 85 | } elseif ($condition == 'like_safe') { |
||
| 86 | $condition = 'like'; |
||
| 87 | if (str_contains_ascii_only($search)) { |
||
| 88 | $value = "%{$search}%"; |
||
| 89 | } else { |
||
| 90 | $value = null; |
||
| 91 | } |
||
| 92 | } else { |
||
| 93 | $value = ($condition == 'like' || $condition == 'ilike') ? "%{$search}%" : $search; |
||
| 94 | } |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | $relation = null; |
||
| 99 | if (stripos($field, '.')) { |
||
| 100 | $explode = explode('.', $field); |
||
| 101 | $field = array_pop($explode); |
||
| 102 | $relation = implode('.', $explode); |
||
| 103 | } |
||
| 104 | $modelTableName = $query->getModel()->getTable(); |
||
| 105 | if ($isFirstField || $modelForceAndWhere) { |
||
| 106 | if (!is_null($value)) { |
||
| 107 | if (!is_null($relation)) { |
||
| 108 | $query->whereHas($relation, function ($query) use ($field, $condition, $value) { |
||
| 109 | $query->where($field, $condition, $value); |
||
| 110 | }); |
||
| 111 | } else { |
||
| 112 | $query->where($modelTableName.'.'.$field, $condition, $value); |
||
| 113 | } |
||
| 114 | $isFirstField = false; |
||
| 115 | } |
||
| 116 | } else { |
||
| 117 | if (!is_null($value)) { |
||
| 118 | if (!is_null($relation)) { |
||
| 119 | $query->orWhereHas($relation, function ($query) use ($field, $condition, $value) { |
||
| 120 | $query->where($field, $condition, $value); |
||
| 121 | }); |
||
| 122 | } else { |
||
| 123 | $query->orWhere($modelTableName.'.'.$field, $condition, $value); |
||
| 124 | } |
||
| 125 | } |
||
| 126 | } |
||
| 127 | } |
||
| 128 | }); |
||
| 129 | } |
||
| 130 | |||
| 131 | if (isset($orderBy) && !empty($orderBy)) { |
||
| 132 | $split = explode('|', $orderBy); |
||
| 133 | if (count($split) > 1) { |
||
| 134 | /* |
||
| 135 | * ex. |
||
| 136 | * products|description -> join products on current_table.product_id = products.id order by description |
||
| 137 | * |
||
| 138 | * products:custom_id|products.description -> join products on current_table.custom_id = products.id order |
||
| 139 | * by products.description (in case both tables have same column name) |
||
| 140 | */ |
||
| 141 | $table = $model->getModel()->getTable(); |
||
| 142 | $sortTable = $split[0]; |
||
| 143 | $sortColumn = $split[1]; |
||
| 144 | |||
| 145 | $split = explode(':', $sortTable); |
||
| 146 | if (count($split) > 1) { |
||
| 147 | $sortTable = $split[0]; |
||
| 148 | $keyName = $table.'.'.$split[1]; |
||
| 149 | } else { |
||
| 150 | /* |
||
| 151 | * If you do not define which column to use as a joining column on current table, it will |
||
| 152 | * use a singular of a join table appended with _id |
||
| 153 | * |
||
| 154 | * ex. |
||
| 155 | * products -> product_id |
||
| 156 | */ |
||
| 157 | $prefix = rtrim($sortTable, 's'); |
||
| 158 | $keyName = $table.'.'.$prefix.'_id'; |
||
| 159 | } |
||
| 160 | |||
| 161 | $model = $model |
||
| 162 | ->leftJoin($sortTable, $keyName, '=', $sortTable.'.id') |
||
| 163 | ->orderBy($sortColumn, $sortedBy) |
||
| 164 | ->addSelect($table.'.*'); |
||
| 165 | } else { |
||
| 166 | $model = $model->orderBy($orderBy, $sortedBy); |
||
| 167 | } |
||
| 168 | } |
||
| 169 | |||
| 170 | if (isset($filter) && !empty($filter)) { |
||
| 171 | if (is_string($filter)) { |
||
| 172 | $filter = explode(';', $filter); |
||
| 173 | } |
||
| 174 | |||
| 175 | $model = $model->select($filter); |
||
| 176 | } |
||
| 177 | |||
| 178 | if ($with) { |
||
| 179 | $with = explode(';', $with); |
||
| 180 | $model = $model->with($with); |
||
| 181 | } |
||
| 182 | |||
| 183 | $this->request->offsetUnset(config('repository.criteria.params.search', 'search')); |
||
| 184 | $this->request->offsetUnset(config('repository.criteria.params.searchFields', 'searchFields')); |
||
| 185 | |||
| 186 | return $model; |
||
| 187 | } |
||
| 263 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths