| Total Complexity | 43 |
| Total Lines | 133 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 0 |
Complex classes like receiver 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.
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 receiver, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class receiver { |
||
| 20 | private static array $handlers = [ |
||
| 21 | 'message' => null, |
||
| 22 | 'edited_message' => null, |
||
| 23 | 'channel_post' => null, |
||
| 24 | 'edited_channel_post' => null, |
||
| 25 | 'inline_query' => null, |
||
| 26 | 'callback_query' => null, |
||
| 27 | 'my_chat_member' => null, |
||
| 28 | 'chat_member' => null, |
||
| 29 | 'chat_join_request' => null, |
||
| 30 | 'something_else' => null |
||
| 31 | ]; |
||
| 32 | |||
| 33 | protected static function telegramVerify(string $ip = null): void { |
||
| 34 | if (settings::$telegram_verify) { |
||
| 35 | $ip = $ip ?? tools::remoteIP(); |
||
| 36 | if (!tools::isTelegram($ip)) { |
||
| 37 | logger::write('not authorized access denied. IP : '. $ip ?? 'unknown',loggerTypes::WARNING); |
||
| 38 | BPT::exit(); |
||
| 39 | } |
||
| 40 | } |
||
| 41 | } |
||
| 42 | |||
| 43 | protected static function processUpdate(string|stdClass|update $update = null): void { |
||
| 44 | if (!is_object($update)) { |
||
| 45 | $update = json_decode($update ?? file_get_contents("php://input")); |
||
| 46 | if (!$update) { |
||
| 47 | BPT::exit(); |
||
| 48 | } |
||
| 49 | } |
||
| 50 | |||
| 51 | if (settings::$ignore_updates_older_then > 0) { |
||
| 52 | if (time() - settings::$ignore_updates_older_then > telegram::catchFields(fields::UPDATE_DATE)) { |
||
| 53 | logger::write('Update is old, Ignored.'); |
||
| 54 | return; |
||
| 55 | } |
||
| 56 | } |
||
| 57 | |||
| 58 | if (!is_a($update,'update')) { |
||
| 59 | $update = new update($update); |
||
|
|
|||
| 60 | } |
||
| 61 | |||
| 62 | self::setMessageExtra($update); |
||
| 63 | BPT::$update = $update; |
||
| 64 | db::process(); |
||
| 65 | self::processHandler(); |
||
| 66 | db::save(); |
||
| 67 | } |
||
| 68 | |||
| 69 | protected static function setMessageExtra (update &$update): void { |
||
| 70 | if ((isset($update->message) && isset($update->message->text)) || (isset($update->edited_message) && isset($update->edited_message->text))) { |
||
| 71 | $type = isset($update->message) ? 'message' : 'edited_message'; |
||
| 72 | $text = &$update->$type->text; |
||
| 73 | if (settings::$security) { |
||
| 74 | $text = tools::clearText($text); |
||
| 75 | } |
||
| 76 | if (str_starts_with($text, '/')) { |
||
| 77 | preg_match('/\/([a-zA-Z_0-9]{1,64})(@[a-zA-Z]\w{1,28}bot)?( [\S]{1,64})?/', $text, $result); |
||
| 78 | if (isset($result[1])) { |
||
| 79 | $update->$type->command = $result[1]; |
||
| 80 | } |
||
| 81 | if (isset($result[2])) { |
||
| 82 | $update->$type->command_username = $result[2]; |
||
| 83 | } |
||
| 84 | if (isset($result[3])) { |
||
| 85 | $update->$type->command_payload = trim($result[3]); |
||
| 86 | } |
||
| 87 | } |
||
| 88 | } |
||
| 89 | } |
||
| 90 | |||
| 91 | private static function processHandler(): void { |
||
| 143 | } |
||
| 144 | } |
||
| 145 | } |
||
| 146 | |||
| 147 | private static function handlerExist(string $handler): bool { |
||
| 152 | } |
||
| 153 | } |