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 GroupsPlugin implements ListenerInterfaceMpLegacyPlayer, StatusAwarePluginInterface |
||
| 19 | { |
||
| 20 | /** @var AdminGroupConfiguration */ |
||
| 21 | protected $adminGroupConfiguration; |
||
| 22 | |||
| 23 | /** @var Factory */ |
||
| 24 | protected $userGroupFactory; |
||
| 25 | |||
| 26 | /** @var PlayerStorage */ |
||
| 27 | protected $playerStorage; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * GroupsPlugin constructor. |
||
| 31 | * |
||
| 32 | * @param AdminGroupConfiguration $adminGroupConfiguration |
||
| 33 | * @param Factory $userGroupFactory |
||
| 34 | */ |
||
| 35 | 2 | public function __construct( |
|
| 36 | AdminGroupConfiguration $adminGroupConfiguration, |
||
| 37 | Factory $userGroupFactory, |
||
| 38 | PlayerStorage $playerStorage |
||
| 39 | ) { |
||
| 40 | 2 | $this->adminGroupConfiguration = $adminGroupConfiguration; |
|
| 41 | 2 | $this->userGroupFactory = $userGroupFactory; |
|
| 42 | 2 | $this->playerStorage = $playerStorage; |
|
| 43 | |||
| 44 | // Create a user group for each admin group & for guest players. |
||
| 45 | 2 | foreach ($this->adminGroupConfiguration->getGroups() as $groupName) { |
|
| 46 | 2 | $this->userGroupFactory->create("admin:$groupName"); |
|
| 47 | 2 | } |
|
| 48 | 2 | $this->userGroupFactory->create('admin:guest'); |
|
| 49 | 2 | } |
|
| 50 | |||
| 51 | /** |
||
| 52 | * @inheritdoc |
||
| 53 | */ |
||
| 54 | View Code Duplication | public function setStatus($status) |
|
|
|
|||
| 55 | { |
||
| 56 | if ($status && !empty($this->playerStorage->getOnline())) { |
||
| 57 | foreach ($this->playerStorage->getOnline() as $player) { |
||
| 58 | $this->onPlayerConnect($player); |
||
| 59 | } |
||
| 60 | } |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @inheritdoc |
||
| 65 | */ |
||
| 66 | 1 | public function onPlayerConnect(Player $player) |
|
| 67 | { |
||
| 68 | 1 | $groupName = $this->adminGroupConfiguration->getLoginGroupName($player->getLogin()); |
|
| 69 | |||
| 70 | 1 | if ($groupName === null) { |
|
| 71 | 1 | $this->userGroupFactory->getGroup('admin:guest')->addLogin($player->getLogin()); |
|
| 72 | 1 | } else { |
|
| 73 | 1 | $this->userGroupFactory->getGroup("admin:$groupName")->addLogin($player->getLogin()); |
|
| 74 | } |
||
| 75 | 1 | } |
|
| 76 | |||
| 77 | /** |
||
| 78 | * @inheritdoc |
||
| 79 | */ |
||
| 80 | 1 | public function onPlayerDisconnect(Player $player, $disconnectionReason) |
|
| 84 | |||
| 85 | /** |
||
| 86 | * @inheritdoc |
||
| 87 | */ |
||
| 88 | 1 | public function onPlayerInfoChanged(Player $oldPlayer, Player $player) |
|
| 92 | |||
| 93 | /** |
||
| 94 | * @inheritdoc |
||
| 95 | */ |
||
| 96 | 1 | public function onPlayerAlliesChanged(Player $oldPlayer, Player $player) |
|
| 100 | } |
||
| 101 |
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.