| Conditions | 10 |
| Paths | 8 |
| Total Lines | 55 |
| Code Lines | 33 |
| 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 |
||
| 38 | public function transformCriteria(array $criteria) |
||
| 39 | { |
||
| 40 | $apiCriteria = []; |
||
| 41 | foreach ($criteria as $field => $values) { |
||
| 42 | if ($this->metadata->hasAssociation($field)) { |
||
| 43 | $mapping = $this->metadata->getAssociationMapping($field); |
||
| 44 | /** @var EntityMetadata $target */ |
||
| 45 | $target = $this->manager->getClassMetadata($mapping['target']); |
||
| 46 | |||
| 47 | $converter = function ($value) use ($target) { |
||
| 48 | if (!is_object($value)) { |
||
| 49 | return $value; |
||
| 50 | } |
||
| 51 | |||
| 52 | $ids = $target->getIdentifierValues($value); |
||
| 53 | if ($target->isIdentifierComposite) { |
||
| 54 | return $ids; |
||
| 55 | } |
||
| 56 | |||
| 57 | return array_shift($ids); |
||
| 58 | }; |
||
| 59 | |||
| 60 | if ($values instanceof Collection) { |
||
| 61 | if ($values instanceof ApiCollection && !$values->isInitialized()) { |
||
| 62 | continue; |
||
| 63 | } |
||
| 64 | $values = $values->toArray(); |
||
| 65 | } |
||
| 66 | |||
| 67 | if (is_array($values)) { |
||
| 68 | $values = array_map($converter, $values); |
||
| 69 | } else { |
||
| 70 | $values = $converter($values); |
||
| 71 | } |
||
| 72 | } else { |
||
| 73 | $caster = function ($value) use ($field) { |
||
| 74 | $type = $this->manager |
||
| 75 | ->getConfiguration() |
||
| 76 | ->getTypeRegistry()->get($this->metadata->getTypeOfField($field)); |
||
| 77 | |||
| 78 | return $type->toApiValue($value); |
||
| 79 | }; |
||
| 80 | |||
| 81 | if (is_array($values)) { |
||
| 82 | $values = array_map($caster, $values); |
||
| 83 | } else { |
||
| 84 | $values = $caster($values); |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | $apiCriteria[$this->metadata->getApiFieldName($field)] = $values; |
||
| 89 | } |
||
| 90 | |||
| 91 | return $apiCriteria; |
||
| 92 | } |
||
| 93 | |||
| 111 |