Conditions | 12 |
Paths | 36 |
Total Lines | 39 |
Code Lines | 24 |
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 |
||
54 | public function format($response) |
||
55 | { |
||
56 | $response->getHeaders()->set('Content-Type', 'application/vnd.api+json; charset=UTF-8'); |
||
57 | $options = $this->encodeOptions; |
||
58 | if ($this->prettyPrint) { |
||
59 | $options |= JSON_PRETTY_PRINT; |
||
60 | } |
||
61 | |||
62 | $apiDocument = $response->data; |
||
63 | |||
64 | if (!$response->isEmpty) { |
||
65 | $apiDocument = ['data' => $response->data]; |
||
66 | if (\Yii::$app->controller) { |
||
67 | $apiDocument['links'] = Link::serialize([ |
||
68 | Link::REL_SELF => Url::current([], true) |
||
69 | ]); |
||
70 | } |
||
71 | } |
||
72 | |||
73 | if ($response->isClientError || $response->isServerError) { |
||
74 | if (ArrayHelper::isAssociative($response->data)) { |
||
75 | $response->data = [$response->data]; |
||
76 | } |
||
77 | $formattedErrors = []; |
||
78 | foreach ($response->data as $error) { |
||
79 | $formattedError = array_intersect_key($error, array_flip(static::ERROR_ALLOWED_MEMBERS)); |
||
80 | foreach (static::ERROR_EXCEPTION_MAPPING as $member => $key) { |
||
81 | if (isset($error[$key])) { |
||
82 | $formattedError[$member] = (string) $error[$key]; |
||
83 | } |
||
84 | } |
||
85 | if (!empty($formattedError)) { |
||
86 | $formattedErrors[] = $formattedError; |
||
87 | } |
||
88 | } |
||
89 | $apiDocument = ['errors' => $formattedErrors]; |
||
90 | } |
||
91 | if ($apiDocument !== null) { |
||
92 | $response->content = Json::encode($apiDocument, $options); |
||
93 | } |
||
96 |
This check marks files that end in a newline character, i.e. an empy line.