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 ClientInterface |
||
26 | { |
||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $token; |
||
31 | |||
32 | /** |
||
33 | * @var \React\EventLoop\ExtEventLoop|\React\EventLoop\LibEventLoop|\React\EventLoop\LibEvLoop|\React\EventLoop\StreamSelectLoop |
||
34 | */ |
||
35 | protected $loop; |
||
36 | |||
37 | /** |
||
38 | * @var \Slack\RealTimeClient |
||
39 | */ |
||
40 | protected $client; |
||
41 | |||
42 | /** |
||
43 | * @var TextParser |
||
44 | */ |
||
45 | protected $parser; |
||
46 | |||
47 | /** |
||
48 | * Client constructor. |
||
49 | * |
||
50 | * @param string $token |
||
51 | */ |
||
52 | public function __construct($token) |
||
61 | |||
62 | /** |
||
63 | * @param RoomInterface $room |
||
64 | * @param string $message |
||
65 | * |
||
66 | * @return void |
||
67 | */ |
||
68 | public function sendMessage(RoomInterface $room, $message) |
||
78 | |||
79 | /** |
||
80 | * @param RoomInterface $room |
||
81 | * |
||
82 | * @return void |
||
83 | */ |
||
84 | public function listen(RoomInterface $room) |
||
124 | |||
125 | |||
126 | /** |
||
127 | * @param Storage $middleware |
||
128 | * @param Message $message |
||
129 | */ |
||
130 | public function onMessage(Storage $middleware, Message $message) |
||
138 | |||
139 | protected function onClose() |
||
143 | |||
144 | /** |
||
145 | * @param \Exception $e |
||
146 | */ |
||
147 | protected function onError(\Exception $e) |
||
151 | /** |
||
152 | * @return string |
||
153 | */ |
||
154 | public function version() |
||
158 | |||
159 | /** |
||
160 | * @return ClientInterface |
||
161 | */ |
||
162 | public function run(): ClientInterface |
||
168 | |||
169 | /** |
||
170 | * @param \Exception $e |
||
171 | */ |
||
172 | View Code Duplication | protected function logException(\Exception $e) |
|
180 | |||
181 | /** |
||
182 | * @param string $id |
||
183 | * |
||
184 | * @return User |
||
185 | */ |
||
186 | public function getUserById($id) |
||
198 | } |
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.