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