Conditions | 10 |
Paths | 8 |
Total Lines | 40 |
Code Lines | 23 |
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 |
||
63 | protected function query($date, $base, $targets) |
||
64 | { |
||
65 | $url = $this->url . '/' . $date; |
||
66 | $query = []; |
||
67 | |||
68 | // add base to query string |
||
69 | if ($base !== 'EUR') { |
||
70 | $query[] = 'base=' . $base; |
||
71 | } |
||
72 | |||
73 | // add symbols to query string |
||
74 | if (!empty($targets)) { |
||
75 | $query[] = 'symbols=' . implode(',', $targets); |
||
76 | } |
||
77 | |||
78 | // append query string to url |
||
79 | if (!empty($query)) { |
||
80 | $url .= '?' . implode('&', $query); |
||
81 | } |
||
82 | |||
83 | // query the API |
||
84 | try { |
||
85 | $response = $this->guzzle->request('GET', $url); |
||
86 | } catch (TransferException $e) { |
||
87 | throw new ConnectionException($e->getMessage()); |
||
88 | } |
||
89 | |||
90 | // process response |
||
91 | $response = json_decode($response->getBody(), true); |
||
92 | if (isset($response['rates']) && is_array($response['rates']) && |
||
93 | isset($response['base']) && isset($response['date'])) { |
||
94 | return new Result( |
||
95 | $response['base'], |
||
96 | new DateTime($response['date']), |
||
97 | $response['rates'] |
||
98 | ); |
||
99 | } elseif (isset($response['error'])) { |
||
100 | throw new ResponseException($response['error']); |
||
101 | } else { |
||
102 | throw new ResponseException('Response body is malformed.'); |
||
103 | } |
||
106 |