| Conditions | 14 |
| Paths | 31 |
| Total Lines | 45 |
| 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('value', $data) || null === $data['value']) { |
||
|
|
|||
| 27 | return; |
||
| 28 | } |
||
| 29 | |||
| 30 | $value = trim((string) $data['value']); |
||
| 31 | $data['type'] = empty($data['type']) ? ChoiceType::TYPE_CONTAINS : $data['type']; |
||
| 32 | |||
| 33 | if (0 === \strlen($value)) { |
||
| 34 | return; |
||
| 35 | } |
||
| 36 | |||
| 37 | $where = $this->getWhere($proxyQuery); |
||
| 38 | $isComparisonLowerCase = $this->getOption('compare_case_insensitive'); |
||
| 39 | $value = $isComparisonLowerCase ? strtolower($value) : $value; |
||
| 40 | switch ($data['type']) { |
||
| 41 | case ChoiceType::TYPE_EQUAL: |
||
| 42 | if ($isComparisonLowerCase) { |
||
| 43 | $where->eq()->lowerCase()->field('a.'.$field)->end()->literal($value); |
||
| 44 | } else { |
||
| 45 | $where->eq()->field('a.'.$field)->literal($value); |
||
| 46 | } |
||
| 47 | |||
| 48 | break; |
||
| 49 | case ChoiceType::TYPE_NOT_CONTAINS: |
||
| 50 | $where->fullTextSearch('a.'.$field, '* -'.$value); |
||
| 51 | |||
| 52 | break; |
||
| 53 | case ChoiceType::TYPE_CONTAINS: |
||
| 54 | if ($isComparisonLowerCase) { |
||
| 55 | $where->like()->lowerCase()->field('a.'.$field)->end()->literal('%'.$value.'%'); |
||
| 56 | } else { |
||
| 57 | $where->like()->field('a.'.$field)->literal('%'.$value.'%'); |
||
| 58 | } |
||
| 59 | |||
| 60 | break; |
||
| 61 | case ChoiceType::TYPE_CONTAINS_WORDS: |
||
| 62 | default: |
||
| 63 | $where->fullTextSearch('a.'.$field, $value); |
||
| 64 | } |
||
| 65 | |||
| 66 | // filter is active as we have now modified the query |
||
| 67 | $this->active = true; |
||
| 68 | } |
||
| 69 | |||
| 93 |
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.