Conditions | 14 |
Paths | 193 |
Total Lines | 57 |
Code Lines | 35 |
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 |
||
39 | public function createFromRequest(Request $request, bool $normalization, array $attributes = null): array |
||
40 | { |
||
41 | if (null === $attributes && !$attributes = RequestAttributesExtractor::extractAttributes($request)) { |
||
42 | throw new RuntimeException('Request attributes are not valid.'); |
||
43 | } |
||
44 | |||
45 | $resourceMetadata = $this->resourceMetadataFactory->create($attributes['resource_class']); |
||
46 | $key = $normalization ? 'normalization_context' : 'denormalization_context'; |
||
47 | |||
48 | $operationKey = null; |
||
49 | $operationType = null; |
||
50 | |||
51 | if (isset($attributes['collection_operation_name'])) { |
||
52 | $operationKey = 'collection_operation_name'; |
||
53 | $operationType = OperationType::COLLECTION; |
||
54 | } elseif (isset($attributes['subresource_operation_name'])) { |
||
55 | $operationKey = 'subresource_operation_name'; |
||
56 | $operationType = OperationType::SUBRESOURCE; |
||
57 | } |
||
58 | |||
59 | if (null !== $operationKey) { |
||
60 | $attribute = $attributes[$operationKey]; |
||
61 | $context = $resourceMetadata->getCollectionOperationAttribute($attribute, $key, [], true); |
||
62 | $context[$operationKey] = $attribute; |
||
63 | } else { |
||
64 | $context = $resourceMetadata->getItemOperationAttribute($attributes['item_operation_name'], $key, [], true); |
||
65 | $context['item_operation_name'] = $attributes['item_operation_name']; |
||
66 | } |
||
67 | |||
68 | $context['operation_type'] = $operationType ? $operationType : OperationType::ITEM; |
||
69 | |||
70 | if (!$normalization && !isset($context['api_allow_update'])) { |
||
71 | $context['api_allow_update'] = Request::METHOD_PUT === $request->getMethod(); |
||
72 | } |
||
73 | |||
74 | $context['resource_class'] = $attributes['resource_class']; |
||
75 | $context['request_uri'] = $request->getRequestUri(); |
||
76 | |||
77 | if (isset($attributes['subresource_context'])) { |
||
78 | $context['subresource_identifiers'] = []; |
||
79 | |||
80 | foreach ($attributes['subresource_context']['identifiers'] as $key => list($id, $resourceClass)) { |
||
81 | if (!isset($context['subresource_resources'][$resourceClass])) { |
||
82 | $context['subresource_resources'][$resourceClass] = []; |
||
83 | } |
||
84 | |||
85 | $context['subresource_identifiers'][$id] = $context['subresource_resources'][$resourceClass][$id] = $request->attributes->get($id); |
||
86 | } |
||
87 | } |
||
88 | |||
89 | if (isset($attributes['subresource_property'])) { |
||
90 | $context['subresource_property'] = $attributes['subresource_property']; |
||
91 | $context['subresource_resource_class'] = $attributes['subresource_resource_class'] ?? null; |
||
92 | } |
||
93 | |||
94 | return $context; |
||
95 | } |
||
96 | } |
||
97 |