Conditions | 11 |
Paths | 26 |
Total Lines | 69 |
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 |
||
191 | public function send() |
||
192 | { |
||
193 | // Process URI based on the OpenAPI schema |
||
194 | $uriSchema = new Uri($this->schema->getServerUrl()); |
||
195 | |||
196 | $this->psr7Request->getUri() |
||
197 | ->withScheme($uriSchema->getScheme()) |
||
198 | ->withHost($uriSchema->getHost()) |
||
199 | ->withPort($uriSchema->getPort()) |
||
200 | ->withPath($uriSchema->getPath() . $this->psr7Request->getUri()->getPath()); |
||
201 | |||
202 | if (!preg_match("~^{$this->schema->getBasePath()}~", $this->psr7Request->getUri()->getPath())) { |
||
203 | $this->psr7Request->getUri()->withPath($this->schema->getBasePath() . $this->psr7Request->getUri()->getPath()); |
||
204 | } |
||
205 | |||
206 | // Prepare Body to Match Against Specification |
||
207 | $requestBody = $this->psr7Request->getBody(); |
||
208 | if (!empty($requestBody)) { |
||
209 | $requestBody = $requestBody->getContents(); |
||
210 | |||
211 | $contentType = $this->psr7Request->getHeader("content-type"); |
||
212 | if (empty($contentType) || $contentType == "application/json") { |
||
213 | $requestBody = json_decode($requestBody, true); |
||
214 | } else { |
||
215 | throw new InvalidRequestException("Cannot handle Content Type '$contentType'"); |
||
216 | } |
||
217 | } |
||
218 | |||
219 | // Check if the body is the expected before request |
||
220 | $bodyRequestDef = $this->schema->getRequestParameters($this->psr7Request->getUri()->getPath(), $this->psr7Request->getMethod()); |
||
221 | $bodyRequestDef->match($requestBody); |
||
222 | |||
223 | // Handle Request |
||
224 | $response = $this->handleRequest($this->psr7Request); |
||
225 | $responseHeader = $response->getHeaders(); |
||
226 | $responseBodyStr = (string) $response->getBody(); |
||
227 | $responseBody = json_decode($responseBodyStr, true); |
||
228 | $statusReturned = $response->getStatusCode(); |
||
229 | |||
230 | // Assert results |
||
231 | if ($this->statusExpected != $statusReturned) { |
||
232 | throw new StatusCodeNotMatchedException( |
||
233 | "Status code not matched: Expected {$this->statusExpected}, got {$statusReturned}", |
||
234 | $responseBody |
||
235 | ); |
||
236 | } |
||
237 | |||
238 | $bodyResponseDef = $this->schema->getResponseParameters( |
||
239 | $this->psr7Request->getUri()->getPath(), |
||
240 | $this->psr7Request->getMethod(), |
||
241 | $this->statusExpected |
||
242 | ); |
||
243 | $bodyResponseDef->match($responseBody); |
||
244 | |||
245 | foreach ($this->assertHeader as $key => $value) { |
||
246 | if (!isset($responseHeader[$key]) || strpos($responseHeader[$key][0], $value) === false) { |
||
247 | throw new NotMatchedException( |
||
248 | "Does not exists header '$key' with value '$value'", |
||
249 | $responseHeader |
||
250 | ); |
||
251 | } |
||
252 | } |
||
253 | |||
254 | if (!empty($this->assertBody) && strpos($responseBodyStr, $this->assertBody) === false) { |
||
255 | throw new NotMatchedException("Body does not contain '{$this->assertBody}'"); |
||
256 | } |
||
257 | |||
258 | return $response; |
||
259 | } |
||
260 | } |
||
261 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: