| Conditions | 13 |
| Paths | 19 |
| Total Lines | 39 |
| Code Lines | 22 |
| 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 |
||
| 22 | public function filter(ProxyQueryInterface $proxyQuery, $alias, $field, $data) |
||
| 23 | { |
||
| 24 | if (!$data || !is_array($data) || !array_key_exists('type', $data) || !array_key_exists('value', $data)) { |
||
| 25 | return; |
||
| 26 | } |
||
| 27 | |||
| 28 | $values = (array) $data['value']; |
||
| 29 | $type = $data['type']; |
||
| 30 | |||
| 31 | // clean values |
||
| 32 | foreach ($values as $key => $value) { |
||
| 33 | $value = trim($value); |
||
| 34 | if (!$value) { |
||
| 35 | unset($values[$key]); |
||
| 36 | } else { |
||
| 37 | $values[$key] = $value; |
||
| 38 | } |
||
| 39 | } |
||
| 40 | |||
| 41 | // if values not set or "all" specified, do not do this filter |
||
| 42 | if (!$values || in_array('all', $values, true)) { |
||
|
|
|||
| 43 | return; |
||
| 44 | } |
||
| 45 | |||
| 46 | $andX = $this->getWhere($proxyQuery)->andX(); |
||
| 47 | |||
| 48 | foreach ($values as $value) { |
||
| 49 | if ($type == ChoiceType::TYPE_NOT_CONTAINS) { |
||
| 50 | $andX->not()->like()->field('a.'.$field)->literal('%'.$value.'%'); |
||
| 51 | } elseif ($type == ChoiceType::TYPE_CONTAINS) { |
||
| 52 | $andX->like()->field('a.'.$field)->literal('%'.$value.'%'); |
||
| 53 | } elseif ($type == ChoiceType::TYPE_EQUAL) { |
||
| 54 | $andX->like()->field('a.'.$field)->literal($value); |
||
| 55 | } |
||
| 56 | } |
||
| 57 | |||
| 58 | // filter is active as we have now modified the query |
||
| 59 | $this->active = true; |
||
| 60 | } |
||
| 61 | |||
| 83 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.