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 | |||
133 | /** |
||
134 | * @param ResponseInterface|Exception|ClientExceptionInterface|callable $result |
||
135 | * |
||
136 | * @throws \InvalidArgumentException |
||
137 | * |
||
138 | * @return callable |
||
139 | */ |
||
140 | 4 | private static function makeCallable($result) |
|
141 | { |
||
142 | 4 | if (is_callable($result)) { |
|
143 | 1 | return $result; |
|
144 | } |
||
145 | |||
146 | 3 | if ($result instanceof ResponseInterface) { |
|
147 | return function () use ($result) { |
||
148 | 1 | return $result; |
|
149 | 2 | }; |
|
150 | } |
||
151 | |||
152 | 1 | if ($result instanceof \Exception) { |
|
153 | 1 | return function () use ($result) { |
|
154 | 1 | throw $result; |
|
155 | 1 | }; |
|
156 | } |
||
157 | |||
158 | throw new \InvalidArgumentException('Result must be either a response, an exception, or a callable'); |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * Adds an exception that will be thrown. |
||
163 | */ |
||
164 | 2 | View Code Duplication | public function addException(\Exception $exception) |
171 | |||
172 | /** |
||
173 | * Sets the default exception to throw when the list of added exceptions and responses is exhausted. |
||
174 | * |
||
175 | * If both a default exception and a default response are set, the exception will be thrown. |
||
176 | */ |
||
177 | View Code Duplication | public function setDefaultException(\Exception $defaultException = null) |
|
184 | |||
185 | /** |
||
186 | * Adds a response that will be returned in first in first out order. |
||
187 | */ |
||
188 | public function addResponse(ResponseInterface $response) |
||
192 | |||
193 | /** |
||
194 | * Sets the default response to be returned when the list of added exceptions and responses is exhausted. |
||
195 | */ |
||
196 | public function setDefaultResponse(ResponseInterface $defaultResponse = null) |
||
200 | |||
201 | /** |
||
202 | * Returns requests that were sent. |
||
203 | * |
||
204 | * @return RequestInterface[] |
||
205 | */ |
||
206 | public function getRequests() |
||
210 | |||
211 | public function getLastRequest() |
||
215 | |||
216 | public function reset() |
||
224 | } |
||
225 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.