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