| Conditions | 13 |
| Paths | 50 |
| Total Lines | 75 |
| 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 |
||
| 193 | public function send() |
||
| 194 | { |
||
| 195 | // Process URI based on the OpenAPI schema |
||
| 196 | $uriSchema = new Uri($this->schema->getServerUrl()); |
||
| 197 | |||
| 198 | $this->psr7Request->getUri() |
||
| 199 | ->withScheme($uriSchema->getScheme()) |
||
| 200 | ->withHost($uriSchema->getHost()) |
||
| 201 | ->withPort($uriSchema->getPort()) |
||
| 202 | ->withPath($uriSchema->getPath() . $this->psr7Request->getUri()->getPath()); |
||
| 203 | |||
| 204 | if (!preg_match("~^{$this->schema->getBasePath()}~", $this->psr7Request->getUri()->getPath())) { |
||
| 205 | $this->psr7Request->getUri()->withPath($this->schema->getBasePath() . $this->psr7Request->getUri()->getPath()); |
||
| 206 | } |
||
| 207 | |||
| 208 | // Prepare Body to Match Against Specification |
||
| 209 | $requestBody = $this->psr7Request->getBody(); |
||
| 210 | if (!empty($requestBody)) { |
||
| 211 | $requestBody = $requestBody->getContents(); |
||
| 212 | |||
| 213 | $contentType = $this->psr7Request->getHeaderLine("content-type"); |
||
| 214 | if (empty($contentType) || strpos($contentType, "application/json") !== false) { |
||
| 215 | $requestBody = json_decode($requestBody, true); |
||
| 216 | } elseif (strpos($contentType, "multipart/") !== false) { |
||
| 217 | $requestBody = $this->parseMultiPartForm($contentType, $requestBody); |
||
| 218 | } else { |
||
| 219 | throw new InvalidRequestException("Cannot handle Content Type '{$contentType}'"); |
||
| 220 | } |
||
| 221 | } |
||
| 222 | |||
| 223 | // Check if the body is the expected before request |
||
| 224 | $bodyRequestDef = $this->schema->getRequestParameters($this->psr7Request->getUri()->getPath(), $this->psr7Request->getMethod()); |
||
| 225 | $bodyRequestDef->match($requestBody); |
||
| 226 | |||
| 227 | // Handle Request |
||
| 228 | $response = $this->handleRequest($this->psr7Request); |
||
| 229 | $responseHeader = $response->getHeaders(); |
||
| 230 | $responseBodyStr = (string) $response->getBody(); |
||
| 231 | $responseBody = json_decode($responseBodyStr, true); |
||
| 232 | $statusReturned = $response->getStatusCode(); |
||
| 233 | |||
| 234 | // Assert results |
||
| 235 | if ($this->statusExpected != $statusReturned) { |
||
| 236 | throw new StatusCodeNotMatchedException( |
||
| 237 | "Status code not matched: Expected {$this->statusExpected}, got {$statusReturned}", |
||
| 238 | $responseBody |
||
| 239 | ); |
||
| 240 | } |
||
| 241 | |||
| 242 | $bodyResponseDef = $this->schema->getResponseParameters( |
||
| 243 | $this->psr7Request->getUri()->getPath(), |
||
| 244 | $this->psr7Request->getMethod(), |
||
| 245 | $this->statusExpected |
||
| 246 | ); |
||
| 247 | $bodyResponseDef->match($responseBody); |
||
| 248 | |||
| 249 | foreach ($this->assertHeader as $key => $value) { |
||
| 250 | if (!isset($responseHeader[$key]) || strpos($responseHeader[$key][0], $value) === false) { |
||
| 251 | throw new NotMatchedException( |
||
| 252 | "Does not exists header '$key' with value '$value'", |
||
| 253 | $responseHeader |
||
| 254 | ); |
||
| 255 | } |
||
| 256 | } |
||
| 257 | |||
| 258 | if (!empty($responseBodyStr)) { |
||
| 259 | foreach ($this->assertBody as $item) { |
||
| 260 | if (strpos($responseBodyStr, $item) === false) { |
||
| 261 | throw new NotMatchedException("Body does not contain '{$item}'"); |
||
| 262 | } |
||
| 263 | } |
||
| 264 | } |
||
| 265 | |||
| 266 | return $response; |
||
| 267 | } |
||
| 268 | |||
| 302 |
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: