| Conditions | 8 |
| Paths | 64 |
| Total Lines | 57 |
| Code Lines | 29 |
| 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 |
||
| 44 | public function normalize($object, $format = null, array $context = []): array |
||
| 45 | { |
||
| 46 | $nested = $object->getMessage() || $object->getReason(); |
||
| 47 | |||
| 48 | $errors = []; |
||
| 49 | |||
| 50 | foreach ($object as $item) { |
||
| 51 | $errors[] = $this->normalizer->normalize($item, $format, $context); |
||
| 52 | } |
||
| 53 | |||
| 54 | $links = []; |
||
| 55 | |||
| 56 | if (\count($object->getRelations())) { |
||
| 57 | // @phpstan-ignore-next-line |
||
| 58 | $links = \array_merge($links, $this->normalizer->normalize($object->getRelations(), $format, $context)); |
||
| 59 | } |
||
| 60 | |||
| 61 | if (\count($object->getActions())) { |
||
| 62 | // @phpstan-ignore-next-line |
||
| 63 | $links = \array_merge($links, $this->normalizer->normalize($object->getActions(), $format, $context)); |
||
| 64 | } |
||
| 65 | |||
| 66 | if ($nested) { |
||
| 67 | $innerError = new ErrorResource( |
||
| 68 | $object->getMessage(), |
||
| 69 | $object->getReason(), |
||
| 70 | $object->getPath(), |
||
| 71 | $object->getAttributes(), |
||
| 72 | $object->getIdentifier() |
||
| 73 | ); |
||
| 74 | |||
| 75 | /** @var array $data */ |
||
| 76 | $data = $this->normalizer->normalize($innerError, $format, $context); |
||
| 77 | |||
| 78 | if (\count($links)) { |
||
| 79 | $data['_links'] = $links; |
||
| 80 | } |
||
| 81 | |||
| 82 | $data['_embedded'] = [ |
||
| 83 | 'errors' => $errors, |
||
| 84 | ]; |
||
| 85 | |||
| 86 | return $data; |
||
| 87 | } |
||
| 88 | |||
| 89 | $data = [ |
||
| 90 | 'total' => \count($errors), |
||
| 91 | '_embedded' => [ |
||
| 92 | 'errors' => $errors, |
||
| 93 | ], |
||
| 94 | ]; |
||
| 95 | |||
| 96 | if (\count($links)) { |
||
| 97 | $data['_links'] = $links; |
||
| 98 | } |
||
| 99 | |||
| 100 | return $data; |
||
| 101 | } |
||
| 103 |