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 | 11 | ||
59 | /** |
||
60 | 11 | * @var Exception|null |
|
61 | 11 | */ |
|
62 | private $defaultException; |
||
63 | |||
64 | public function __construct(ResponseFactory $responseFactory = null) |
||
68 | 7 | ||
69 | /** |
||
70 | 7 | * {@inheritdoc} |
|
71 | 1 | */ |
|
72 | public function doSendRequest(RequestInterface $request) |
||
108 | |||
109 | /** |
||
110 | * @param RequestMatcher $requestMatcher |
||
111 | * @param ResponseInterface|\Exception|callable $result |
||
112 | */ |
||
113 | public function on(RequestMatcher $requestMatcher, $result) |
||
142 | |||
143 | /** |
||
144 | * Adds an exception that will be thrown. |
||
145 | */ |
||
146 | View Code Duplication | public function addException(\Exception $exception) |
|
153 | |||
154 | /** |
||
155 | * Sets the default exception to throw when the list of added exceptions and responses is exhausted. |
||
156 | * |
||
157 | * If both a default exception and a default response are set, the exception will be thrown. |
||
158 | */ |
||
159 | View Code Duplication | public function setDefaultException(\Exception $defaultException = null) |
|
166 | |||
167 | /** |
||
168 | * Adds a response that will be returned in first in first out order. |
||
169 | */ |
||
170 | public function addResponse(ResponseInterface $response) |
||
174 | |||
175 | /** |
||
176 | * Sets the default response to be returned when the list of added exceptions and responses is exhausted. |
||
177 | */ |
||
178 | public function setDefaultResponse(ResponseInterface $defaultResponse = null) |
||
182 | |||
183 | /** |
||
184 | * Returns requests that were sent. |
||
185 | * |
||
186 | * @return RequestInterface[] |
||
187 | */ |
||
188 | public function getRequests() |
||
192 | |||
193 | public function getLastRequest() |
||
197 | |||
198 | public function reset() |
||
206 | } |
||
207 |
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.