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 |
||
26 | class Client implements HttpClient, HttpAsyncClient |
||
27 | { |
||
28 | use HttpAsyncClientEmulator; |
||
29 | use VersionBridgeClient; |
||
30 | |||
31 | /** |
||
32 | * @var ResponseFactory|ResponseFactoryInterface |
||
33 | */ |
||
34 | private $responseFactory; |
||
35 | |||
36 | /** |
||
37 | * @var array |
||
38 | */ |
||
39 | private $conditionalResults = []; |
||
40 | |||
41 | /** |
||
42 | * @var RequestInterface[] |
||
43 | */ |
||
44 | private $requests = []; |
||
45 | |||
46 | /** |
||
47 | * @var ResponseInterface[] |
||
48 | */ |
||
49 | private $responses = []; |
||
50 | |||
51 | /** |
||
52 | * @var ResponseInterface|null |
||
53 | */ |
||
54 | private $defaultResponse; |
||
55 | |||
56 | /** |
||
57 | * @var Exception[] |
||
58 | */ |
||
59 | private $exceptions = []; |
||
60 | |||
61 | /** |
||
62 | * @var Exception|null |
||
63 | */ |
||
64 | private $defaultException; |
||
65 | |||
66 | /** |
||
67 | * @param ResponseFactory|ResponseFactoryInterface|null |
||
68 | */ |
||
69 | 15 | public function __construct($responseFactory = null) |
|
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | */ |
||
83 | 11 | public function doSendRequest(RequestInterface $request) |
|
122 | |||
123 | /** |
||
124 | * Adds an exception to be thrown or response to be returned if the request |
||
125 | * matcher matches. |
||
126 | * |
||
127 | * For more complex logic, pass a callable as $result. The method is given |
||
128 | * the request and MUST either return a ResponseInterface or throw an |
||
129 | * exception that implements the PSR-18 / HTTPlug exception interface. |
||
130 | * |
||
131 | * @param ResponseInterface|Exception|ClientExceptionInterface|callable $result |
||
132 | */ |
||
133 | 4 | public function on(RequestMatcher $requestMatcher, $result) |
|
148 | |||
149 | /** |
||
150 | * @param ResponseInterface|Exception|ClientExceptionInterface|callable $result |
||
151 | * |
||
152 | * @return callable |
||
153 | */ |
||
154 | 4 | private static function makeCallable($result) |
|
170 | |||
171 | /** |
||
172 | * Adds an exception that will be thrown. |
||
173 | */ |
||
174 | 2 | public function addException(\Exception $exception) |
|
181 | |||
182 | /** |
||
183 | * Sets the default exception to throw when the list of added exceptions and responses is exhausted. |
||
184 | * |
||
185 | * If both a default exception and a default response are set, the exception will be thrown. |
||
186 | */ |
||
187 | 2 | public function setDefaultException(\Exception $defaultException = null) |
|
194 | |||
195 | /** |
||
196 | * Adds a response that will be returned in first in first out order. |
||
197 | */ |
||
198 | 4 | public function addResponse(ResponseInterface $response) |
|
202 | |||
203 | /** |
||
204 | * Sets the default response to be returned when the list of added exceptions and responses is exhausted. |
||
205 | */ |
||
206 | 2 | public function setDefaultResponse(ResponseInterface $defaultResponse = null) |
|
210 | |||
211 | /** |
||
212 | * Returns requests that were sent. |
||
213 | * |
||
214 | * @return RequestInterface[] |
||
215 | */ |
||
216 | 1 | public function getRequests() |
|
220 | |||
221 | 2 | public function getLastRequest() |
|
225 | |||
226 | 1 | public function reset() |
|
235 | } |
||
236 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.