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 |
||
| 21 | class ChatNotification implements ChatNotificationInterface |
||
| 22 | { |
||
| 23 | /** @var Factory */ |
||
| 24 | protected $factory; |
||
| 25 | |||
| 26 | /** @var Translations */ |
||
| 27 | protected $translations; |
||
| 28 | |||
| 29 | /** @var PlayerStorage */ |
||
| 30 | protected $playerStorage; |
||
| 31 | |||
| 32 | /** @var Console */ |
||
| 33 | protected $console; |
||
| 34 | /** |
||
| 35 | * @var LoggerInterface |
||
| 36 | */ |
||
| 37 | private $logger; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * ChatNotification constructor. |
||
| 41 | * |
||
| 42 | * @param Factory $connectionFactory |
||
| 43 | * @param Translations $translations |
||
| 44 | * @param PlayerStorage $playerStorage |
||
| 45 | * @param Console $console |
||
| 46 | * @param LoggerInterface $logger |
||
| 47 | */ |
||
| 48 | 11 | View Code Duplication | public function __construct( |
|
|
|||
| 49 | Factory $connectionFactory, |
||
| 50 | Translations $translations, |
||
| 51 | PlayerStorage $playerStorage, |
||
| 52 | Console $console, |
||
| 53 | LoggerInterface $logger |
||
| 54 | ) { |
||
| 55 | 11 | $this->factory = $connectionFactory; |
|
| 56 | 11 | $this->translations = $translations; |
|
| 57 | 11 | $this->playerStorage = $playerStorage; |
|
| 58 | 11 | $this->console = $console; |
|
| 59 | 11 | $this->logger = $logger; |
|
| 60 | 11 | } |
|
| 61 | |||
| 62 | /** |
||
| 63 | * Send message. |
||
| 64 | * |
||
| 65 | * @param string $messageId |
||
| 66 | * @param string|string[]|Group|null $to |
||
| 67 | * @param string[] $parameters |
||
| 68 | */ |
||
| 69 | 6 | public function sendMessage($messageId, $to = null, $parameters = []) |
|
| 70 | { |
||
| 71 | 6 | $message = $messageId; |
|
| 72 | |||
| 73 | 6 | if (is_string($to)) { |
|
| 74 | 2 | $player = $this->playerStorage->getPlayerInfo($to); |
|
| 75 | 2 | $message = $this->translations->getTranslation($messageId, $parameters, strtolower($player->getLanguage())); |
|
| 76 | } |
||
| 77 | |||
| 78 | 6 | if (is_array($to)) { |
|
| 79 | 2 | $to = implode(",", $to); |
|
| 80 | 2 | $message = $this->translations->getTranslations($messageId, $parameters); |
|
| 81 | } |
||
| 82 | |||
| 83 | 6 | if ($to instanceof Group) { |
|
| 84 | $to = implode(",", $to->getLogins()); |
||
| 85 | $message = $this->translations->getTranslations($messageId, $parameters); |
||
| 86 | } |
||
| 87 | |||
| 88 | 6 | if ($to === null || $to instanceof Group) { |
|
| 89 | 2 | $message = $this->translations->getTranslations($messageId, $parameters); |
|
| 90 | 2 | $this->console->writeln(end($message)['Text']); |
|
| 91 | } |
||
| 92 | |||
| 93 | try { |
||
| 94 | 6 | $this->factory->getConnection()->chatSendServerMessage($message, $to); |
|
| 95 | } catch (UnknownPlayerException $e) { |
||
| 96 | $this->logger->info("can't send chat message: $message", ["to" => $to, "exception" => $e]); |
||
| 97 | // Nothing to do, it happens. |
||
| 98 | } catch (InvalidArgumentException $ex) { |
||
| 99 | // Nothing to do |
||
| 100 | } |
||
| 101 | 6 | } |
|
| 102 | |||
| 103 | /** |
||
| 104 | * Return messageId with arguments as a string |
||
| 105 | * Usage: used for retrieving partials for chat messages |
||
| 106 | * * defaults to English locale, without parameters |
||
| 107 | * |
||
| 108 | * @param string $messageId |
||
| 109 | * @param array $parameters |
||
| 110 | * @param string $locale |
||
| 111 | * @return string |
||
| 112 | */ |
||
| 113 | public function getMessage($messageId, $parameters = [], $locale = "en") |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Return messageId with arguments as a string |
||
| 120 | * Usage: used for retrieving partials for chat messages |
||
| 121 | * * defaults to English locale, without parameters |
||
| 122 | * |
||
| 123 | * @param string $messageId |
||
| 124 | * @param array $parameters |
||
| 125 | * |
||
| 126 | * @return string[] |
||
| 127 | */ |
||
| 128 | public function getMessages($messageId, $parameters = []) |
||
| 132 | |||
| 133 | } |
||
| 134 |
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.