Total Complexity | 43 |
Total Lines | 138 |
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 | if (!callback::process()) { |
||
38 | logger::write('not authorized access denied. IP : '. $ip ?? 'unknown',loggerTypes::WARNING); |
||
39 | BPT::exit(); |
||
40 | } |
||
41 | die('callback handler stole the process :('); |
||
|
|||
42 | } |
||
43 | } |
||
44 | } |
||
45 | |||
46 | protected static function processUpdate(string|stdClass|update $update = null): void { |
||
47 | if (!is_object($update)) { |
||
48 | $update = json_decode($update ?? file_get_contents('php://input')); |
||
49 | if (!$update) { |
||
50 | BPT::exit(); |
||
51 | } |
||
52 | } |
||
53 | |||
54 | if (settings::$ignore_updates_older_then > 0) { |
||
55 | if (time() - settings::$ignore_updates_older_then > telegram::catchFields(fields::UPDATE_DATE)) { |
||
56 | logger::write('Update is old, Ignored.'); |
||
57 | return; |
||
58 | } |
||
59 | } |
||
60 | |||
61 | if (settings::$use_types_classes && !is_a($update,'update')) { |
||
62 | $update = new update($update); |
||
63 | } |
||
64 | |||
65 | self::setMessageExtra($update); |
||
66 | BPT::$update = $update; |
||
67 | db::process(); |
||
68 | self::processHandler(); |
||
69 | db::save(); |
||
70 | } |
||
71 | |||
72 | protected static function setMessageExtra (stdClass|update &$update): void { |
||
73 | if (!isset($update->message->text) && !isset($update->edited_message->text)) { |
||
74 | return; |
||
75 | } |
||
76 | $type = isset($update->message) ? 'message' : 'edited_message'; |
||
77 | $text = &$update->{$type}->text; |
||
78 | if (settings::$security) { |
||
79 | $text = tools::clearText($text); |
||
80 | } |
||
81 | if (str_starts_with($text, '/')) { |
||
82 | preg_match('/\/([a-zA-Z_0-9]{1,64})(@[a-zA-Z]\w{1,28}bot)?( [\S]{1,64})?/', $text, $result); |
||
83 | if (isset($result[1])) { |
||
84 | $update->{$type}->command = $result[1]; |
||
85 | } |
||
86 | if (isset($result[2])) { |
||
87 | $update->{$type}->command_username = $result[2]; |
||
88 | } |
||
89 | if (isset($result[3])) { |
||
90 | $update->{$type}->command_payload = trim($result[3]); |
||
91 | } |
||
92 | } |
||
93 | } |
||
94 | |||
95 | private static function processHandler(): void { |
||
149 | } |
||
150 | } |
||
151 | |||
152 | private static function handlerExist(string $handler): bool { |
||
157 | } |
||
158 | } |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.