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 |
||
18 | class Client |
||
19 | { |
||
20 | use ConfigTrait; |
||
21 | use LogTrait; |
||
22 | |||
23 | /** @var RealTimeClient */ |
||
24 | private $realTimeClient; |
||
25 | |||
26 | /** @var Users */ |
||
27 | private $users; |
||
28 | |||
29 | /** @var Bots */ |
||
30 | private $bots; |
||
31 | |||
32 | /** @var Channels */ |
||
33 | private $channels; |
||
34 | |||
35 | /** @var User */ |
||
36 | protected $authedUser; |
||
37 | |||
38 | public function __construct( |
||
53 | |||
54 | public function init() : Client |
||
61 | |||
62 | public function update(Closure $authedUserUpdated) |
||
69 | |||
70 | public function onEvent($event, array $onEvent) |
||
76 | |||
77 | public function getRealTimeClient() |
||
81 | |||
82 | public function getAuthedUser() |
||
86 | |||
87 | public function getAuthedUsername() |
||
91 | |||
92 | public function connect() : PromiseInterface |
||
96 | |||
97 | public function disconnect() |
||
101 | |||
102 | public function reconnect() : PromiseInterface |
||
107 | |||
108 | public function ping() : PromiseInterface |
||
112 | |||
113 | public function say($text, $channelOrName, array $additionalParameters = []) |
||
136 | |||
137 | public function send($text, ChannelInterface $channel) |
||
141 | |||
142 | View Code Duplication | public function post($text, ChannelInterface $channel, array $additionalParameters = []) |
|
152 | |||
153 | View Code Duplication | public function directMessage($text, $userName) |
|
163 | |||
164 | |||
165 | public function getUsersMap() : array |
||
169 | |||
170 | public function getBotsMap() : array |
||
174 | |||
175 | public function getChannelsMap() : array |
||
179 | |||
180 | /** |
||
181 | * @param $id |
||
182 | * @return null|User |
||
183 | */ |
||
184 | public function getUserById($id) |
||
188 | |||
189 | /** |
||
190 | * @param $name |
||
191 | * @return null|User |
||
192 | */ |
||
193 | public function getUserByName($name) |
||
197 | |||
198 | /** |
||
199 | * @param $id |
||
200 | * @return null|Bot |
||
201 | */ |
||
202 | public function getBotById($id) |
||
206 | |||
207 | /** |
||
208 | * @param $name |
||
209 | * @return null|Bot |
||
210 | */ |
||
211 | public function getBotByName($name) |
||
215 | |||
216 | /** |
||
217 | * @param $id |
||
218 | * @return null|Channel |
||
219 | */ |
||
220 | public function getChannelById($id) |
||
224 | |||
225 | /** |
||
226 | * @param $name |
||
227 | * @return null|Channel |
||
228 | */ |
||
229 | public function getChannelByName($name) |
||
233 | |||
234 | public function updateUsers() |
||
240 | |||
241 | public function updateBots() |
||
247 | |||
248 | public function updateChannels() |
||
254 | |||
255 | protected function updateAuthedUser(Closure $authedUserUpdated) |
||
262 | |||
263 | protected function initChannelUpdateHandlers() |
||
270 | |||
271 | protected function initUserUpdateHandlers() |
||
278 | |||
279 | protected function useWebSocket() : bool |
||
283 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.