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 ChatNotification implements ChatNotificationInterface |
||
| 18 | { |
||
| 19 | /** @var Connection */ |
||
| 20 | protected $connection; |
||
| 21 | |||
| 22 | /** @var Translations */ |
||
| 23 | protected $translations; |
||
| 24 | |||
| 25 | /** @var PlayerStorage */ |
||
| 26 | protected $playerStorage; |
||
| 27 | |||
| 28 | /** @var Console */ |
||
| 29 | protected $console; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * ChatNotification constructor. |
||
| 33 | * |
||
| 34 | * @param Connection $connection |
||
| 35 | * @param Translations $translations |
||
| 36 | * @param PlayerStorage $playerStorage |
||
| 37 | */ |
||
| 38 | 8 | public function __construct( |
|
| 49 | |||
| 50 | /** |
||
| 51 | * Send message. |
||
| 52 | * |
||
| 53 | * @param string $messageId |
||
| 54 | * @param string|string[]|null $to |
||
| 55 | * @param string[] $parameters |
||
| 56 | */ |
||
| 57 | 3 | public function sendMessage($messageId, $to = null, $parameters = []) |
|
| 58 | { |
||
| 59 | 3 | $message = $messageId; |
|
| 60 | |||
| 61 | 3 | if (is_string($to)) { |
|
| 62 | 1 | $player = $this->playerStorage->getPlayerInfo($to); |
|
| 63 | 1 | $message = $this->translations->getTranslation($messageId, $parameters, strtolower($player->getLanguage())); |
|
| 64 | 1 | } |
|
| 65 | |||
| 66 | 3 | View Code Duplication | if (is_array($to)) { |
|
|
|||
| 67 | 1 | $to = implode(",", $to); |
|
| 68 | 1 | $message = $this->translations->getTranslations($messageId, $parameters); |
|
| 69 | 1 | } |
|
| 70 | |||
| 71 | 3 | View Code Duplication | if ($to === null) { |
| 72 | 1 | $message = $this->translations->getTranslations($messageId, $parameters); |
|
| 73 | 1 | $this->console->writeln(end($message)['Text']); |
|
| 74 | 1 | } |
|
| 75 | |||
| 76 | try { |
||
| 77 | 3 | $this->connection->chatSendServerMessage($message, $to); |
|
| 78 | 3 | } catch (UnknownPlayerException $e) { |
|
| 79 | // Nothing to do, it happens. |
||
| 80 | } |
||
| 81 | 3 | } |
|
| 82 | |||
| 83 | /** |
||
| 84 | * Return messageId with arguments as a string |
||
| 85 | * Usage: used for retrieving partials for chat messages |
||
| 86 | * * defaults to English locale, without parameters |
||
| 87 | * |
||
| 88 | * @param string $messageId |
||
| 89 | * @param array $parameters |
||
| 90 | * @param string $locale |
||
| 91 | * @return string |
||
| 92 | */ |
||
| 93 | 1 | public function getMessage($messageId, $parameters = [], $locale = "en") |
|
| 97 | |||
| 98 | |||
| 99 | } |
||
| 100 |
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.