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 |
||
| 10 | class JoinLeaveMessages implements PlayerDataListenerInterface |
||
| 11 | { |
||
| 12 | /** @var Connection */ |
||
| 13 | protected $connection; |
||
| 14 | /** @var Console */ |
||
| 15 | protected $console; |
||
| 16 | /** @var bool */ |
||
| 17 | private $enabled = true; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * JoinLeaveMessages constructor. |
||
| 21 | * |
||
| 22 | * @param Connection $connection |
||
| 23 | * @param Console $console |
||
| 24 | */ |
||
| 25 | function __construct(Connection $connection, Console $console) |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @inheritdoc |
||
| 33 | */ |
||
| 34 | public function onRun() |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @inheritdoc |
||
| 42 | */ |
||
| 43 | View Code Duplication | public function onPlayerConnect(Player $player) |
|
| 50 | |||
| 51 | /** |
||
| 52 | * @inheritdoc |
||
| 53 | */ |
||
| 54 | View Code Duplication | public function onPlayerDisconnect(Player $player, $disconnectionReason) |
|
| 61 | |||
| 62 | /** |
||
| 63 | * @inheritdoc |
||
| 64 | */ |
||
| 65 | public function onPlayerInfoChanged(Player $oldPlayer, Player $player) |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @inheritdoc |
||
| 71 | */ |
||
| 72 | public function onPlayerAlliesChanged(Player $oldPlayer, Player $player) |
||
| 75 | } |
||
| 76 |
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.