Conditions | 16 |
Paths | 193 |
Total Lines | 65 |
Code Lines | 40 |
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 |
||
40 | public function createFromRequest(Request $request, bool $normalization, array $attributes = null): array |
||
41 | { |
||
42 | if (null === $attributes && !$attributes = RequestAttributesExtractor::extractAttributes($request)) { |
||
43 | throw new RuntimeException('Request attributes are not valid.'); |
||
44 | } |
||
45 | |||
46 | $resourceMetadata = $this->resourceMetadataFactory->create($attributes['resource_class']); |
||
47 | $key = $normalization ? 'normalization_context' : 'denormalization_context'; |
||
48 | if (isset($attributes['collection_operation_name'])) { |
||
49 | $operationKey = 'collection_operation_name'; |
||
50 | $operationType = OperationType::COLLECTION; |
||
51 | } elseif (isset($attributes['item_operation_name'])) { |
||
52 | $operationKey = 'item_operation_name'; |
||
53 | $operationType = OperationType::ITEM; |
||
54 | } else { |
||
55 | $operationKey = 'subresource_operation_name'; |
||
56 | $operationType = OperationType::SUBRESOURCE; |
||
57 | } |
||
58 | |||
59 | $context = $resourceMetadata->getTypedOperationAttribute($operationType, $attributes[$operationKey], $key, [], true); |
||
60 | $context['operation_type'] = $operationType; |
||
61 | $context[$operationKey] = $attributes[$operationKey]; |
||
62 | |||
63 | if (!$normalization && !isset($context['api_allow_update'])) { |
||
64 | $context['api_allow_update'] = \in_array($request->getMethod(), ['PUT', 'PATCH'], true); |
||
65 | } |
||
66 | |||
67 | $context['resource_class'] = $attributes['resource_class']; |
||
68 | $context['input'] = $resourceMetadata->getTypedOperationAttribute($operationType, $attributes[$operationKey], 'input', null, true); |
||
69 | $context['output'] = $resourceMetadata->getTypedOperationAttribute($operationType, $attributes[$operationKey], 'output', null, true); |
||
70 | $context['request_uri'] = $request->getRequestUri(); |
||
71 | $context['uri'] = $request->getUri(); |
||
72 | |||
73 | if (isset($attributes['subresource_context'])) { |
||
74 | $context['subresource_identifiers'] = []; |
||
75 | |||
76 | foreach ($attributes['subresource_context']['identifiers'] as $key => [$id, $resourceClass]) { |
||
77 | if (!isset($context['subresource_resources'][$resourceClass])) { |
||
78 | $context['subresource_resources'][$resourceClass] = []; |
||
79 | } |
||
80 | |||
81 | $context['subresource_identifiers'][$id] = $context['subresource_resources'][$resourceClass][$id] = $request->attributes->get($id); |
||
82 | } |
||
83 | } |
||
84 | |||
85 | if (isset($attributes['subresource_property'])) { |
||
86 | $context['subresource_property'] = $attributes['subresource_property']; |
||
87 | $context['subresource_resource_class'] = $attributes['subresource_resource_class'] ?? null; |
||
88 | } |
||
89 | |||
90 | unset($context[DocumentationNormalizer::SWAGGER_DEFINITION_NAME]); |
||
91 | |||
92 | if (isset($context['skip_null_values'])) { |
||
93 | return $context; |
||
94 | } |
||
95 | |||
96 | foreach ($resourceMetadata->getItemOperations() as $operation) { |
||
97 | if ('PATCH' === $operation['method'] && \in_array('application/merge-patch+json', $operation['input_formats']['json'] ?? [], true)) { |
||
98 | $context['skip_null_values'] = true; |
||
99 | |||
100 | break; |
||
101 | } |
||
102 | } |
||
103 | |||
104 | return $context; |
||
105 | } |
||
107 |