| Conditions | 14 |
| Paths | 22 |
| Total Lines | 65 |
| 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 |
||
| 30 | public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data): void |
||
| 31 | { |
||
| 32 | if (!$data || !\is_array($data) || !\array_key_exists('value', $data) || null === $data['value']) { |
||
|
|
|||
| 33 | return; |
||
| 34 | } |
||
| 35 | |||
| 36 | $data['value'] = trim($data['value']); |
||
| 37 | |||
| 38 | if (0 === \strlen($data['value'])) { |
||
| 39 | return; |
||
| 40 | } |
||
| 41 | |||
| 42 | $type = $data['type'] ?? StringOperatorType::TYPE_CONTAINS; |
||
| 43 | $operator = $this->getOperator((int) $type); |
||
| 44 | |||
| 45 | // c.name > '1' => c.name OPERATOR :FIELDNAME |
||
| 46 | $parameterName = $this->getNewParameterName($queryBuilder); |
||
| 47 | |||
| 48 | $or = $queryBuilder->expr()->orX(); |
||
| 49 | |||
| 50 | if ($this->getOption('case_sensitive')) { |
||
| 51 | $or->add(sprintf('%s.%s %s :%s', $alias, $field, $operator, $parameterName)); |
||
| 52 | } else { |
||
| 53 | $or->add(sprintf('LOWER(%s.%s) %s :%s', $alias, $field, $operator, $parameterName)); |
||
| 54 | } |
||
| 55 | |||
| 56 | if (StringOperatorType::TYPE_NOT_CONTAINS === $type) { |
||
| 57 | $or->add($queryBuilder->expr()->isNull(sprintf('%s.%s', $alias, $field))); |
||
| 58 | } |
||
| 59 | |||
| 60 | $this->applyWhere($queryBuilder, $or); |
||
| 61 | |||
| 62 | if (StringOperatorType::TYPE_EQUAL === $type) { |
||
| 63 | $queryBuilder->setParameter( |
||
| 64 | $parameterName, |
||
| 65 | $this->getOption('case_sensitive') ? $data['value'] : mb_strtolower($data['value']) |
||
| 66 | ); |
||
| 67 | } else { |
||
| 68 | switch ($type) { |
||
| 69 | case StringOperatorType::TYPE_STARTS_WITH: |
||
| 70 | $format = '%s%%'; |
||
| 71 | break; |
||
| 72 | case StringOperatorType::TYPE_ENDS_WITH: |
||
| 73 | $format = '%%%s'; |
||
| 74 | break; |
||
| 75 | default: |
||
| 76 | $format = $this->getOption('format'); |
||
| 77 | |||
| 78 | if ('%%%s%%' !== $format) { |
||
| 79 | @trigger_error( |
||
| 80 | 'The "format" option is deprecated since sonata-project/doctrine-orm-admin-bundle 3.21 and will be removed in version 4.0.', |
||
| 81 | E_USER_DEPRECATED |
||
| 82 | ); |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | $queryBuilder->setParameter( |
||
| 87 | $parameterName, |
||
| 88 | sprintf( |
||
| 89 | $format, |
||
| 90 | $this->getOption('case_sensitive') ? $data['value'] : mb_strtolower($data['value']) |
||
| 91 | ) |
||
| 92 | ); |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 119 |
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.