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 |
||
| 16 | class CustomChat implements ListenerInterfaceExpApplication, ListenerInterfaceMpLegacyChat |
||
| 17 | { |
||
| 18 | /** @var Connection */ |
||
| 19 | protected $connection; |
||
| 20 | |||
| 21 | /** @var Console */ |
||
| 22 | protected $console; |
||
| 23 | |||
| 24 | /** @var AdminGroups */ |
||
| 25 | protected $adminGroups; |
||
| 26 | |||
| 27 | /** @var bool */ |
||
| 28 | protected $enabled = true; |
||
| 29 | /** |
||
| 30 | * @var ChatNotification |
||
| 31 | */ |
||
| 32 | private $chatNotification; |
||
| 33 | /** |
||
| 34 | * @var PlayerStorage |
||
| 35 | */ |
||
| 36 | private $playerStorage; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * CustomChat constructor. |
||
| 40 | * @param Connection $connection |
||
| 41 | * @param Console $console |
||
| 42 | * @param AdminGroups $adminGroups |
||
| 43 | * @param ChatNotification $chatNotification |
||
| 44 | * @param PlayerStorage $playerStorage |
||
| 45 | */ |
||
| 46 | View Code Duplication | function __construct( |
|
| 59 | |||
| 60 | /** |
||
| 61 | * Called when a player chats. |
||
| 62 | * |
||
| 63 | * @param Player $player |
||
| 64 | * @param $text |
||
| 65 | * |
||
| 66 | * @return void |
||
| 67 | */ |
||
| 68 | public function onPlayerChat(Player $player, $text) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @param Player $player |
||
| 132 | * @param $text |
||
| 133 | * @param $color |
||
| 134 | * @param null $group |
||
| 135 | */ |
||
| 136 | private function sendChat(Player $player, $text, $color, $group = null) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Set the status of the plugin |
||
| 162 | * |
||
| 163 | * @param boolean $status |
||
| 164 | * |
||
| 165 | * @return void |
||
| 166 | */ |
||
| 167 | public function setStatus($status) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * called at eXpansion init |
||
| 174 | * |
||
| 175 | * @return void |
||
| 176 | */ |
||
| 177 | public function onApplicationInit() |
||
| 181 | |||
| 182 | /** |
||
| 183 | * called when init is done and callbacks are enabled |
||
| 184 | * |
||
| 185 | * @return void |
||
| 186 | */ |
||
| 187 | public function onApplicationReady() |
||
| 195 | |||
| 196 | /** |
||
| 197 | * called when requesting application stop |
||
| 198 | * |
||
| 199 | * @return void |
||
| 200 | */ |
||
| 201 | public function onApplicationStop() |
||
| 205 | } |
||
| 206 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.