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) |
|
152 | |||
153 | /** |
||
154 | * Adds an exception that will be thrown. |
||
155 | */ |
||
156 | 2 | View Code Duplication | public function addException(\Exception $exception) |
163 | |||
164 | /** |
||
165 | * Sets the default exception to throw when the list of added exceptions and responses is exhausted. |
||
166 | * |
||
167 | * If both a default exception and a default response are set, the exception will be thrown. |
||
168 | */ |
||
169 | View Code Duplication | public function setDefaultException(\Exception $defaultException = null) |
|
176 | |||
177 | /** |
||
178 | * Adds a response that will be returned in first in first out order. |
||
179 | */ |
||
180 | public function addResponse(ResponseInterface $response) |
||
184 | |||
185 | /** |
||
186 | * Sets the default response to be returned when the list of added exceptions and responses is exhausted. |
||
187 | */ |
||
188 | public function setDefaultResponse(ResponseInterface $defaultResponse = null) |
||
192 | |||
193 | /** |
||
194 | * Returns requests that were sent. |
||
195 | * |
||
196 | * @return RequestInterface[] |
||
197 | */ |
||
198 | public function getRequests() |
||
202 | |||
203 | public function getLastRequest() |
||
207 | |||
208 | public function reset() |
||
216 | } |
||
217 |
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.