Total Complexity | 41 |
Total Lines | 126 |
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 |
||
17 | class receiver { |
||
18 | private static array $handlers = [ |
||
19 | 'message' => null, |
||
20 | 'edited_message' => null, |
||
21 | 'channel_post' => null, |
||
22 | 'edited_channel_post' => null, |
||
23 | 'inline_query' => null, |
||
24 | 'callback_query' => null, |
||
25 | 'my_chat_member' => null, |
||
26 | 'chat_member' => null, |
||
27 | 'chat_join_request' => null, |
||
28 | 'something_else' => null |
||
29 | ]; |
||
30 | |||
31 | protected static function telegramVerify(string $ip = null): void { |
||
32 | if (settings::$telegram_verify) { |
||
33 | $ip = $ip ?? tools::remoteIP(); |
||
34 | if (!tools::isTelegram($ip)) { |
||
35 | logger::write('not authorized access denied. IP : '. $ip ?? 'unknown',loggerTypes::WARNING); |
||
36 | BPT::exit(); |
||
37 | } |
||
38 | } |
||
39 | } |
||
40 | |||
41 | protected static function processUpdate(string|stdClass|update $update = null): void { |
||
42 | if (!is_object($update)) { |
||
43 | $update = json_decode($update ?? file_get_contents("php://input")); |
||
44 | if (!$update) { |
||
45 | BPT::exit(); |
||
46 | } |
||
47 | } |
||
48 | |||
49 | if (!is_a($update,'update')) { |
||
50 | $update = new update($update); |
||
|
|||
51 | } |
||
52 | |||
53 | self::setMessageExtra($update); |
||
54 | BPT::$update = $update; |
||
55 | db::process(); |
||
56 | self::processHandler(); |
||
57 | db::save(); |
||
58 | } |
||
59 | |||
60 | protected static function setMessageExtra (update &$update): void { |
||
61 | if ((isset($update->message) && isset($update->message->text)) || (isset($update->edited_message) && isset($update->edited_message->text))) { |
||
62 | $type = isset($update->message) ? 'message' : 'edited_message'; |
||
63 | $text = &$update->$type->text; |
||
64 | if (settings::$security) { |
||
65 | $text = tools::clearText($text); |
||
66 | } |
||
67 | if (str_starts_with($text, '/')) { |
||
68 | preg_match('/\/([a-zA-Z_0-9]{1,64})(@[a-zA-Z]\w{1,28}bot)?( [\S]{1,64})?/', $text, $result); |
||
69 | if (isset($result[1])) { |
||
70 | $update->$type->commend = $result[1]; |
||
71 | } |
||
72 | if (isset($result[2])) { |
||
73 | $update->$type->commend_username = $result[2]; |
||
74 | } |
||
75 | if (isset($result[3])) { |
||
76 | $update->$type->commend_payload = $result[3]; |
||
77 | } |
||
78 | } |
||
79 | } |
||
80 | } |
||
81 | |||
82 | private static function processHandler(): void { |
||
134 | } |
||
135 | } |
||
136 | } |
||
137 | |||
138 | private static function handlerExist(string $handler): bool { |
||
143 | } |
||
144 | } |