| Conditions | 23 |
| Paths | 22 |
| Total Lines | 84 |
| Code Lines | 65 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | 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 |
||
| 229 | public static function buildFilter($filter) |
||
| 230 | { |
||
| 231 | |||
| 232 | $operators = []; |
||
| 233 | $operators['range'] = ['gt', 'gte', 'lt', 'lte']; |
||
| 234 | $operators['standart'] = ['prefix', 'regexp', 'wildchard']; |
||
| 235 | $operators['BooleanQueryLevel'] = ['not']; |
||
| 236 | $operators['special'] = ['in']; |
||
| 237 | |||
| 238 | $filters = []; |
||
| 239 | foreach ($filter as $key => $value) { |
||
| 240 | $is_not = ''; |
||
| 241 | if (strpos($key, '__')!==false) { |
||
| 242 | preg_match('/__(.*?)$/i', $key, $matches); |
||
| 243 | $operator = $matches[1]; |
||
| 244 | if (strpos($operator, '!')===0) { |
||
| 245 | $operator = str_replace('!', '', $operator); |
||
| 246 | $is_not = '_not'; |
||
| 247 | } |
||
| 248 | $key = str_replace($matches[0], '', $key); |
||
| 249 | foreach ($operators as $type => $possibilities) { |
||
| 250 | if (in_array($operator, $possibilities)) { |
||
| 251 | switch ($type) { |
||
| 252 | case 'range': |
||
| 253 | $filters['must'.$is_not][] = ['range' => [$key => [$operator => $value]]]; |
||
| 254 | break; |
||
| 255 | case 'standart': |
||
| 256 | $filters['must'.$is_not][] = [$type => [$key => $value]]; |
||
| 257 | break; |
||
| 258 | case 'BooleanQueryLevel': |
||
| 259 | switch ($operator) { |
||
| 260 | case 'not': |
||
| 261 | $filters['must_not'][] = ['term' => [$key => $value]]; |
||
| 262 | break; |
||
| 263 | } |
||
| 264 | break; |
||
| 265 | case 'special': |
||
| 266 | switch ($operator) { |
||
| 267 | case 'in': |
||
| 268 | $filters['must'.$is_not][] = ['terms' => [$key => $value]]; |
||
| 269 | break; |
||
| 270 | } |
||
| 271 | break; |
||
| 272 | } |
||
| 273 | } |
||
| 274 | } |
||
| 275 | } elseif (strpos($key, '__')===false && is_array($value)) { |
||
| 276 | foreach ($value as $skey => $svalue) { |
||
| 277 | if (strpos($skey, '__')!==false) { |
||
| 278 | preg_match('/__(.*?)$/i', $skey, $smatches); |
||
| 279 | $soperator = $smatches[1]; |
||
| 280 | if (strpos($soperator, '!')===0) { |
||
| 281 | $soperator = str_replace('!', '', $soperator); |
||
| 282 | } |
||
| 283 | $skey = str_replace($smatches[0], '', $skey); |
||
| 284 | foreach ($operators as $type => $possibilities) { |
||
| 285 | if (in_array($soperator, $possibilities)) { |
||
| 286 | switch ($type) { |
||
| 287 | case 'range': |
||
| 288 | $filters['should'][] = ['range' => [$skey => [$soperator => $svalue]]]; |
||
| 289 | break; |
||
| 290 | case 'standart': |
||
| 291 | $filters['should'][] = [$type => [$skey => $svalue]]; |
||
| 292 | break; |
||
| 293 | case 'special': |
||
| 294 | switch ($soperator) { |
||
| 295 | case 'in': |
||
| 296 | $filters['should'][] = ['terms' => [$skey => $svalue]]; |
||
| 297 | break; |
||
| 298 | } |
||
| 299 | break; |
||
| 300 | } |
||
| 301 | } |
||
| 302 | } |
||
| 303 | } else { |
||
| 304 | $filters['should'][] = ['term' => [$skey => $svalue]]; |
||
| 305 | } |
||
| 306 | } |
||
| 307 | } else { |
||
| 308 | $filters['must'][] = ['term' => [$key => $value]]; |
||
| 309 | } |
||
| 310 | } |
||
| 311 | return $filters; |
||
| 312 | } |
||
| 313 | } |
||
| 314 |