Conditions | 14 |
Paths | 9 |
Total Lines | 53 |
Code Lines | 30 |
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 |
||
47 | public function onKernelView(GetResponseForControllerResultEvent $event): void |
||
48 | { |
||
49 | $controllerResult = $event->getControllerResult(); |
||
50 | $request = $event->getRequest(); |
||
51 | |||
52 | if ($controllerResult instanceof Response || !(($attributes = RequestAttributesExtractor::extractAttributes($request))['respond'] ?? $request->attributes->getBoolean('_api_respond', false))) { |
||
53 | return; |
||
54 | } |
||
55 | |||
56 | if (!$attributes) { |
||
57 | $this->serializeRawData($event, $request, $controllerResult); |
||
58 | |||
59 | return; |
||
60 | } |
||
61 | |||
62 | $context = $this->serializerContextBuilder->createFromRequest($request, true, $attributes); |
||
63 | |||
64 | if ( |
||
65 | (isset($context['output']) && \array_key_exists('class', $context['output']) && null === $context['output']['class']) |
||
66 | || |
||
67 | ( |
||
68 | null === $controllerResult && isset($context['input']) && \array_key_exists('class', $context['input']) && |
||
69 | null === $context['input']['class'] |
||
70 | ) |
||
71 | ) { |
||
72 | $event->setControllerResult(''); |
||
73 | |||
74 | return; |
||
75 | } |
||
76 | |||
77 | if ($included = $request->attributes->get('_api_included')) { |
||
78 | $context['api_included'] = $included; |
||
79 | } |
||
80 | $resources = new ResourceList(); |
||
81 | $context['resources'] = &$resources; |
||
82 | |||
83 | $resourcesToPush = new ResourceList(); |
||
84 | $context['resources_to_push'] = &$resourcesToPush; |
||
85 | |||
86 | $request->attributes->set('_api_normalization_context', $context); |
||
87 | |||
88 | $event->setControllerResult($this->serializer->serialize($controllerResult, $request->getRequestFormat(), $context)); |
||
89 | |||
90 | $request->attributes->set('_resources', $request->attributes->get('_resources', []) + (array) $resources); |
||
91 | if (!\count($resourcesToPush)) { |
||
92 | return; |
||
93 | } |
||
94 | |||
95 | $linkProvider = $request->attributes->get('_links', new GenericLinkProvider()); |
||
96 | foreach ($resourcesToPush as $resourceToPush) { |
||
97 | $linkProvider = $linkProvider->withLink(new Link('preload', $resourceToPush)); |
||
98 | } |
||
99 | $request->attributes->set('_links', $linkProvider); |
||
100 | } |
||
124 |