| Conditions | 12 |
| Paths | 19 |
| Total Lines | 39 |
| 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 |
||
| 24 | public function filter(ProxyQueryInterface $proxyQuery, $alias, $field, $data): void |
||
| 25 | { |
||
| 26 | if (!$data || !\is_array($data) || !\array_key_exists('type', $data) || !\array_key_exists('value', $data)) { |
||
|
|
|||
| 27 | return; |
||
| 28 | } |
||
| 29 | |||
| 30 | $values = (array) $data['value']; |
||
| 31 | $type = $data['type']; |
||
| 32 | |||
| 33 | // clean values |
||
| 34 | foreach ($values as $key => $value) { |
||
| 35 | $value = trim((string) $value); |
||
| 36 | if (!$value) { |
||
| 37 | unset($values[$key]); |
||
| 38 | } else { |
||
| 39 | $values[$key] = $value; |
||
| 40 | } |
||
| 41 | } |
||
| 42 | |||
| 43 | // if values not set, do not do this filter |
||
| 44 | if (!$values) { |
||
| 45 | return; |
||
| 46 | } |
||
| 47 | |||
| 48 | $andX = $this->getWhere($proxyQuery)->andX(); |
||
| 49 | |||
| 50 | foreach ($values as $value) { |
||
| 51 | if (ChoiceType::TYPE_NOT_CONTAINS === $type) { |
||
| 52 | $andX->not()->like()->field('a.'.$field)->literal('%'.$value.'%'); |
||
| 53 | } elseif (ChoiceType::TYPE_CONTAINS === $type) { |
||
| 54 | $andX->like()->field('a.'.$field)->literal('%'.$value.'%'); |
||
| 55 | } elseif (ChoiceType::TYPE_EQUAL === $type) { |
||
| 56 | $andX->like()->field('a.'.$field)->literal($value); |
||
| 57 | } |
||
| 58 | } |
||
| 59 | |||
| 60 | // filter is active as we have now modified the query |
||
| 61 | $this->active = true; |
||
| 62 | } |
||
| 63 | |||
| 85 |
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.