Conditions | 11 |
Paths | 96 |
Total Lines | 76 |
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 |
||
168 | public function send() |
||
169 | { |
||
170 | // Preparing Parameters |
||
171 | $paramInQuery = null; |
||
172 | if (!empty($this->query)) { |
||
173 | $paramInQuery = '?' . http_build_query($this->query); |
||
174 | } |
||
175 | |||
176 | // Preparing Header |
||
177 | if (empty($this->requestHeader)) { |
||
178 | $this->requestHeader = []; |
||
179 | } |
||
180 | $header = array_merge( |
||
181 | [ |
||
182 | 'Accept' => 'application/json' |
||
183 | ], |
||
184 | $this->requestHeader |
||
185 | ); |
||
186 | |||
187 | // Defining Variables |
||
188 | $serverUrl = $this->schema->getServerUrl(); |
||
189 | $basePath = $this->schema->getBasePath(); |
||
190 | $pathName = $this->path; |
||
191 | |||
192 | // Check if the body is the expected before request |
||
193 | $bodyRequestDef = $this->schema->getRequestParameters("$basePath$pathName", $this->method); |
||
194 | $bodyRequestDef->match($this->requestBody); |
||
195 | |||
196 | // Make the request |
||
197 | $request = Request::getInstance(Uri::getInstanceFromString($serverUrl . $pathName . $paramInQuery)) |
||
198 | ->withMethod($this->method); |
||
199 | |||
200 | if (!empty($this->requestBody)) { |
||
201 | $request->withBody(new MemoryStream(json_encode($this->requestBody))); |
||
202 | } |
||
203 | |||
204 | foreach ($header as $key => $value) { |
||
205 | $request->withHeader($key, $value); |
||
206 | } |
||
207 | |||
208 | $response = $this->handleRequest($request); |
||
209 | $responseHeader = $response->getHeaders(); |
||
210 | $responseBodyStr = (string) $response->getBody(); |
||
211 | $responseBody = json_decode($responseBodyStr, true); |
||
212 | $statusReturned = $response->getStatusCode(); |
||
213 | |||
214 | // Assert results |
||
215 | if ($this->statusExpected != $statusReturned) { |
||
216 | throw new StatusCodeNotMatchedException( |
||
217 | "Status code not matched: Expected {$this->statusExpected}, got {$statusReturned}", |
||
218 | $responseBody |
||
219 | ); |
||
220 | } |
||
221 | |||
222 | $bodyResponseDef = $this->schema->getResponseParameters( |
||
223 | "$basePath$pathName", |
||
224 | $this->method, |
||
225 | $this->statusExpected |
||
226 | ); |
||
227 | $bodyResponseDef->match($responseBody); |
||
228 | |||
229 | foreach ($this->assertHeader as $key => $value) { |
||
230 | if (!isset($responseHeader[$key]) || strpos($responseHeader[$key][0], $value) === false) { |
||
231 | throw new NotMatchedException( |
||
232 | "Does not exists header '$key' with value '$value'", |
||
233 | $responseHeader |
||
234 | ); |
||
235 | } |
||
236 | } |
||
237 | |||
238 | if (!empty($this->assertBody) && strpos($responseBodyStr, $this->assertBody) === false) { |
||
239 | throw new NotMatchedException("Body does not contain '{$this->assertBody}'"); |
||
240 | } |
||
241 | |||
242 | return $responseBody; |
||
243 | } |
||
244 | } |
||
245 |