| Conditions | 22 |
| Paths | 42 |
| Total Lines | 67 |
| Code Lines | 47 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 77 | private function buildCriteria(array $filter, ClassMetadata $collectionMetadata): Criteria |
||
| 78 | { |
||
| 79 | $orderBy = []; |
||
| 80 | $criteria = Criteria::create(); |
||
| 81 | |||
| 82 | foreach ($filter as $field => $value) { |
||
| 83 | // Pagination is handled elsewhere |
||
| 84 | switch ($field) { |
||
| 85 | case '_first': |
||
| 86 | case '_after': |
||
| 87 | case '_last': |
||
| 88 | case '_before': |
||
| 89 | continue 2; |
||
| 90 | } |
||
| 91 | |||
| 92 | // Handle other fields as $field_$type: $value |
||
| 93 | // Get right-most _text |
||
| 94 | $filter = substr($field, strrpos($field, '_') + 1); |
||
| 95 | |||
| 96 | if (strrpos($field, '_') === false) { |
||
| 97 | // Special case for eq `field: value` |
||
| 98 | $value = $this->parseValue($collectionMetadata, $field, $value); |
||
| 99 | $criteria->andWhere($criteria->expr()->eq($field, $value)); |
||
| 100 | } else { |
||
| 101 | $field = substr($field, 0, strrpos($field, '_')); |
||
| 102 | |||
| 103 | // Format value type - this seems like something which should |
||
| 104 | // be done in GraphQL. |
||
| 105 | switch ($filter) { |
||
| 106 | case 'eq': |
||
| 107 | case 'neq': |
||
| 108 | case 'lt': |
||
| 109 | case 'lte': |
||
| 110 | case 'gt': |
||
| 111 | case 'gte': |
||
| 112 | case 'contains': |
||
| 113 | case 'startswith': |
||
| 114 | case 'endswith': |
||
| 115 | $value = $this->parseValue($collectionMetadata, $field, $value); |
||
| 116 | $criteria->andWhere($criteria->expr()->$filter($field, $value)); |
||
| 117 | break; |
||
| 118 | case 'in': |
||
| 119 | case 'notin': |
||
| 120 | $value = $this->parseArrayValue($collectionMetadata, $field, $value); |
||
| 121 | $criteria->andWhere($criteria->expr()->$filter($field, $value)); |
||
| 122 | break; |
||
| 123 | case 'isnull': |
||
| 124 | $criteria->andWhere($criteria->expr()->$filter($field)); |
||
| 125 | break; |
||
| 126 | case 'between': |
||
| 127 | $value = $this->parseArrayValue($collectionMetadata, $field, $value); |
||
| 128 | |||
| 129 | $criteria->andWhere($criteria->expr()->gte($field, $value['from'])); |
||
| 130 | $criteria->andWhere($criteria->expr()->lte($field, $value['to'])); |
||
| 131 | break; |
||
| 132 | case 'sort': |
||
| 133 | $orderBy[$field] = $value; |
||
| 134 | break; |
||
| 135 | } |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | if (! empty($orderBy)) { |
||
| 140 | $criteria->orderBy($orderBy); |
||
| 141 | } |
||
| 142 | |||
| 143 | return $criteria; |
||
| 144 | } |
||
| 249 |