| Conditions | 19 |
| Paths | 520 |
| Total Lines | 89 |
| Code Lines | 53 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| Bugs | 1 | Features | 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 |
||
| 213 | public function send(bool $matchQueryParams = true): ResponseInterface |
||
| 214 | { |
||
| 215 | // Process URI based on the OpenAPI schema |
||
| 216 | $uriSchema = new Uri($this->schema->getServerUrl()); |
||
|
|
|||
| 217 | |||
| 218 | if (empty($uriSchema->getScheme())) { |
||
| 219 | $uriSchema = $uriSchema->withScheme($this->psr7Request->getUri()->getScheme()); |
||
| 220 | } |
||
| 221 | |||
| 222 | if (empty($uriSchema->getHost())) { |
||
| 223 | $uriSchema = $uriSchema->withHost($this->psr7Request->getUri()->getHost()); |
||
| 224 | } |
||
| 225 | |||
| 226 | $uri = $this->psr7Request->getUri() |
||
| 227 | ->withScheme($uriSchema->getScheme()) |
||
| 228 | ->withHost($uriSchema->getHost()) |
||
| 229 | ->withPort($uriSchema->getPort()) |
||
| 230 | ->withPath($uriSchema->getPath() . $this->psr7Request->getUri()->getPath()); |
||
| 231 | |||
| 232 | if (!preg_match("~^{$this->schema->getBasePath()}~", $uri->getPath())) { |
||
| 233 | $uri = $uri->withPath($this->schema->getBasePath() . $uri->getPath()); |
||
| 234 | } |
||
| 235 | |||
| 236 | $this->psr7Request = $this->psr7Request->withUri($uri); |
||
| 237 | |||
| 238 | // Prepare Body to Match Against Specification |
||
| 239 | $rawBody = $this->psr7Request->getBody()->getContents(); |
||
| 240 | $isXmlBody = false; |
||
| 241 | $requestBody = null; |
||
| 242 | $contentType = $this->psr7Request->getHeaderLine("content-type"); |
||
| 243 | if (!empty($rawBody)) { |
||
| 244 | if (str_contains($contentType, 'application/xml') || str_contains($contentType, 'text/xml')) { |
||
| 245 | $isXmlBody = new XmlDocument($rawBody); |
||
| 246 | } elseif (empty($contentType) || str_contains($contentType, "application/json")) { |
||
| 247 | $requestBody = json_decode($rawBody, true); |
||
| 248 | } elseif (str_contains($contentType, "multipart/")) { |
||
| 249 | $requestBody = $this->parseMultiPartForm($contentType, $rawBody); |
||
| 250 | } else { |
||
| 251 | throw new InvalidRequestException("Cannot handle Content Type '$contentType'"); |
||
| 252 | } |
||
| 253 | |||
| 254 | } |
||
| 255 | |||
| 256 | // Check if the body is the expected before request |
||
| 257 | if ($isXmlBody === false) { |
||
| 258 | $bodyRequestDef = $this->schema->getRequestParameters($this->psr7Request->getUri()->getPath(), $this->psr7Request->getMethod(), $matchQueryParams ? $this->psr7Request->getUri()->getQuery() : null); |
||
| 259 | $bodyRequestDef->match($requestBody); |
||
| 260 | } |
||
| 261 | |||
| 262 | // Handle Request |
||
| 263 | $response = $this->handleRequest($this->psr7Request); |
||
| 264 | $responseHeader = $response->getHeaders(); |
||
| 265 | $responseBodyStr = (string) $response->getBody(); |
||
| 266 | $responseBody = json_decode($responseBodyStr, true); |
||
| 267 | $statusReturned = $response->getStatusCode(); |
||
| 268 | |||
| 269 | // Assert results |
||
| 270 | if ($this->statusExpected != $statusReturned) { |
||
| 271 | throw new StatusCodeNotMatchedException( |
||
| 272 | "Status code not matched: Expected $this->statusExpected, got $statusReturned", |
||
| 273 | $responseBody |
||
| 274 | ); |
||
| 275 | } |
||
| 276 | |||
| 277 | $bodyResponseDef = $this->schema->getResponseParameters( |
||
| 278 | $this->psr7Request->getUri()->getPath(), |
||
| 279 | $this->psr7Request->getMethod(), |
||
| 280 | $this->statusExpected |
||
| 281 | ); |
||
| 282 | $bodyResponseDef->match($responseBody); |
||
| 283 | |||
| 284 | foreach ($this->assertHeader as $key => $value) { |
||
| 285 | if (!isset($responseHeader[$key]) || !str_contains($responseHeader[$key][0], $value)) { |
||
| 286 | throw new NotMatchedException( |
||
| 287 | "Does not exists header '$key' with value '$value'", |
||
| 288 | $responseHeader |
||
| 289 | ); |
||
| 290 | } |
||
| 291 | } |
||
| 292 | |||
| 293 | if (!empty($responseBodyStr)) { |
||
| 294 | foreach ($this->assertBody as $item) { |
||
| 295 | if (!str_contains($responseBodyStr, $item)) { |
||
| 296 | throw new NotMatchedException("Body does not contain '$item'"); |
||
| 297 | } |
||
| 298 | } |
||
| 299 | } |
||
| 300 | |||
| 301 | return $response; |
||
| 302 | } |
||
| 337 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.