| Conditions | 10 |
| Paths | 64 |
| 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 |
||
| 160 | public function send() |
||
| 161 | { |
||
| 162 | // Preparing Parameters |
||
| 163 | $paramInQuery = null; |
||
| 164 | if (!empty($this->query)) { |
||
| 165 | $paramInQuery = '?' . http_build_query($this->query); |
||
| 166 | } |
||
| 167 | |||
| 168 | // Preparing Header |
||
| 169 | if (empty($this->requestHeader)) { |
||
| 170 | $this->requestHeader = []; |
||
| 171 | } |
||
| 172 | $header = array_merge( |
||
| 173 | [ |
||
| 174 | 'Accept' => 'application/json' |
||
| 175 | ], |
||
| 176 | $this->requestHeader |
||
| 177 | ); |
||
| 178 | |||
| 179 | // Defining Variables |
||
| 180 | $serverUrl = $this->schema->getServerUrl(); |
||
| 181 | $basePath = $this->schema->getBasePath(); |
||
| 182 | $pathName = $this->path; |
||
| 183 | |||
| 184 | // Check if the body is the expected before request |
||
| 185 | $bodyRequestDef = $this->schema->getRequestParameters("$basePath$pathName", $this->method); |
||
| 186 | $bodyRequestDef->match($this->requestBody); |
||
| 187 | |||
| 188 | // Make the request |
||
| 189 | $request = Request::getInstance(Uri::getInstanceFromString($serverUrl . $pathName . $paramInQuery)) |
||
| 190 | ->withMethod($this->method); |
||
| 191 | |||
| 192 | if (!empty($this->requestBody)) { |
||
| 193 | $request->withBody(new MemoryStream(json_encode($this->requestBody))); |
||
| 194 | } |
||
| 195 | |||
| 196 | foreach ($header as $key => $value) { |
||
| 197 | $request->withHeader($key, $value); |
||
| 198 | } |
||
| 199 | |||
| 200 | $statusReturned = null; |
||
|
|
|||
| 201 | |||
| 202 | $response = $this->handleRequest($request); |
||
| 203 | $responseHeader = $response->getHeaders(); |
||
| 204 | $responseBody = json_decode((string) $response->getBody(), true); |
||
| 205 | $statusReturned = $response->getStatusCode(); |
||
| 206 | |||
| 207 | // Assert results |
||
| 208 | if ($this->statusExpected != $statusReturned) { |
||
| 209 | throw new StatusCodeNotMatchedException( |
||
| 210 | "Status code not matched: Expected {$this->statusExpected}, got {$statusReturned}", |
||
| 211 | $responseBody |
||
| 212 | ); |
||
| 213 | } |
||
| 214 | |||
| 215 | $bodyResponseDef = $this->schema->getResponseParameters( |
||
| 216 | "$basePath$pathName", |
||
| 217 | $this->method, |
||
| 218 | $this->statusExpected |
||
| 219 | ); |
||
| 220 | $bodyResponseDef->match($responseBody); |
||
| 221 | |||
| 222 | if (count($this->assertHeader) > 0) { |
||
| 223 | foreach ($this->assertHeader as $key => $value) { |
||
| 224 | if (!isset($responseHeader[$key]) || strpos($responseHeader[$key][0], $value) === false) { |
||
| 225 | throw new NotMatchedException( |
||
| 226 | "Does not exists header '$key' with value '$value'", |
||
| 227 | $responseHeader |
||
| 228 | ); |
||
| 229 | } |
||
| 230 | } |
||
| 231 | } |
||
| 232 | |||
| 233 | return $responseBody; |
||
| 234 | } |
||
| 235 | } |
||
| 236 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.