Conditions | 10 |
Paths | 25 |
Total Lines | 78 |
Code Lines | 37 |
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 |
||
141 | public function callEndpoint(string $action, string $method, array $params = [], array $extraOptions = []) |
||
142 | { |
||
143 | // Check our method... |
||
144 | if (! in_array(strtoupper($method), ['GET', 'POST'])) { |
||
145 | throw InvalidFactomApiConfig::invalidMethodCalled(); |
||
146 | } |
||
147 | |||
148 | $options = [ |
||
149 | 'headers' => [ |
||
150 | 'Content-Type' => self::HEADER_CONTENT_TYPE, |
||
151 | 'Accept' => self::HEADER_ACCEPT, |
||
152 | ], |
||
153 | // 'verify' => false, |
||
154 | 'json' => [ |
||
155 | 'jsonrpc' => self::JSON_RPC, |
||
156 | 'id' => self::REQUEST_ID, |
||
157 | 'method' => strtolower($action), |
||
158 | 'params' => $params, |
||
159 | ], |
||
160 | ] + $extraOptions; |
||
161 | |||
162 | // Append certificate verification |
||
163 | // if ($this->ssl) { |
||
164 | // $options['verify'] = $this->certificate; |
||
165 | // $options['cert'] = [ |
||
166 | // 'cert' => [ |
||
167 | // $this->certificate, |
||
168 | // $this->password |
||
169 | // ], |
||
170 | // ]; |
||
171 | // } |
||
172 | |||
173 | // Append authentication to params |
||
174 | if (! empty($this->username) && ! empty($this->password)) { |
||
175 | $options['auth'] = [ |
||
176 | 'username' => $this->username, |
||
177 | 'password' => $this->password, |
||
178 | ]; |
||
179 | } |
||
180 | |||
181 | $response = null; |
||
182 | $error = null; |
||
183 | |||
184 | // Make the call to factom server |
||
185 | try { |
||
186 | $response = $this->client->{strtolower($method)}($this->url, $options); |
||
187 | } catch(RequestException $e) { |
||
188 | $error = $e->getMessage(); |
||
189 | } |
||
190 | |||
191 | if (!empty($error)) { |
||
192 | throw InvalidFactomApiConfig::invalidApiResponse($error, $action); |
||
193 | } |
||
194 | |||
195 | $status_code = $response->getStatusCode(); |
||
196 | $reason_phrase = $response->getReasonPhrase(); |
||
197 | $body = (string) $response->getBody()->getContents(); |
||
198 | |||
199 | // Check for empty body |
||
200 | if (empty($body)) { |
||
201 | throw InvalidFactomApiConfig::emptyApiResponse($action); |
||
202 | } elseif ($status_code != 200) { |
||
203 | throw InvalidFactomApiConfig::invalidApiResponse($reason_phrase, $action); |
||
204 | } |
||
205 | |||
206 | // return Json |
||
207 | if ($json_body = json_decode($body)) { |
||
208 | // Check for empty result |
||
209 | if (empty($json_body->result)) { |
||
210 | throw InvalidFactomApiConfig::emptyApiResponse($action); |
||
211 | } |
||
212 | |||
213 | return $json_body->result; |
||
214 | } |
||
215 | |||
216 | // return Response |
||
217 | return $body; |
||
218 | } |
||
219 | } |
||
220 |
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.