| Conditions | 15 |
| Paths | 200 |
| Total Lines | 85 |
| 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 |
||
| 197 | public function send() |
||
| 198 | { |
||
| 199 | // Process URI based on the OpenAPI schema |
||
| 200 | $uriSchema = new Uri($this->schema->getServerUrl()); |
||
| 201 | |||
| 202 | if (empty($uriSchema->getScheme())) { |
||
| 203 | $uriSchema = $uriSchema->withScheme($this->psr7Request->getUri()->getScheme()); |
||
| 204 | } |
||
| 205 | |||
| 206 | if (empty($uriSchema->getHost())) { |
||
| 207 | $uriSchema = $uriSchema->withHost($this->psr7Request->getUri()->getHost()); |
||
| 208 | } |
||
| 209 | |||
| 210 | $uri = $this->psr7Request->getUri() |
||
| 211 | ->withScheme($uriSchema->getScheme()) |
||
| 212 | ->withHost($uriSchema->getHost()) |
||
| 213 | ->withPort($uriSchema->getPort()) |
||
| 214 | ->withPath($uriSchema->getPath() . $this->psr7Request->getUri()->getPath()); |
||
| 215 | |||
| 216 | if (!preg_match("~^{$this->schema->getBasePath()}~", $uri->getPath())) { |
||
| 217 | $uri = $uri->withPath($this->schema->getBasePath() . $uri->getPath()); |
||
| 218 | } |
||
| 219 | |||
| 220 | $this->psr7Request = $this->psr7Request->withUri($uri); |
||
| 221 | |||
| 222 | // Prepare Body to Match Against Specification |
||
| 223 | $requestBody = $this->psr7Request->getBody(); |
||
| 224 | if (!empty($requestBody)) { |
||
| 225 | $requestBody = $requestBody->getContents(); |
||
| 226 | |||
| 227 | $contentType = $this->psr7Request->getHeaderLine("content-type"); |
||
| 228 | if (empty($contentType) || strpos($contentType, "application/json") !== false) { |
||
| 229 | $requestBody = json_decode($requestBody, true); |
||
| 230 | } elseif (strpos($contentType, "multipart/") !== false) { |
||
| 231 | $requestBody = $this->parseMultiPartForm($contentType, $requestBody); |
||
| 232 | } else { |
||
| 233 | throw new InvalidRequestException("Cannot handle Content Type '{$contentType}'"); |
||
| 234 | } |
||
| 235 | } |
||
| 236 | |||
| 237 | // Check if the body is the expected before request |
||
| 238 | $bodyRequestDef = $this->schema->getRequestParameters($this->psr7Request->getUri()->getPath(), $this->psr7Request->getMethod()); |
||
| 239 | $bodyRequestDef->match($requestBody); |
||
| 240 | |||
| 241 | // Handle Request |
||
| 242 | $response = $this->handleRequest($this->psr7Request); |
||
| 243 | $responseHeader = $response->getHeaders(); |
||
| 244 | $responseBodyStr = (string) $response->getBody(); |
||
| 245 | $responseBody = json_decode($responseBodyStr, true); |
||
| 246 | $statusReturned = $response->getStatusCode(); |
||
| 247 | |||
| 248 | // Assert results |
||
| 249 | if ($this->statusExpected != $statusReturned) { |
||
| 250 | throw new StatusCodeNotMatchedException( |
||
| 251 | "Status code not matched: Expected {$this->statusExpected}, got {$statusReturned}", |
||
| 252 | $responseBody |
||
| 253 | ); |
||
| 254 | } |
||
| 255 | |||
| 256 | $bodyResponseDef = $this->schema->getResponseParameters( |
||
| 257 | $this->psr7Request->getUri()->getPath(), |
||
| 258 | $this->psr7Request->getMethod(), |
||
| 259 | $this->statusExpected |
||
| 260 | ); |
||
| 261 | $bodyResponseDef->match($responseBody); |
||
| 262 | |||
| 263 | foreach ($this->assertHeader as $key => $value) { |
||
| 264 | if (!isset($responseHeader[$key]) || strpos($responseHeader[$key][0], $value) === false) { |
||
| 265 | throw new NotMatchedException( |
||
| 266 | "Does not exists header '$key' with value '$value'", |
||
| 267 | $responseHeader |
||
| 268 | ); |
||
| 269 | } |
||
| 270 | } |
||
| 271 | |||
| 272 | if (!empty($responseBodyStr)) { |
||
| 273 | foreach ($this->assertBody as $item) { |
||
| 274 | if (strpos($responseBodyStr, $item) === false) { |
||
| 275 | throw new NotMatchedException("Body does not contain '{$item}'"); |
||
| 276 | } |
||
| 277 | } |
||
| 278 | } |
||
| 279 | |||
| 280 | return $response; |
||
| 281 | } |
||
| 282 | |||
| 316 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.