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 |
||
13 | class ConversationBus extends AnswerBus |
||
14 | { |
||
15 | /** |
||
16 | * @var Conversation[] |
||
17 | */ |
||
18 | private $conversations; |
||
19 | |||
20 | /** |
||
21 | * ConversationBus constructor. |
||
22 | * |
||
23 | * @param Api $telegram |
||
24 | */ |
||
25 | 68 | public function __construct(Api $telegram) |
|
29 | |||
30 | /** |
||
31 | * @return Conversation[] |
||
32 | */ |
||
33 | public function getConversations() |
||
37 | |||
38 | /** |
||
39 | * @param array $conversations |
||
40 | * @return $this |
||
41 | * @throws TelegramSDKException |
||
42 | */ |
||
43 | public function addConversations(array $conversations) |
||
51 | |||
52 | /** |
||
53 | * @param $conversation |
||
54 | * @return $this |
||
55 | * @throws TelegramSDKException |
||
56 | */ |
||
57 | public function addConversation($conversation) |
||
94 | |||
95 | /** |
||
96 | * @param array $names |
||
97 | * @return $this |
||
98 | */ |
||
99 | public function removeConversations(array $names) |
||
100 | { |
||
101 | foreach ($names as $name) { |
||
102 | $this->removeConversation($name); |
||
103 | } |
||
104 | |||
105 | return $this; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * @param $name |
||
110 | * @return $this |
||
111 | */ |
||
112 | public function removeConversation($name) |
||
113 | { |
||
114 | unset($this->conversations[$name]); |
||
115 | |||
116 | return $this; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Handles Inbound Messages and Executes Appropriate Command. |
||
121 | * |
||
122 | * @param $message |
||
123 | * @param $update |
||
124 | * |
||
125 | * @throws TelegramSDKException |
||
126 | * |
||
127 | * @return Update |
||
128 | */ |
||
129 | protected function handler($message, Update $update) |
||
135 | |||
136 | /** |
||
137 | * Execute the command. |
||
138 | * |
||
139 | * @param $name |
||
140 | * @param $message |
||
141 | * |
||
142 | * @return mixed |
||
143 | */ |
||
144 | protected function execute($name, $update) |
||
152 | } |
||
153 |
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.