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