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 |
||
27 | class Response |
||
28 | { |
||
29 | /** |
||
30 | * ServerResponse constructor. |
||
31 | * |
||
32 | * @param array $data |
||
33 | * @param string $bot_username |
||
34 | * |
||
35 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
36 | */ |
||
37 | 10 | public function __construct(array $data, $bot_username = '') |
|
67 | |||
68 | /** |
||
69 | * Helper to set member variables |
||
70 | * |
||
71 | * @param array $data |
||
72 | */ |
||
73 | 10 | protected function assignMemberVariables(array $data) |
|
79 | |||
80 | /** |
||
81 | * Check if array is associative |
||
82 | * |
||
83 | * @link https://stackoverflow.com/a/4254008 |
||
84 | * |
||
85 | * @param array $array |
||
86 | * |
||
87 | * @return bool |
||
88 | */ |
||
89 | 6 | protected function isAssoc(array $array) |
|
93 | |||
94 | /** |
||
95 | * If response is ok |
||
96 | * |
||
97 | * @return bool |
||
98 | */ |
||
99 | 6 | public function isOk() |
|
103 | |||
104 | /** |
||
105 | * If response is ok |
||
106 | * |
||
107 | * @return bool |
||
108 | */ |
||
109 | 6 | public function getOk() |
|
113 | |||
114 | /** |
||
115 | * Return result |
||
116 | * |
||
117 | * @return mixed |
||
118 | */ |
||
119 | 9 | public function getResult() |
|
123 | |||
124 | /** |
||
125 | * Return error code |
||
126 | * |
||
127 | * @return int |
||
128 | */ |
||
129 | 5 | public function getErrorCode() |
|
133 | |||
134 | /** |
||
135 | * Return human-readable description of the result / unsuccessful request |
||
136 | * |
||
137 | * @return string |
||
138 | */ |
||
139 | 5 | public function getDescription() |
|
143 | |||
144 | /** |
||
145 | * Print error |
||
146 | * |
||
147 | * @see https://secure.php.net/manual/en/function.print-r.php |
||
148 | * |
||
149 | * @param bool $return |
||
150 | * |
||
151 | * @return bool|string |
||
152 | */ |
||
153 | public function printError($return = false) |
||
165 | |||
166 | /** |
||
167 | * Create and return the object of the received result |
||
168 | * |
||
169 | * @param array $result |
||
170 | * @param string $bot_username |
||
171 | * |
||
172 | * @return \Longman\TelegramBot\Entities\Chat|\Longman\TelegramBot\Entities\ChatMember|\Longman\TelegramBot\Entities\File|\Longman\TelegramBot\Entities\Message|\Longman\TelegramBot\Entities\User|\Longman\TelegramBot\Entities\UserProfilePhotos|\Longman\TelegramBot\Entities\WebhookInfo |
||
173 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
174 | */ |
||
175 | 4 | private function createResultObject($result, $bot_username) |
|
197 | |||
198 | /** |
||
199 | * Create and return the objects array of the received result |
||
200 | * |
||
201 | * @param array $result |
||
202 | * @param string $bot_username |
||
203 | * |
||
204 | * @return null|\Longman\TelegramBot\Entities\ChatMember[]|\Longman\TelegramBot\Entities\Update[] |
||
205 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
206 | */ |
||
207 | 2 | private function createResultObjects($result, $bot_username) |
|
230 | |||
231 | /** |
||
232 | * Return an empty Server Response |
||
233 | * |
||
234 | * No request to telegram are sent, this function is used in commands that |
||
235 | * don't need to fire a message after execution |
||
236 | * |
||
237 | * @return \Longman\TelegramBot\Http\Response |
||
238 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
239 | */ |
||
240 | public static function createEmpty() |
||
244 | } |
||
245 |
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.