| Conditions | 12 |
| Paths | 26 |
| Total Lines | 64 |
| 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 |
||
| 35 | public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data) |
||
| 36 | { |
||
| 37 | if (!$data || !\is_array($data) || !\array_key_exists('value', $data) || null === $data['value']) { |
||
|
|
|||
| 38 | return; |
||
| 39 | } |
||
| 40 | |||
| 41 | $data['value'] = trim($data['value']); |
||
| 42 | |||
| 43 | if (0 === \strlen($data['value'])) { |
||
| 44 | return; |
||
| 45 | } |
||
| 46 | $joinAlias = 'tff'; |
||
| 47 | $filterMode = $this->getOption('filter_mode'); |
||
| 48 | |||
| 49 | // verify if the join is not already done |
||
| 50 | $aliasAlreadyExists = false; |
||
| 51 | foreach ($queryBuilder->getDQLParts()['join'] as $joins) { |
||
| 52 | foreach ($joins as $join) { |
||
| 53 | if ($join->getAlias() === $joinAlias) { |
||
| 54 | $aliasAlreadyExists = true; |
||
| 55 | break 2; |
||
| 56 | } |
||
| 57 | } |
||
| 58 | } |
||
| 59 | |||
| 60 | if (!$aliasAlreadyExists) { |
||
| 61 | $queryBuilder->leftJoin($alias.'.translations', $joinAlias); |
||
| 62 | } |
||
| 63 | |||
| 64 | if (TranslationFilterMode::GEDMO === $filterMode) { |
||
| 65 | // search on translation OR on normal field when using Gedmo |
||
| 66 | $this->applyWhere($queryBuilder, $queryBuilder->expr()->orX( |
||
| 67 | $queryBuilder->expr()->andX( |
||
| 68 | $queryBuilder->expr()->eq($joinAlias.'.field', $queryBuilder->expr()->literal($field)), |
||
| 69 | $queryBuilder->expr()->like( |
||
| 70 | $joinAlias.'.content', |
||
| 71 | $queryBuilder->expr()->literal('%'.$data['value'].'%') |
||
| 72 | ) |
||
| 73 | ), |
||
| 74 | $queryBuilder->expr()->like( |
||
| 75 | sprintf('%s.%s', $alias, $field), |
||
| 76 | $queryBuilder->expr()->literal('%'.$data['value'].'%') |
||
| 77 | ) |
||
| 78 | )); |
||
| 79 | |||
| 80 | $this->active = true; |
||
| 81 | } elseif (TranslationFilterMode::KNPLABS === $filterMode) { |
||
| 82 | // search on translation OR on normal field when using Knp |
||
| 83 | $this->applyWhere($queryBuilder, |
||
| 84 | $queryBuilder->expr()->andX( |
||
| 85 | $queryBuilder->expr()->like( |
||
| 86 | $joinAlias.".$field", |
||
| 87 | $queryBuilder->expr()->literal('%'.$data['value'].'%') |
||
| 88 | ) |
||
| 89 | ) |
||
| 90 | ); |
||
| 91 | |||
| 92 | $this->active = true; |
||
| 93 | } else { |
||
| 94 | throw new \LogicException( |
||
| 95 | sprintf('Invalid filter mode given: "%s"', $filterMode) |
||
| 96 | ); |
||
| 97 | } |
||
| 98 | } |
||
| 99 | |||
| 137 |
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.