| Conditions | 10 |
| Paths | 160 |
| 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 |
||
| 164 | public function send() |
||
| 165 | { |
||
| 166 | // Preparing Parameters |
||
| 167 | $paramInQuery = null; |
||
| 168 | if (!empty($this->query)) { |
||
| 169 | $paramInQuery = '?' . http_build_query($this->query); |
||
| 170 | } |
||
| 171 | |||
| 172 | // Preparing Header |
||
| 173 | if (empty($this->requestHeader)) { |
||
| 174 | $this->requestHeader = []; |
||
| 175 | } |
||
| 176 | $header = array_merge( |
||
| 177 | [ |
||
| 178 | 'Accept' => 'application/json' |
||
| 179 | ], |
||
| 180 | $this->requestHeader |
||
| 181 | ); |
||
| 182 | |||
| 183 | // Defining Variables |
||
| 184 | $serverUrl = null; |
||
|
|
|||
| 185 | $basePath = ""; |
||
| 186 | $pathName = ""; |
||
| 187 | if ($this->swaggerSchema->getSpecificationVersion() === '3') { |
||
| 188 | $serverUrl = $this->swaggerSchema->getServerUrl(); |
||
| 189 | } else { |
||
| 190 | $httpSchema = $this->swaggerSchema->getHttpSchema(); |
||
| 191 | $host = $this->swaggerSchema->getHost(); |
||
| 192 | $basePath = $this->swaggerSchema->getBasePath(); |
||
| 193 | $pathName = $this->path; |
||
| 194 | $serverUrl = "$httpSchema://$host$basePath$pathName$paramInQuery"; |
||
| 195 | } |
||
| 196 | |||
| 197 | // Check if the body is the expected before request |
||
| 198 | $bodyRequestDef = $this->swaggerSchema->getRequestParameters("$basePath$pathName", $this->method); |
||
| 199 | $bodyRequestDef->match($this->requestBody); |
||
| 200 | |||
| 201 | // Make the request |
||
| 202 | $request = new Request( |
||
| 203 | $this->method, |
||
| 204 | $serverUrl, |
||
| 205 | $header, |
||
| 206 | json_encode($this->requestBody) |
||
| 207 | ); |
||
| 208 | |||
| 209 | $statusReturned = null; |
||
| 210 | try { |
||
| 211 | $response = $this->handleRequest($request); |
||
| 212 | $responseHeader = $response->getHeaders(); |
||
| 213 | $responseBody = json_decode((string) $response->getBody(), true); |
||
| 214 | $statusReturned = $response->getStatusCode(); |
||
| 215 | } catch (BadResponseException $ex) { |
||
| 216 | $responseHeader = $ex->getResponse()->getHeaders(); |
||
| 217 | $responseBody = json_decode((string) $ex->getResponse()->getBody(), true); |
||
| 218 | $statusReturned = $ex->getResponse()->getStatusCode(); |
||
| 219 | } |
||
| 220 | |||
| 221 | // Assert results |
||
| 222 | if ($this->statusExpected != $statusReturned) { |
||
| 223 | throw new StatusCodeNotMatchedException( |
||
| 224 | "Status code not matched $statusReturned", |
||
| 225 | $responseBody |
||
| 226 | ); |
||
| 227 | } |
||
| 228 | |||
| 229 | $bodyResponseDef = $this->swaggerSchema->getResponseParameters( |
||
| 230 | "$basePath$pathName", |
||
| 231 | $this->method, |
||
| 232 | $this->statusExpected |
||
| 233 | ); |
||
| 234 | $bodyResponseDef->match($responseBody); |
||
| 235 | |||
| 236 | if (count($this->assertHeader) > 0) { |
||
| 237 | foreach ($this->assertHeader as $key => $value) { |
||
| 238 | if (!isset($responseHeader[$key]) || strpos($responseHeader[$key][0], $value) === false) { |
||
| 239 | throw new NotMatchedException( |
||
| 240 | "Does not exists header '$key' with value '$value'", |
||
| 241 | $responseHeader |
||
| 242 | ); |
||
| 243 | } |
||
| 244 | } |
||
| 245 | } |
||
| 246 | |||
| 247 | return $responseBody; |
||
| 248 | } |
||
| 249 | } |
||
| 250 |
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.