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 |
||
15 | class HttpClient implements HttpClientInterface |
||
16 | { |
||
17 | protected $options = array( |
||
18 | 'base_url' => 'https://api.trello.com/', |
||
19 | 'user_agent' => 'php-trello-api (http://github.com/cdaguerre/php-trello-api)', |
||
20 | 'timeout' => 10, |
||
21 | 'api_version' => 1, |
||
22 | ); |
||
23 | |||
24 | /** |
||
25 | * @var ClientInterface |
||
26 | */ |
||
27 | protected $client; |
||
28 | |||
29 | protected $headers = array(); |
||
30 | |||
31 | private $lastResponse; |
||
32 | private $lastRequest; |
||
33 | |||
34 | /** |
||
35 | * @param array $options |
||
36 | * @param ClientInterface $client |
||
37 | */ |
||
38 | 307 | public function __construct(array $options = array(), ClientInterface $client = null) |
|
47 | |||
48 | /** |
||
49 | * {@inheritDoc} |
||
50 | */ |
||
51 | 1 | public function setOption($name, $value) |
|
55 | |||
56 | /** |
||
57 | * {@inheritDoc} |
||
58 | */ |
||
59 | public function setHeaders(array $headers) |
||
63 | |||
64 | /** |
||
65 | * Clears used headers |
||
66 | */ |
||
67 | 13 | public function clearHeaders() |
|
74 | |||
75 | /** |
||
76 | * @param string $eventName |
||
77 | */ |
||
78 | 13 | public function addListener($eventName, $listener) |
|
82 | |||
83 | public function addSubscriber(EventSubscriberInterface $subscriber) |
||
87 | |||
88 | /** |
||
89 | * {@inheritDoc} |
||
90 | */ |
||
91 | 2 | public function get($path, array $parameters = array(), array $headers = array()) |
|
95 | |||
96 | /** |
||
97 | * {@inheritDoc} |
||
98 | */ |
||
99 | View Code Duplication | public function post($path, $body = null, array $headers = array()) |
|
107 | |||
108 | /** |
||
109 | * {@inheritDoc} |
||
110 | */ |
||
111 | 1 | View Code Duplication | public function patch($path, $body = null, array $headers = array()) |
119 | |||
120 | /** |
||
121 | * {@inheritDoc} |
||
122 | */ |
||
123 | 1 | public function delete($path, $body = null, array $headers = array()) |
|
127 | |||
128 | /** |
||
129 | * {@inheritDoc} |
||
130 | */ |
||
131 | 1 | View Code Duplication | public function put($path, $body, array $headers = array()) |
139 | |||
140 | /** |
||
141 | * {@inheritDoc} |
||
142 | */ |
||
143 | 5 | public function request($path, $body = null, $httpMethod = 'GET', array $headers = array(), array $options = array()) |
|
160 | |||
161 | /** |
||
162 | * {@inheritDoc} |
||
163 | */ |
||
164 | 4 | public function authenticate($tokenOrLogin, $password = null, $method) |
|
170 | |||
171 | /** |
||
172 | * @return Request |
||
173 | */ |
||
174 | public function getLastRequest() |
||
178 | |||
179 | /** |
||
180 | * @return Response |
||
181 | */ |
||
182 | public function getLastResponse() |
||
186 | |||
187 | /** |
||
188 | * @param string $httpMethod |
||
189 | * @param string $path |
||
190 | */ |
||
191 | 5 | protected function createRequest($httpMethod, $path, $body = null, array $headers = array(), array $options = array()) |
|
208 | } |
||
209 |
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.