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 |
||
17 | class Client |
||
18 | { |
||
19 | use ConfigTrait; |
||
20 | use LogTrait; |
||
21 | |||
22 | /** @var RealTimeClient */ |
||
23 | private $realTimeClient; |
||
24 | |||
25 | /** @var Users */ |
||
26 | private $users; |
||
27 | |||
28 | /** @var Channels */ |
||
29 | private $channels; |
||
30 | |||
31 | /** @var User */ |
||
32 | protected $authedUser; |
||
33 | |||
34 | public function __construct( |
||
47 | |||
48 | public function init() : Client |
||
55 | |||
56 | public function update(Closure $authedUserUpdated) |
||
62 | |||
63 | public function onEvent($event, array $onEvent) |
||
69 | |||
70 | public function getRealTimeClient() |
||
74 | |||
75 | public function getAuthedUser() |
||
79 | |||
80 | public function getAuthedUsername() |
||
84 | |||
85 | public function connect() : PromiseInterface |
||
89 | |||
90 | public function disconnect() |
||
94 | |||
95 | public function say($text, $channelOrName, array $additionalParameters = []) |
||
118 | |||
119 | public function send($text, ChannelInterface $channel) |
||
123 | |||
124 | View Code Duplication | public function post($text, ChannelInterface $channel, array $additionalParameters = []) |
|
134 | |||
135 | View Code Duplication | public function directMessage($text, $userName) |
|
145 | |||
146 | /** |
||
147 | * @param $id |
||
148 | * @return null|User |
||
149 | */ |
||
150 | public function getUserById($id) |
||
154 | |||
155 | /** |
||
156 | * @param $name |
||
157 | * @return null|User |
||
158 | */ |
||
159 | public function getUserByName($name) |
||
163 | |||
164 | /** |
||
165 | * @param $id |
||
166 | * @return null|Channel |
||
167 | */ |
||
168 | public function getChannelById($id) |
||
172 | |||
173 | /** |
||
174 | * @param $name |
||
175 | * @return null|Channel |
||
176 | */ |
||
177 | public function getChannelByName($name) |
||
181 | |||
182 | protected function useWebSocket() : bool |
||
186 | |||
187 | protected function updateUsers() |
||
193 | |||
194 | protected function updateChannels() |
||
200 | |||
201 | protected function updateAuthedUser(Closure $authedUserUpdated) |
||
208 | |||
209 | protected function initChannelUpdateHandlers() |
||
216 | |||
217 | protected function initUserUpdateHandlers() |
||
224 | } |
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.