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 |
||
24 | class Client implements HttpClient, HttpAsyncClient |
||
25 | { |
||
26 | use HttpAsyncClientEmulator; |
||
27 | use VersionBridgeClient; |
||
28 | |||
29 | /** |
||
30 | * @var ResponseFactory |
||
31 | */ |
||
32 | private $responseFactory; |
||
33 | |||
34 | /** |
||
35 | * @var array |
||
36 | */ |
||
37 | private $conditionalResults = []; |
||
38 | |||
39 | /** |
||
40 | * @var RequestInterface[] |
||
41 | */ |
||
42 | private $requests = []; |
||
43 | |||
44 | /** |
||
45 | * @var ResponseInterface[] |
||
46 | */ |
||
47 | private $responses = []; |
||
48 | |||
49 | /** |
||
50 | * @var ResponseInterface|null |
||
51 | */ |
||
52 | private $defaultResponse; |
||
53 | |||
54 | /** |
||
55 | * @var Exception[] |
||
56 | */ |
||
57 | private $exceptions = []; |
||
58 | |||
59 | /** |
||
60 | * @var Exception|null |
||
61 | */ |
||
62 | private $defaultException; |
||
63 | |||
64 | 15 | public function __construct(ResponseFactory $responseFactory = null) |
|
68 | |||
69 | /** |
||
70 | * {@inheritdoc} |
||
71 | */ |
||
72 | 11 | public function doSendRequest(RequestInterface $request) |
|
111 | |||
112 | /** |
||
113 | * @param RequestMatcher $requestMatcher |
||
114 | * @param ResponseInterface|\Exception|callable $result |
||
115 | */ |
||
116 | 4 | public function on(RequestMatcher $requestMatcher, $result) |
|
145 | |||
146 | /** |
||
147 | * Adds an exception that will be thrown. |
||
148 | */ |
||
149 | 2 | View Code Duplication | public function addException(\Exception $exception) |
156 | |||
157 | /** |
||
158 | * Sets the default exception to throw when the list of added exceptions and responses is exhausted. |
||
159 | * |
||
160 | * If both a default exception and a default response are set, the exception will be thrown. |
||
161 | */ |
||
162 | View Code Duplication | public function setDefaultException(\Exception $defaultException = null) |
|
169 | |||
170 | /** |
||
171 | * Adds a response that will be returned in first in first out order. |
||
172 | */ |
||
173 | public function addResponse(ResponseInterface $response) |
||
177 | |||
178 | /** |
||
179 | * Sets the default response to be returned when the list of added exceptions and responses is exhausted. |
||
180 | */ |
||
181 | public function setDefaultResponse(ResponseInterface $defaultResponse = null) |
||
185 | |||
186 | /** |
||
187 | * Returns requests that were sent. |
||
188 | * |
||
189 | * @return RequestInterface[] |
||
190 | */ |
||
191 | public function getRequests() |
||
195 | |||
196 | public function getLastRequest() |
||
200 | |||
201 | public function reset() |
||
209 | } |
||
210 |
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.