| Total Complexity | 50 |
| Total Lines | 413 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Update 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 Update, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class Update implements \JsonSerializable |
||
| 20 | { |
||
| 21 | |||
| 22 | /** |
||
| 23 | * The update's unique identifier. Update identifiers start from a certain positive number and increase sequentially. |
||
| 24 | * This ID becomes especially handy if you're using Webhooks, since it allows you to ignore repeated updates or to |
||
| 25 | * restore the correct update sequence, should they get out of order. If there are no new updates for at least a |
||
| 26 | * week, then identifier of the next update will be chosen randomly instead of sequentially. |
||
| 27 | * |
||
| 28 | * @var int |
||
| 29 | */ |
||
| 30 | private $update_id; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Optional. New incoming message of any kind -- text, photo, sticker, etc. |
||
| 34 | * |
||
| 35 | * @var Message|null |
||
| 36 | */ |
||
| 37 | private $message; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Optional. New version of a message that is known to the bot and was edited |
||
| 41 | * |
||
| 42 | * @var EditedMessage|null |
||
| 43 | */ |
||
| 44 | private $edited_message; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Optional. New incoming channel post of any kind -- text, photo, sticker, etc. |
||
| 48 | * |
||
| 49 | * @var ChannelPost|null |
||
| 50 | */ |
||
| 51 | private $channel_post; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Optional. New version of a channel post that is known to the bot and was edited |
||
| 55 | * |
||
| 56 | * @var EditedChannelPost|null |
||
| 57 | */ |
||
| 58 | private $edited_channel_post; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Optional. New incoming inline query |
||
| 62 | * |
||
| 63 | * @var InlineQuery|null |
||
| 64 | */ |
||
| 65 | private $inline_query; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Optional. The result of an inline query that was chosen by a user and sent to their chat partner. Please see our |
||
| 69 | * documentation on the feedback collecting for details on how to enable these updates for your bot. |
||
| 70 | * |
||
| 71 | * @var ChosenInlineResult|null |
||
| 72 | */ |
||
| 73 | private $chosen_inline_result; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Optional. New incoming callback query |
||
| 77 | * |
||
| 78 | * @var CallbackQuery|null |
||
| 79 | */ |
||
| 80 | private $callback_query; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Optional. New incoming shipping query. Only for invoices with flexible price |
||
| 84 | * |
||
| 85 | * @var ShippingQuery|null |
||
| 86 | */ |
||
| 87 | private $shipping_query; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Optional. New incoming pre-checkout query. Contains full information about checkout |
||
| 91 | * |
||
| 92 | * @var PreCheckoutQuery|null |
||
| 93 | */ |
||
| 94 | private $pre_checkout_query; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Optional. New poll state. Bots receive only updates about stopped polls and polls, which are sent by the bot |
||
| 98 | * |
||
| 99 | * @var Poll|null |
||
| 100 | */ |
||
| 101 | private $poll; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Optional. A user changed their answer in a non-anonymous poll. Bots receive new votes only in polls that were sent by |
||
| 105 | * the bot itself. |
||
| 106 | * |
||
| 107 | * @var PollAnswer|null |
||
| 108 | */ |
||
| 109 | private $poll_answer; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @var string |
||
| 113 | */ |
||
| 114 | private $updateType; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @var User|null |
||
| 118 | */ |
||
| 119 | private $effectiveUser; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @var Chat|null |
||
| 123 | */ |
||
| 124 | private $effectiveChat; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @return int |
||
| 128 | */ |
||
| 129 | public function getUpdateId(): int |
||
| 130 | { |
||
| 131 | return $this->update_id; |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @param int $update_id |
||
| 136 | */ |
||
| 137 | public function setUpdateId(int $update_id): void |
||
| 138 | { |
||
| 139 | $this->update_id = $update_id; |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @return Message|null |
||
| 144 | */ |
||
| 145 | public function getMessage(): ?Message |
||
| 146 | { |
||
| 147 | return $this->message; |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @param Message|null $message |
||
| 152 | */ |
||
| 153 | public function setMessage(?Message $message): void |
||
| 154 | { |
||
| 155 | $this->message = $message; |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @return EditedMessage|null |
||
| 160 | */ |
||
| 161 | public function getEditedMessage(): ?EditedMessage |
||
| 162 | { |
||
| 163 | return $this->edited_message; |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @param EditedMessage|null $edited_message |
||
| 168 | */ |
||
| 169 | public function setEditedMessage(?EditedMessage $edited_message): void |
||
| 170 | { |
||
| 171 | $this->edited_message = $edited_message; |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @return ChannelPost|null |
||
| 176 | */ |
||
| 177 | public function getChannelPost(): ?ChannelPost |
||
| 178 | { |
||
| 179 | return $this->channel_post; |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @param ChannelPost|null $channel_post |
||
| 184 | */ |
||
| 185 | public function setChannelPost(?ChannelPost $channel_post): void |
||
| 186 | { |
||
| 187 | $this->channel_post = $channel_post; |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @return EditedChannelPost|null |
||
| 192 | */ |
||
| 193 | public function getEditedChannelPost(): ?EditedChannelPost |
||
| 194 | { |
||
| 195 | return $this->edited_channel_post; |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @param EditedChannelPost|null $edited_channel_post |
||
| 200 | */ |
||
| 201 | public function setEditedChannelPost(?EditedChannelPost $edited_channel_post): void |
||
| 202 | { |
||
| 203 | $this->edited_channel_post = $edited_channel_post; |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @return InlineQuery|null |
||
| 208 | */ |
||
| 209 | public function getInlineQuery(): ?InlineQuery |
||
| 210 | { |
||
| 211 | return $this->inline_query; |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @param InlineQuery|null $inline_query |
||
| 216 | */ |
||
| 217 | public function setInlineQuery(?InlineQuery $inline_query): void |
||
| 218 | { |
||
| 219 | $this->inline_query = $inline_query; |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @return ChosenInlineResult|null |
||
| 224 | */ |
||
| 225 | public function getChosenInlineResult(): ?ChosenInlineResult |
||
| 226 | { |
||
| 227 | return $this->chosen_inline_result; |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @param ChosenInlineResult|null $chosen_inline_result |
||
| 232 | */ |
||
| 233 | public function setChosenInlineResult(?ChosenInlineResult $chosen_inline_result): void |
||
| 234 | { |
||
| 235 | $this->chosen_inline_result = $chosen_inline_result; |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @return CallbackQuery|null |
||
| 240 | */ |
||
| 241 | public function getCallbackQuery(): ?CallbackQuery |
||
| 242 | { |
||
| 243 | return $this->callback_query; |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @param CallbackQuery|null $callback_query |
||
| 248 | */ |
||
| 249 | public function setCallbackQuery(?CallbackQuery $callback_query): void |
||
| 250 | { |
||
| 251 | $this->callback_query = $callback_query; |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @return ShippingQuery|null |
||
| 256 | */ |
||
| 257 | public function getShippingQuery(): ?ShippingQuery |
||
| 258 | { |
||
| 259 | return $this->shipping_query; |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @param ShippingQuery|null $shipping_query |
||
| 264 | */ |
||
| 265 | public function setShippingQuery(?ShippingQuery $shipping_query): void |
||
| 266 | { |
||
| 267 | $this->shipping_query = $shipping_query; |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @return PreCheckoutQuery|null |
||
| 272 | */ |
||
| 273 | public function getPreCheckoutQuery(): ?PreCheckoutQuery |
||
| 274 | { |
||
| 275 | return $this->pre_checkout_query; |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @param PreCheckoutQuery|null $pre_checkout_query |
||
| 280 | */ |
||
| 281 | public function setPreCheckoutQuery(?PreCheckoutQuery $pre_checkout_query): void |
||
| 282 | { |
||
| 283 | $this->pre_checkout_query = $pre_checkout_query; |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @return Poll|null |
||
| 288 | */ |
||
| 289 | public function getPoll(): ?Poll |
||
| 290 | { |
||
| 291 | return $this->poll; |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @param Poll|null $poll |
||
| 296 | */ |
||
| 297 | public function setPoll(?Poll $poll): void |
||
| 298 | { |
||
| 299 | $this->poll = $poll; |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @return PollAnswer|null |
||
| 304 | */ |
||
| 305 | public function getPollAnswer(): ?PollAnswer |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @param PollAnswer|null $poll_answer |
||
| 312 | */ |
||
| 313 | public function setPollAnswer(?PollAnswer $poll_answer): void |
||
| 314 | { |
||
| 315 | $this->poll_answer = $poll_answer; |
||
| 316 | } |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @return string |
||
| 320 | */ |
||
| 321 | public function getUpdateType(): string |
||
| 322 | { |
||
| 323 | return $this->updateType; |
||
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * |
||
| 328 | */ |
||
| 329 | public function detectUpdateType() |
||
| 330 | { |
||
| 331 | if ($this->message && $this->message->getSuccessfulPayment()) { |
||
| 332 | $this->updateType = SuccessfulPayment::class; |
||
| 333 | $this->effectiveUser = $this->message->getFrom(); |
||
| 334 | $this->effectiveChat = $this->message->getChat(); |
||
| 335 | } else if ($this->message && $this->message->getReplyToMessage()) { |
||
| 336 | $this->updateType = ReplyToMessage::class; |
||
| 337 | $this->effectiveUser = $this->message->getFrom(); |
||
| 338 | $this->effectiveChat = $this->message->getChat(); |
||
| 339 | } else if ($this->message && $this->message->getPassportData()) { |
||
| 340 | $this->updateType = PassportData::class; |
||
| 341 | $this->effectiveUser = $this->message->getFrom(); |
||
| 342 | $this->effectiveChat = $this->message->getChat(); |
||
| 343 | } else if ($this->message) { |
||
| 344 | $this->updateType = Message::class; |
||
| 345 | $this->effectiveUser = $this->message->getFrom(); |
||
| 346 | $this->effectiveChat = $this->message->getChat(); |
||
| 347 | } else if ($this->edited_message) { |
||
| 348 | $this->updateType = EditedMessage::class; |
||
| 349 | $this->effectiveUser = $this->edited_message->getFrom(); |
||
| 350 | $this->effectiveChat = $this->edited_message->getChat(); |
||
| 351 | } else if ($this->channel_post) { |
||
| 352 | $this->updateType = ChannelPost::class; |
||
| 353 | $this->effectiveUser = $this->channel_post->getFrom(); |
||
| 354 | $this->effectiveChat = $this->channel_post->getChat(); |
||
| 355 | } else if ($this->edited_channel_post) { |
||
| 356 | $this->updateType = EditedChannelPost::class; |
||
| 357 | $this->effectiveUser = $this->edited_channel_post->getFrom(); |
||
| 358 | $this->effectiveChat = $this->edited_channel_post->getChat(); |
||
| 359 | } else if ($this->callback_query) { |
||
| 360 | $this->updateType = CallbackQuery::class; |
||
| 361 | $this->effectiveUser = $this->callback_query->getFrom(); |
||
| 362 | $this->effectiveChat = $this->callback_query->getMessage() |
||
| 363 | ? $this->callback_query->getMessage()->getChat() |
||
| 364 | : null; |
||
| 365 | } else if ($this->shipping_query) { |
||
| 366 | $this->updateType = ShippingQuery::class; |
||
| 367 | $this->effectiveUser = $this->shipping_query->getFrom(); |
||
| 368 | } else if ($this->pre_checkout_query) { |
||
| 369 | $this->updateType = PreCheckoutQuery::class; |
||
| 370 | $this->effectiveUser = $this->pre_checkout_query->getFrom(); |
||
| 371 | } else if ($this->inline_query) { |
||
| 372 | $this->updateType = InlineQuery::class; |
||
| 373 | $this->effectiveUser = $this->inline_query->getFrom(); |
||
| 374 | } else if ($this->chosen_inline_result) { |
||
| 375 | $this->updateType = ChosenInlineResult::class; |
||
| 376 | $this->effectiveUser = $this->chosen_inline_result->getFrom(); |
||
| 377 | } else if ($this->poll) { |
||
| 378 | $this->updateType = Poll::class; |
||
| 379 | } else if ($this->poll_answer) { |
||
| 380 | $this->updateType = PollAnswer::class; |
||
| 381 | $this->effectiveUser = $this->poll_answer->getUser(); |
||
| 382 | } |
||
| 383 | } |
||
| 384 | |||
| 385 | /** |
||
| 386 | * @return User|null |
||
| 387 | */ |
||
| 388 | public function getEffectiveUser(): ?User |
||
| 391 | } |
||
| 392 | |||
| 393 | /** |
||
| 394 | * @param User|null $effectiveUser |
||
| 395 | */ |
||
| 396 | public function setEffectiveUser(?User $effectiveUser): void |
||
| 397 | { |
||
| 398 | $this->effectiveUser = $effectiveUser; |
||
| 399 | } |
||
| 400 | |||
| 401 | /** |
||
| 402 | * @return Chat|null |
||
| 403 | */ |
||
| 404 | public function getEffectiveChat(): ?Chat |
||
| 405 | { |
||
| 406 | return $this->effectiveChat; |
||
| 407 | } |
||
| 408 | |||
| 409 | /** |
||
| 410 | * @param Chat|null $effectiveChat |
||
| 411 | */ |
||
| 412 | public function setEffectiveChat(?Chat $effectiveChat): void |
||
| 415 | } |
||
| 416 | |||
| 417 | public function __toString() |
||
| 420 | } |
||
| 421 | |||
| 422 | /** |
||
| 423 | * @inheritDoc |
||
| 424 | */ |
||
| 425 | public function jsonSerialize() |
||
| 436 |