Complex classes like Message 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 Message, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class Message extends AbstractEntity |
||
| 9 | { |
||
| 10 | const ENTITY_TYPE = 'Message'; |
||
| 11 | |||
| 12 | protected $messageType = self::ENTITY_TYPE; |
||
| 13 | |||
| 14 | protected $message_id; |
||
| 15 | |||
| 16 | /** @var User $from */ |
||
| 17 | protected $from; |
||
| 18 | |||
| 19 | protected $date; |
||
| 20 | |||
| 21 | /** @var Chat $chat */ |
||
| 22 | protected $chat; |
||
| 23 | |||
| 24 | /** @var User $forward_from */ |
||
| 25 | protected $forward_from; |
||
| 26 | |||
| 27 | protected $forward_date; |
||
| 28 | |||
| 29 | /** @var Message $reply_to_message */ |
||
| 30 | protected $reply_to_message; |
||
| 31 | |||
| 32 | protected $text; |
||
| 33 | |||
| 34 | /** @var MessageEntityArray */ |
||
| 35 | protected $entities; |
||
| 36 | |||
| 37 | /** @var Audio $audio */ |
||
| 38 | protected $audio; |
||
| 39 | |||
| 40 | /** @var Document $document */ |
||
| 41 | protected $document; |
||
| 42 | |||
| 43 | /** @var PhotoSizeArray $photo */ |
||
| 44 | protected $photo; |
||
| 45 | |||
| 46 | /** @var Sticker $sticker */ |
||
| 47 | protected $sticker; |
||
| 48 | |||
| 49 | /** @var Video $video */ |
||
| 50 | protected $video; |
||
| 51 | |||
| 52 | /** @var Voice $voice */ |
||
| 53 | protected $voice; |
||
| 54 | |||
| 55 | protected $caption; |
||
| 56 | |||
| 57 | /** @var Contact $contact */ |
||
| 58 | protected $contact; |
||
| 59 | |||
| 60 | /** @var Location $location */ |
||
| 61 | protected $location; |
||
| 62 | |||
| 63 | /** @var User $new_chat_participant */ |
||
| 64 | protected $new_chat_participant; |
||
| 65 | |||
| 66 | /** @var User $left_chat_participant */ |
||
| 67 | protected $left_chat_participant; |
||
| 68 | |||
| 69 | protected $new_chat_title; |
||
| 70 | |||
| 71 | /** @var PhotoSize[] $new_chat_photo */ |
||
| 72 | protected $new_chat_photo; |
||
| 73 | |||
| 74 | protected $delete_chat_photo; |
||
| 75 | |||
| 76 | protected $group_chat_created; |
||
| 77 | |||
| 78 | protected $supergroup_chat_created; |
||
| 79 | |||
| 80 | protected $channel_chat_created; |
||
| 81 | |||
| 82 | protected $migrate_to_chat_id; |
||
| 83 | |||
| 84 | protected $migrate_from_chat_id; |
||
| 85 | |||
| 86 | protected $pinned_message; |
||
| 87 | |||
| 88 | protected $builtInEntities = [ |
||
| 89 | 'from' => User::class, |
||
| 90 | 'chat' => Chat::class, |
||
| 91 | 'location' => Location::class, |
||
| 92 | 'document' => Document::class, |
||
| 93 | 'sticker' => Sticker::class, |
||
| 94 | 'video' => Video::class, |
||
| 95 | 'voice' => Voice::class, |
||
| 96 | 'contact' => Contact::class, |
||
| 97 | 'audio' => Audio::class, |
||
| 98 | 'photo' => PhotoSizeArray::class, |
||
| 99 | 'entities' => MessageEntityArray::class |
||
| 100 | ]; |
||
| 101 | |||
| 102 | public function __construct(array $data) |
||
| 103 | { |
||
| 104 | $data = $data['message'] ?? $data; |
||
| 105 | |||
| 106 | parent::__construct($data); |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @return null |
||
| 111 | */ |
||
| 112 | public function getMessageId() |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @return User |
||
| 119 | */ |
||
| 120 | public function getFrom() |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @return null |
||
| 127 | */ |
||
| 128 | public function getChatId() |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @return null |
||
| 139 | */ |
||
| 140 | public function getDate() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @param mixed $text |
||
| 147 | */ |
||
| 148 | public function setText($text) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @return null |
||
| 155 | */ |
||
| 156 | public function getText() |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @return array |
||
| 163 | */ |
||
| 164 | public function getEntities() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @param array $entities |
||
| 171 | */ |
||
| 172 | public function setEntities($entities) |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @return Chat |
||
| 191 | */ |
||
| 192 | public function getChat() |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @return string |
||
| 199 | */ |
||
| 200 | public function getMessageType() |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @return Location |
||
| 207 | */ |
||
| 208 | public function getLocation() |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param Location $location |
||
| 215 | */ |
||
| 216 | protected function setLocation($location) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @return Document |
||
| 224 | */ |
||
| 225 | public function getDocument() |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @param Document $document |
||
| 232 | */ |
||
| 233 | public function setDocument($document) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @return Sticker |
||
| 241 | */ |
||
| 242 | public function getSticker() |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @param Sticker $sticker |
||
| 249 | */ |
||
| 250 | public function setSticker($sticker) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @return Video |
||
| 258 | */ |
||
| 259 | public function getVideo() |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @param Video $video |
||
| 266 | */ |
||
| 267 | public function setVideo($video) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @return Voice |
||
| 275 | */ |
||
| 276 | public function getVoice() |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @param Voice $voice |
||
| 283 | */ |
||
| 284 | public function setVoice($voice) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @return Contact |
||
| 292 | */ |
||
| 293 | public function getContact() |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @param Contact $contact |
||
| 300 | */ |
||
| 301 | public function setContact($contact) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @return Audio |
||
| 309 | */ |
||
| 310 | public function getAudio() |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @param Audio $audio |
||
| 317 | */ |
||
| 318 | public function setAudio($audio) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @return PhotoSizeArray |
||
| 326 | */ |
||
| 327 | public function getPhoto() |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @param PhotoSizeArray $photo |
||
| 334 | */ |
||
| 335 | public function setPhoto($photo) |
||
| 340 | |||
| 341 | protected function setMessageType($object) |
||
| 349 | |||
| 350 | public function getMessageTypeEntity() |
||
| 386 | } |
||
| 387 |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.