| Conditions | 24 |
| Paths | 154 |
| Total Lines | 129 |
| Code Lines | 86 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 1 | 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 |
||
| 139 | public function selectSearch(Request $request, ModelConfigurationInterface $model, $field, $id = null) |
||
| 140 | { |
||
| 141 | $form = $this->getModelLogic($model, $id); |
||
| 142 | |||
| 143 | if ($form instanceof JsonResponse) { |
||
| 144 | return $form; |
||
| 145 | } |
||
| 146 | |||
| 147 | // because field name in MultiSelectAjax ends with '[]' |
||
| 148 | $fieldPrepared = str_replace('[]', '', $field); |
||
| 149 | // process fields with relations: user[role] |
||
| 150 | $fieldPrepared = strtr($fieldPrepared, ['[' => '.', ']' => '']); |
||
| 151 | |||
| 152 | /** @var SelectAjax|MultiSelectAjax $element */ |
||
| 153 | $element = $form->getElement($fieldPrepared); |
||
| 154 | |||
| 155 | if (is_null($element)) { |
||
| 156 | return new JsonResponse([ |
||
| 157 | 'message' => 'Element not found', |
||
| 158 | ], 404); |
||
| 159 | } |
||
| 160 | |||
| 161 | $params = $request->input('depdrop_all_params', []); |
||
| 162 | $temp = []; |
||
| 163 | foreach ($params as $key => $val) { |
||
| 164 | $key = strtr($key, ['[' => '.', ']' => '']); |
||
| 165 | $key = strtr($key, ['__' => '.']); |
||
| 166 | $temp[$key] = $val; |
||
| 167 | } |
||
| 168 | $params = $temp; |
||
| 169 | |||
| 170 | $element->setAjaxParameters($params); |
||
| 171 | |||
| 172 | if (is_callable($closure = $element->getModelForOptionsCallback())) { |
||
| 173 | $model_classname = $closure($element); |
||
| 174 | } else { |
||
| 175 | $model_classname = $element->getModelForOptions(); |
||
| 176 | } |
||
| 177 | if (is_object($model_classname)) { |
||
| 178 | $model_classname = get_class($model_classname); |
||
| 179 | } |
||
| 180 | if ($model_classname && class_exists($model_classname)) { |
||
| 181 | $model = new $model_classname; |
||
| 182 | |||
| 183 | $search = $element->getSearch(); |
||
| 184 | if (is_callable($search)) { |
||
| 185 | $search = $search($element); |
||
| 186 | } |
||
| 187 | $display = $element->getDisplay(); |
||
| 188 | $custom_name = $element->getCustomName(); |
||
| 189 | $exclude = $element->getExclude(); |
||
| 190 | |||
| 191 | if ($request->q && is_object($model)) { |
||
| 192 | $query = $model; |
||
| 193 | |||
| 194 | // search logic |
||
| 195 | $model_table = $model->getTable(); |
||
| 196 | $q = $request->q; |
||
| 197 | if (is_array($search)) { |
||
| 198 | $query = $query->where(function ($query) use ($model_table, $search, $q) { |
||
| 199 | foreach ($search as $key => $val) { |
||
| 200 | if (is_numeric($key)) { |
||
| 201 | $srch = $val; |
||
| 202 | $value = '%'.$q.'%'; |
||
| 203 | } else { |
||
| 204 | $srch = $key; |
||
| 205 | switch ($val) { |
||
| 206 | case 'equal': |
||
| 207 | $value = $q; |
||
| 208 | break; |
||
| 209 | case 'begins_with': |
||
| 210 | $value = $q.'%'; |
||
| 211 | break; |
||
| 212 | case 'ends_with': |
||
| 213 | $value = '%'.$q; |
||
| 214 | break; |
||
| 215 | case 'contains': |
||
| 216 | default: |
||
| 217 | $value = '%'.$q.'%'; |
||
| 218 | break; |
||
| 219 | } |
||
| 220 | } |
||
| 221 | $query = $query->orWhere($model_table.'.'.$srch, 'LIKE', $value); |
||
| 222 | } |
||
| 223 | }); |
||
| 224 | } else { |
||
| 225 | $query = $query->where($model_table.'.'.$search, 'LIKE', "%{$request->q}%"); |
||
| 226 | } |
||
| 227 | |||
| 228 | // exclude result elements by id |
||
| 229 | if (count($exclude)) { |
||
| 230 | $query = $query->whereNotIn($model_table.'.'.$model->getKeyName(), $exclude); |
||
| 231 | } |
||
| 232 | |||
| 233 | // call the pre load options query preparer if has be set |
||
| 234 | if (is_callable($preparer = $element->getLoadOptionsQueryPreparer())) { |
||
| 235 | $query = $preparer($element, $query); |
||
| 236 | } |
||
| 237 | |||
| 238 | return new JsonResponse( |
||
| 239 | $query |
||
| 240 | ->get() |
||
| 241 | ->map(function (Model $item) use ($display, $element, $custom_name) { |
||
| 242 | if (is_string($display)) { |
||
| 243 | $value = $item->{$display}; |
||
| 244 | } elseif (is_callable($display)) { |
||
| 245 | $value = $display($item, $element); |
||
| 246 | } else { |
||
| 247 | $value = null; |
||
| 248 | } |
||
| 249 | if (is_string($custom_name)) { |
||
| 250 | $custom_name_value = $item->{$custom_name}; |
||
| 251 | } elseif (is_callable($custom_name)) { |
||
| 252 | $custom_name_value = $custom_name($item, $element); |
||
| 253 | } else { |
||
| 254 | $custom_name_value = null; |
||
| 255 | } |
||
| 256 | |||
| 257 | return [ |
||
| 258 | 'tag_name' => $value, |
||
| 259 | 'id' => $item->id, |
||
| 260 | 'custom_name' => $custom_name_value, |
||
| 261 | ]; |
||
| 262 | }) |
||
| 263 | ); |
||
| 264 | } |
||
| 265 | } |
||
| 266 | |||
| 267 | return new JsonResponse([]); |
||
| 268 | } |
||
| 270 |