Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
25 | class Client implements HttpClient, HttpAsyncClient |
||
26 | { |
||
27 | use HttpAsyncClientEmulator; |
||
28 | use VersionBridgeClient; |
||
29 | |||
30 | /** |
||
31 | * @var ResponseFactory |
||
32 | */ |
||
33 | private $responseFactory; |
||
34 | |||
35 | /** |
||
36 | * @var array |
||
37 | */ |
||
38 | private $conditionalResults = []; |
||
39 | |||
40 | /** |
||
41 | * @var RequestInterface[] |
||
42 | */ |
||
43 | private $requests = []; |
||
44 | |||
45 | /** |
||
46 | * @var ResponseInterface[] |
||
47 | */ |
||
48 | private $responses = []; |
||
49 | |||
50 | /** |
||
51 | * @var ResponseInterface|null |
||
52 | */ |
||
53 | private $defaultResponse; |
||
54 | |||
55 | /** |
||
56 | * @var Exception[] |
||
57 | */ |
||
58 | private $exceptions = []; |
||
59 | |||
60 | /** |
||
61 | * @var Exception|null |
||
62 | */ |
||
63 | private $defaultException; |
||
64 | |||
65 | 15 | public function __construct(ResponseFactory $responseFactory = null) |
|
69 | |||
70 | /** |
||
71 | * {@inheritdoc} |
||
72 | */ |
||
73 | 11 | public function doSendRequest(RequestInterface $request) |
|
112 | |||
113 | /** |
||
114 | * Adds an exception to be thrown or response to be returned if the request |
||
115 | * matcher matches. |
||
116 | * |
||
117 | * For more complex logic, pass a callable as $result. The method is given |
||
118 | * the request and MUST either return a ResponseInterface or throw an |
||
119 | * exception that implements the PSR-18 / HTTPlug exception interface. |
||
120 | * |
||
121 | * @param ResponseInterface|Exception|ClientExceptionInterface|callable $result |
||
122 | */ |
||
123 | 4 | public function on(RequestMatcher $requestMatcher, $result) |
|
132 | 3 | ||
133 | /** |
||
134 | 1 | * @param ResponseInterface|Exception|ClientExceptionInterface|callable $result |
|
135 | 2 | * |
|
136 | * @throws \InvalidArgumentException |
||
137 | 2 | * |
|
138 | 1 | * @return callable |
|
139 | 1 | */ |
|
140 | 1 | private static function ensureCallable($result) |
|
159 | |||
160 | /** |
||
161 | * Adds an exception that will be thrown. |
||
162 | */ |
||
163 | View Code Duplication | public function addException(\Exception $exception) |
|
170 | |||
171 | /** |
||
172 | * Sets the default exception to throw when the list of added exceptions and responses is exhausted. |
||
173 | * |
||
174 | * If both a default exception and a default response are set, the exception will be thrown. |
||
175 | */ |
||
176 | View Code Duplication | public function setDefaultException(\Exception $defaultException = null) |
|
183 | |||
184 | /** |
||
185 | * Adds a response that will be returned in first in first out order. |
||
186 | */ |
||
187 | public function addResponse(ResponseInterface $response) |
||
191 | |||
192 | /** |
||
193 | * Sets the default response to be returned when the list of added exceptions and responses is exhausted. |
||
194 | */ |
||
195 | public function setDefaultResponse(ResponseInterface $defaultResponse = null) |
||
199 | |||
200 | /** |
||
201 | * Returns requests that were sent. |
||
202 | * |
||
203 | * @return RequestInterface[] |
||
204 | */ |
||
205 | public function getRequests() |
||
209 | |||
210 | public function getLastRequest() |
||
214 | |||
215 | public function reset() |
||
223 | } |
||
224 |
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.