Conditions | 12 |
Paths | 15 |
Total Lines | 41 |
Code Lines | 25 |
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 |
||
66 | public function normalize($object, $format = null, array $context = []) |
||
67 | { |
||
68 | if (isset($context['api_sub_level'])) { |
||
69 | return $this->normalizeRawCollection($object, $format, $context); |
||
70 | } |
||
71 | |||
72 | try { |
||
73 | $resourceClass = $this->resourceClassResolver->getResourceClass($object, $context['resource_class'] ?? null, true); |
||
74 | } catch (InvalidArgumentException $e) { |
||
75 | if (!isset($context['resource_class'])) { |
||
76 | return $this->normalizeRawCollection($object, $format, $context); |
||
77 | } |
||
78 | |||
79 | throw $e; |
||
80 | } |
||
81 | $data = $this->addJsonLdContext($this->contextBuilder, $resourceClass, $context); |
||
82 | $context = $this->initContext($resourceClass, $context); |
||
83 | |||
84 | if (isset($context['operation_type']) && OperationType::SUBRESOURCE === $context['operation_type']) { |
||
85 | $data['@id'] = $this->iriConverter->getSubresourceIriFromResourceClass($resourceClass, $context); |
||
86 | } else { |
||
87 | $data['@id'] = $this->iriConverter->getIriFromResourceClass($resourceClass); |
||
88 | } |
||
89 | |||
90 | $data['@type'] = 'hydra:Collection'; |
||
91 | |||
92 | $data['hydra:member'] = []; |
||
93 | foreach ($object as $obj) { |
||
94 | $data['hydra:member'][] = $this->normalizer->normalize($obj, $format, $context); |
||
95 | } |
||
96 | |||
97 | $paginated = null; |
||
98 | if ( |
||
99 | \is_array($object) || |
||
100 | ($paginated = $object instanceof PaginatorInterface) || |
||
101 | $object instanceof \Countable && !$object instanceof PartialPaginatorInterface |
||
102 | ) { |
||
103 | $data['hydra:totalItems'] = $paginated ? $object->getTotalItems() : \count($object); |
||
|
|||
104 | } |
||
105 | |||
106 | return $data; |
||
107 | } |
||
130 |