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:
Complex classes like DB often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DB, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class DB |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * MySQL credentials |
||
| 30 | * |
||
| 31 | * @var array |
||
| 32 | */ |
||
| 33 | static protected $mysql_credentials = []; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * PDO object |
||
| 37 | * |
||
| 38 | * @var PDO |
||
| 39 | */ |
||
| 40 | static protected $pdo; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Table prefix |
||
| 44 | * |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | static protected $table_prefix; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Telegram class object |
||
| 51 | * |
||
| 52 | * @var \Longman\TelegramBot\Telegram |
||
| 53 | */ |
||
| 54 | static protected $telegram; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Initialize |
||
| 58 | * |
||
| 59 | * @param array $credentials Database connection details |
||
| 60 | * @param \Longman\TelegramBot\Telegram $telegram Telegram object to connect with this object |
||
| 61 | * @param string $table_prefix Table prefix |
||
| 62 | * @param string $encoding Database character encoding |
||
| 63 | * |
||
| 64 | * @return PDO PDO database object |
||
| 65 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 66 | */ |
||
| 67 | 9 | public static function initialize( |
|
| 95 | |||
| 96 | /** |
||
| 97 | * External Initialize |
||
| 98 | * |
||
| 99 | * Let you use the class with an external already existing Pdo Mysql connection. |
||
| 100 | * |
||
| 101 | * @param PDO $external_pdo_connection PDO database object |
||
| 102 | * @param \Longman\TelegramBot\Telegram $telegram Telegram object to connect with this object |
||
| 103 | * @param string $table_prefix Table prefix |
||
| 104 | * |
||
| 105 | * @return PDO PDO database object |
||
| 106 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 107 | */ |
||
| 108 | public static function externalInitialize( |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Define all the tables with the proper prefix |
||
| 129 | */ |
||
| 130 | 9 | protected static function defineTables() |
|
| 151 | |||
| 152 | /** |
||
| 153 | * Check if database connection has been created |
||
| 154 | * |
||
| 155 | * @return bool |
||
| 156 | */ |
||
| 157 | 9 | public static function isDbConnected() |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Get the PDO object of the connected database |
||
| 164 | * |
||
| 165 | * @return \PDO |
||
| 166 | */ |
||
| 167 | public static function getPdo() |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Fetch update(s) from DB |
||
| 174 | * |
||
| 175 | * @param int $limit Limit the number of updates to fetch |
||
| 176 | * @param int $id Check for unique update id |
||
| 177 | * |
||
| 178 | * @return array|bool Fetched data or false if not connected |
||
| 179 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 180 | */ |
||
| 181 | public static function selectTelegramUpdate($limit = null, $id = null) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Fetch message(s) from DB |
||
| 217 | * |
||
| 218 | * @param int $limit Limit the number of messages to fetch |
||
| 219 | * |
||
| 220 | * @return array|bool Fetched data or false if not connected |
||
| 221 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 222 | */ |
||
| 223 | public static function selectMessages($limit = null) |
||
| 224 | { |
||
| 225 | if (!self::isDbConnected()) { |
||
| 226 | return false; |
||
| 227 | } |
||
| 228 | |||
| 229 | try { |
||
| 230 | $sql = ' |
||
| 231 | SELECT * |
||
| 232 | FROM `' . TB_MESSAGE . '` |
||
| 233 | WHERE `update_id` != 0 |
||
| 234 | ORDER BY `message_id` DESC |
||
| 235 | '; |
||
| 236 | |||
| 237 | if ($limit !== null) { |
||
| 238 | $sql .= 'LIMIT :limit'; |
||
| 239 | } |
||
| 240 | |||
| 241 | $sth = self::$pdo->prepare($sql); |
||
| 242 | $sth->bindParam(':limit', $limit, PDO::PARAM_INT); |
||
| 243 | $sth->execute(); |
||
| 244 | |||
| 245 | return $sth->fetchAll(PDO::FETCH_ASSOC); |
||
| 246 | } catch (PDOException $e) { |
||
| 247 | throw new TelegramException($e->getMessage()); |
||
| 248 | } |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Convert from unix timestamp to timestamp |
||
| 253 | * |
||
| 254 | * @param int $time Unix timestamp (if null, current timestamp is used) |
||
| 255 | * |
||
| 256 | * @return string |
||
| 257 | */ |
||
| 258 | 7 | protected static function getTimestamp($time = null) |
|
| 266 | |||
| 267 | /** |
||
| 268 | * Convert array of Entity items to a JSON array |
||
| 269 | * |
||
| 270 | * @todo Find a better way, as json_* functions are very heavy |
||
| 271 | * |
||
| 272 | * @param array|null $entities |
||
| 273 | * @param mixed $default |
||
| 274 | * |
||
| 275 | * @return mixed |
||
| 276 | */ |
||
| 277 | 6 | public static function entitiesArrayToJson($entities, $default = null) |
|
| 278 | { |
||
| 279 | 6 | if (!is_array($entities)) { |
|
| 280 | 6 | return $default; |
|
| 281 | } |
||
| 282 | |||
| 283 | //Convert each Entity item into an object based on its JSON reflection |
||
| 284 | $json_entities = array_map(function ($entity) { |
||
| 285 | return json_decode($entity, true); |
||
| 286 | }, $entities); |
||
| 287 | |||
| 288 | return json_encode($json_entities); |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Insert entry to telegram_update table |
||
| 293 | * |
||
| 294 | * @param int $id |
||
| 295 | * @param int $chat_id |
||
| 296 | * @param int $message_id |
||
| 297 | * @param int $inline_query_id |
||
| 298 | * @param int $chosen_inline_result_id |
||
| 299 | * @param int $callback_query_id |
||
| 300 | * @param int $edited_message_id |
||
| 301 | * |
||
| 302 | * @return bool If the insert was successful |
||
| 303 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 304 | */ |
||
| 305 | public static function insertTelegramUpdate( |
||
| 306 | $id, |
||
| 307 | $chat_id, |
||
| 308 | $message_id, |
||
| 309 | $inline_query_id, |
||
| 310 | $chosen_inline_result_id, |
||
| 311 | $callback_query_id, |
||
| 312 | $edited_message_id |
||
| 313 | ) { |
||
| 314 | if ($message_id === null && $inline_query_id === null && $chosen_inline_result_id === null && $callback_query_id === null && $edited_message_id === null) { |
||
| 315 | throw new TelegramException('message_id, inline_query_id, chosen_inline_result_id, callback_query_id, edited_message_id are all null'); |
||
| 316 | } |
||
| 317 | |||
| 318 | if (!self::isDbConnected()) { |
||
| 319 | return false; |
||
| 320 | } |
||
| 321 | |||
| 322 | try { |
||
| 323 | $sth = self::$pdo->prepare(' |
||
| 324 | INSERT IGNORE INTO `' . TB_TELEGRAM_UPDATE . '` |
||
| 325 | (`id`, `chat_id`, `message_id`, `inline_query_id`, `chosen_inline_result_id`, `callback_query_id`, `edited_message_id`) |
||
| 326 | VALUES |
||
| 327 | (:id, :chat_id, :message_id, :inline_query_id, :chosen_inline_result_id, :callback_query_id, :edited_message_id) |
||
| 328 | '); |
||
| 329 | |||
| 330 | $sth->bindParam(':id', $id, PDO::PARAM_INT); |
||
| 331 | $sth->bindParam(':chat_id', $chat_id, PDO::PARAM_INT); |
||
| 332 | $sth->bindParam(':message_id', $message_id, PDO::PARAM_INT); |
||
| 333 | $sth->bindParam(':inline_query_id', $inline_query_id, PDO::PARAM_INT); |
||
| 334 | $sth->bindParam(':chosen_inline_result_id', $chosen_inline_result_id, PDO::PARAM_INT); |
||
| 335 | $sth->bindParam(':callback_query_id', $callback_query_id, PDO::PARAM_INT); |
||
| 336 | $sth->bindParam(':edited_message_id', $edited_message_id, PDO::PARAM_INT); |
||
| 337 | |||
| 338 | return $sth->execute(); |
||
| 339 | } catch (PDOException $e) { |
||
| 340 | throw new TelegramException($e->getMessage()); |
||
| 341 | } |
||
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Insert users and save their connection to chats |
||
| 346 | * |
||
| 347 | * @param \Longman\TelegramBot\Entities\User $user |
||
| 348 | * @param string $date |
||
| 349 | * @param \Longman\TelegramBot\Entities\Chat $chat |
||
| 350 | * |
||
| 351 | * @return bool If the insert was successful |
||
| 352 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 353 | */ |
||
| 354 | 6 | public static function insertUser(User $user, $date, Chat $chat = null) |
|
| 355 | { |
||
| 356 | 6 | if (!self::isDbConnected()) { |
|
| 357 | return false; |
||
| 358 | } |
||
| 359 | |||
| 360 | 6 | $user_id = $user->getId(); |
|
| 361 | 6 | $username = $user->getUsername(); |
|
| 362 | 6 | $first_name = $user->getFirstName(); |
|
| 363 | 6 | $last_name = $user->getLastName(); |
|
| 364 | |||
| 365 | try { |
||
| 366 | 6 | $sth = self::$pdo->prepare(' |
|
| 367 | 6 | INSERT INTO `' . TB_USER . '` |
|
| 368 | (`id`, `username`, `first_name`, `last_name`, `created_at`, `updated_at`) |
||
| 369 | VALUES |
||
| 370 | (:id, :username, :first_name, :last_name, :date, :date) |
||
| 371 | ON DUPLICATE KEY UPDATE |
||
| 372 | `username` = :username, |
||
| 373 | `first_name` = :first_name, |
||
| 374 | `last_name` = :last_name, |
||
| 375 | `updated_at` = :date |
||
| 376 | '); |
||
| 377 | |||
| 378 | 6 | $sth->bindParam(':id', $user_id, PDO::PARAM_INT); |
|
| 379 | 6 | $sth->bindParam(':username', $username, PDO::PARAM_STR, 255); |
|
| 380 | 6 | $sth->bindParam(':first_name', $first_name, PDO::PARAM_STR, 255); |
|
| 381 | 6 | $sth->bindParam(':last_name', $last_name, PDO::PARAM_STR, 255); |
|
| 382 | 6 | $sth->bindParam(':date', $date, PDO::PARAM_STR); |
|
| 383 | |||
| 384 | 6 | $status = $sth->execute(); |
|
| 385 | } catch (PDOException $e) { |
||
| 386 | throw new TelegramException($e->getMessage()); |
||
| 387 | } |
||
| 388 | |||
| 389 | //insert also the relationship to the chat into user_chat table |
||
| 390 | 6 | if ($chat instanceof Chat) { |
|
| 391 | 6 | $chat_id = $chat->getId(); |
|
| 392 | try { |
||
| 393 | 6 | $sth = self::$pdo->prepare(' |
|
| 394 | 6 | INSERT IGNORE INTO `' . TB_USER_CHAT . '` |
|
| 395 | (`user_id`, `chat_id`) |
||
| 396 | VALUES |
||
| 397 | (:user_id, :chat_id) |
||
| 398 | '); |
||
| 399 | |||
| 400 | 6 | $sth->bindParam(':user_id', $user_id, PDO::PARAM_INT); |
|
| 401 | 6 | $sth->bindParam(':chat_id', $chat_id, PDO::PARAM_INT); |
|
| 402 | |||
| 403 | 6 | $status = $sth->execute(); |
|
| 404 | } catch (PDOException $e) { |
||
| 405 | throw new TelegramException($e->getMessage()); |
||
| 406 | } |
||
| 407 | } |
||
| 408 | |||
| 409 | 6 | return $status; |
|
| 410 | } |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Insert chat |
||
| 414 | * |
||
| 415 | * @param \Longman\TelegramBot\Entities\Chat $chat |
||
| 416 | * @param string $date |
||
| 417 | * @param int $migrate_to_chat_id |
||
| 418 | * |
||
| 419 | * @return bool If the insert was successful |
||
| 420 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 421 | */ |
||
| 422 | 6 | public static function insertChat(Chat $chat, $date, $migrate_to_chat_id = null) |
|
| 423 | { |
||
| 424 | 6 | if (!self::isDbConnected()) { |
|
| 425 | return false; |
||
| 426 | } |
||
| 427 | |||
| 428 | 6 | $chat_id = $chat->getId(); |
|
| 429 | 6 | $chat_title = $chat->getTitle(); |
|
| 430 | 6 | $chat_username = $chat->getUsername(); |
|
| 431 | 6 | $chat_type = $chat->getType(); |
|
| 432 | 6 | $chat_all_members_are_administrators = $chat->getAllMembersAreAdministrators(); |
|
| 433 | |||
| 434 | try { |
||
| 435 | 6 | $sth = self::$pdo->prepare(' |
|
| 436 | 6 | INSERT IGNORE INTO `' . TB_CHAT . '` |
|
| 437 | (`id`, `type`, `title`, `username`, `all_members_are_administrators`, `created_at` ,`updated_at`, `old_id`) |
||
| 438 | VALUES |
||
| 439 | (:id, :type, :title, :username, :all_members_are_administrators, :date, :date, :oldid) |
||
| 440 | ON DUPLICATE KEY UPDATE |
||
| 441 | `type` = :type, |
||
| 442 | `title` = :title, |
||
| 443 | `username` = :username, |
||
| 444 | `all_members_are_administrators` = :all_members_are_administrators, |
||
| 445 | `updated_at` = :date |
||
| 446 | '); |
||
| 447 | |||
| 448 | 6 | if ($migrate_to_chat_id) { |
|
|
|
|||
| 449 | $chat_type = 'supergroup'; |
||
| 450 | |||
| 451 | $sth->bindParam(':id', $migrate_to_chat_id, PDO::PARAM_INT); |
||
| 452 | $sth->bindParam(':oldid', $chat_id, PDO::PARAM_INT); |
||
| 453 | } else { |
||
| 454 | 6 | $sth->bindParam(':id', $chat_id, PDO::PARAM_INT); |
|
| 455 | 6 | $sth->bindParam(':oldid', $migrate_to_chat_id, PDO::PARAM_INT); |
|
| 456 | } |
||
| 457 | |||
| 458 | 6 | $sth->bindParam(':type', $chat_type, PDO::PARAM_INT); |
|
| 459 | 6 | $sth->bindParam(':title', $chat_title, PDO::PARAM_STR, 255); |
|
| 460 | 6 | $sth->bindParam(':username', $chat_username, PDO::PARAM_STR, 255); |
|
| 461 | 6 | $sth->bindParam(':all_members_are_administrators', $chat_all_members_are_administrators, PDO::PARAM_INT); |
|
| 462 | 6 | $sth->bindParam(':date', $date, PDO::PARAM_STR); |
|
| 463 | |||
| 464 | 6 | return $sth->execute(); |
|
| 465 | } catch (PDOException $e) { |
||
| 466 | throw new TelegramException($e->getMessage()); |
||
| 467 | } |
||
| 468 | } |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Insert request into database |
||
| 472 | * |
||
| 473 | * @todo self::$pdo->lastInsertId() - unsafe usage if expected previous insert fails? |
||
| 474 | * |
||
| 475 | * @param \Longman\TelegramBot\Entities\Update $update |
||
| 476 | * |
||
| 477 | * @return bool |
||
| 478 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 479 | */ |
||
| 480 | public static function insertRequest(Update $update) |
||
| 577 | |||
| 578 | /** |
||
| 579 | * Insert inline query request into database |
||
| 580 | * |
||
| 581 | * @param \Longman\TelegramBot\Entities\InlineQuery $inline_query |
||
| 582 | * |
||
| 583 | * @return bool If the insert was successful |
||
| 584 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 585 | */ |
||
| 586 | View Code Duplication | public static function insertInlineQueryRequest(InlineQuery $inline_query) |
|
| 625 | |||
| 626 | /** |
||
| 627 | * Insert chosen inline result request into database |
||
| 628 | * |
||
| 629 | * @param \Longman\TelegramBot\Entities\ChosenInlineResult $chosen_inline_result |
||
| 630 | * |
||
| 631 | * @return bool If the insert was successful |
||
| 632 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 633 | */ |
||
| 634 | View Code Duplication | public static function insertChosenInlineResultRequest(ChosenInlineResult $chosen_inline_result) |
|
| 673 | |||
| 674 | /** |
||
| 675 | * Insert callback query request into database |
||
| 676 | * |
||
| 677 | * @param \Longman\TelegramBot\Entities\CallbackQuery $callback_query |
||
| 678 | * |
||
| 679 | * @return bool If the insert was successful |
||
| 680 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 681 | */ |
||
| 682 | public static function insertCallbackQueryRequest(CallbackQuery $callback_query) |
||
| 743 | |||
| 744 | /** |
||
| 745 | * Insert Message request in db |
||
| 746 | * |
||
| 747 | * @param \Longman\TelegramBot\Entities\Message $message |
||
| 748 | * |
||
| 749 | * @return bool If the insert was successful |
||
| 750 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 751 | */ |
||
| 752 | 6 | public static function insertMessageRequest(Message $message) |
|
| 910 | |||
| 911 | /** |
||
| 912 | * Insert Edited Message request in db |
||
| 913 | * |
||
| 914 | * @param \Longman\TelegramBot\Entities\Message $edited_message |
||
| 915 | * |
||
| 916 | * @return bool If the insert was successful |
||
| 917 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 918 | */ |
||
| 919 | public static function insertEditedMessageRequest(Message $edited_message) |
||
| 974 | |||
| 975 | /** |
||
| 976 | * Select Group and/or single Chats |
||
| 977 | * |
||
| 978 | * @param bool $select_groups |
||
| 979 | * @param bool $select_super_groups |
||
| 980 | * @param bool $select_users |
||
| 981 | * @param string $date_from |
||
| 982 | * @param string $date_to |
||
| 983 | * @param int $chat_id |
||
| 984 | * @param string $text |
||
| 985 | * |
||
| 986 | * @return array|bool (Selected chats or false if invalid arguments) |
||
| 987 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 988 | */ |
||
| 989 | public static function selectChats( |
||
| 1081 | |||
| 1082 | /** |
||
| 1083 | * Get Telegram API request count for current chat / message |
||
| 1084 | * |
||
| 1085 | * @param integer $chat_id |
||
| 1086 | * @param string $inline_message_id |
||
| 1087 | * |
||
| 1088 | * @return array|bool (Array containing TOTAL and CURRENT fields or false on invalid arguments) |
||
| 1089 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 1090 | */ |
||
| 1091 | public static function getTelegramRequestCount($chat_id = null, $inline_message_id = null) |
||
| 1119 | |||
| 1120 | /** |
||
| 1121 | * Insert Telegram API request in db |
||
| 1122 | * |
||
| 1123 | * @param string $method |
||
| 1124 | * @param array $data |
||
| 1125 | * |
||
| 1126 | * @return bool If the insert was successful |
||
| 1127 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 1128 | */ |
||
| 1129 | public static function insertTelegramRequest($method, $data) |
||
| 1160 | } |
||
| 1161 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: