| Conditions | 10 |
| Paths | 49 |
| Total Lines | 40 |
| 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 |
||
| 36 | public function createFromRequest(Request $request, bool $normalization, array $attributes = null): array |
||
| 37 | { |
||
| 38 | if (null === $attributes && !$attributes = RequestAttributesExtractor::extractAttributes($request)) { |
||
| 39 | throw new RuntimeException('Request attributes are not valid.'); |
||
| 40 | } |
||
| 41 | |||
| 42 | $resourceMetadata = $this->resourceMetadataFactory->create($attributes['resource_class']); |
||
| 43 | $key = $normalization ? 'normalization_context' : 'denormalization_context'; |
||
| 44 | |||
| 45 | $operationKey = null; |
||
| 46 | |||
| 47 | if (isset($attributes['collection_operation_name'])) { |
||
| 48 | $operationKey = 'collection_operation_name'; |
||
| 49 | } elseif (isset($attributes['subresource_operation_name'])) { |
||
| 50 | $operationKey = 'subresource_operation_name'; |
||
| 51 | } |
||
| 52 | |||
| 53 | if (null !== $operationKey) { |
||
| 54 | $attribute = $attributes[$operationKey]; |
||
| 55 | $context = $resourceMetadata->getCollectionOperationAttribute($attribute, $key, [], true); |
||
| 56 | $context[$operationKey] = $attribute; |
||
| 57 | } else { |
||
| 58 | $context = $resourceMetadata->getItemOperationAttribute($attributes['item_operation_name'], $key, [], true); |
||
| 59 | $context['item_operation_name'] = $attributes['item_operation_name']; |
||
| 60 | } |
||
| 61 | |||
| 62 | if (!$normalization && !isset($context['api_allow_update'])) { |
||
| 63 | $context['api_allow_update'] = Request::METHOD_PUT === $request->getMethod(); |
||
| 64 | } |
||
| 65 | |||
| 66 | $context['resource_class'] = $attributes['resource_class']; |
||
| 67 | $context['request_uri'] = $request->getRequestUri(); |
||
| 68 | |||
| 69 | if (isset($attributes['subresource_property'])) { |
||
| 70 | $context['subresource_property'] = $attributes['subresource_property']; |
||
| 71 | $context['subresource_resource_class'] = $attributes['subresource_resource_class'] ?? null; |
||
| 72 | } |
||
| 73 | |||
| 74 | return $context; |
||
| 75 | } |
||
| 76 | } |
||
| 77 |