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 |
||
| 5 | class Message extends AbstractEntity |
||
| 6 | { |
||
| 7 | const ENTITY_TYPE = 'Message'; |
||
| 8 | |||
| 9 | const MESSAGE_TYPE_REGEXP_COMMAND = 'RegexpCommand'; |
||
| 10 | |||
| 11 | protected $messageType = self::ENTITY_TYPE; |
||
| 12 | |||
| 13 | protected $message_id; |
||
| 14 | |||
| 15 | /** @var User $from */ |
||
| 16 | protected $from; |
||
| 17 | |||
| 18 | protected $date; |
||
| 19 | |||
| 20 | /** @var Chat $chat */ |
||
| 21 | protected $chat; |
||
| 22 | |||
| 23 | /** @var User $forward_from */ |
||
| 24 | protected $forward_from; |
||
| 25 | |||
| 26 | protected $forward_date; |
||
| 27 | |||
| 28 | /** @var Message $reply_to_message */ |
||
| 29 | protected $reply_to_message; |
||
| 30 | |||
| 31 | protected $text; |
||
| 32 | |||
| 33 | /** @var MessageEntityArray */ |
||
| 34 | protected $entities; |
||
| 35 | |||
| 36 | /** @var Audio $audio */ |
||
| 37 | protected $audio; |
||
| 38 | |||
| 39 | /** @var Document $document */ |
||
| 40 | protected $document; |
||
| 41 | |||
| 42 | /** @var PhotoSizeArray $photo */ |
||
| 43 | protected $photo; |
||
| 44 | |||
| 45 | /** @var Sticker $sticker */ |
||
| 46 | protected $sticker; |
||
| 47 | |||
| 48 | /** @var Video $video */ |
||
| 49 | protected $video; |
||
| 50 | |||
| 51 | /** @var Voice $voice */ |
||
| 52 | protected $voice; |
||
| 53 | |||
| 54 | protected $caption; |
||
| 55 | |||
| 56 | /** @var Contact $contact */ |
||
| 57 | protected $contact; |
||
| 58 | |||
| 59 | /** @var Location $location */ |
||
| 60 | protected $location; |
||
| 61 | |||
| 62 | /** @var User $new_chat_participant */ |
||
| 63 | protected $new_chat_participant; |
||
| 64 | |||
| 65 | /** @var User $left_chat_participant */ |
||
| 66 | protected $left_chat_participant; |
||
| 67 | |||
| 68 | protected $new_chat_title; |
||
| 69 | |||
| 70 | /** @var PhotoSize[] $new_chat_photo */ |
||
| 71 | protected $new_chat_photo; |
||
| 72 | |||
| 73 | protected $delete_chat_photo; |
||
| 74 | |||
| 75 | protected $group_chat_created; |
||
| 76 | |||
| 77 | protected $supergroup_chat_created; |
||
| 78 | |||
| 79 | protected $channel_chat_created; |
||
| 80 | |||
| 81 | protected $migrate_to_chat_id; |
||
| 82 | |||
| 83 | protected $migrate_from_chat_id; |
||
| 84 | |||
| 85 | protected $pinned_message; |
||
| 86 | |||
| 87 | protected $builtInEntities = [ |
||
| 88 | 'from' => User::class, |
||
| 89 | 'chat' => Chat::class, |
||
| 90 | 'location' => Location::class, |
||
| 91 | 'document' => Document::class, |
||
| 92 | 'sticker' => Sticker::class, |
||
| 93 | 'video' => Video::class, |
||
| 94 | 'voice' => Voice::class, |
||
| 95 | 'contact' => Contact::class, |
||
| 96 | 'audio' => Audio::class, |
||
| 97 | 'photo' => PhotoSizeArray::class, |
||
| 98 | 'entities' => MessageEntityArray::class |
||
| 99 | ]; |
||
| 100 | |||
| 101 | public function __construct(array $data) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @return null |
||
| 110 | */ |
||
| 111 | public function getMessageId() |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @return User |
||
| 118 | */ |
||
| 119 | public function getFrom() |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @return null |
||
| 126 | */ |
||
| 127 | public function getChatId() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @return null |
||
| 138 | */ |
||
| 139 | public function getDate() |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @param mixed $text |
||
| 146 | */ |
||
| 147 | public function setText($text) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @return null |
||
| 154 | */ |
||
| 155 | public function getText() |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @return array |
||
| 162 | */ |
||
| 163 | public function getEntities() |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @param array $entities |
||
| 170 | */ |
||
| 171 | public function setEntities($entities) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @return Chat |
||
| 190 | */ |
||
| 191 | public function getChat() |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @return string |
||
| 198 | */ |
||
| 199 | public function getMessageType() |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @return Location |
||
| 206 | */ |
||
| 207 | public function getLocation() |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @param Location $location |
||
| 214 | */ |
||
| 215 | protected function setLocation($location) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @return Document |
||
| 223 | */ |
||
| 224 | public function getDocument() |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @param Document $document |
||
| 231 | */ |
||
| 232 | public function setDocument($document) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @return Sticker |
||
| 240 | */ |
||
| 241 | public function getSticker() |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @param Sticker $sticker |
||
| 248 | */ |
||
| 249 | public function setSticker($sticker) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @return Video |
||
| 257 | */ |
||
| 258 | public function getVideo() |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @param Video $video |
||
| 265 | */ |
||
| 266 | public function setVideo($video) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @return Voice |
||
| 274 | */ |
||
| 275 | public function getVoice() |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @param Voice $voice |
||
| 282 | */ |
||
| 283 | public function setVoice($voice) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @return Contact |
||
| 291 | */ |
||
| 292 | public function getContact() |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @param Contact $contact |
||
| 299 | */ |
||
| 300 | public function setContact($contact) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * @return Audio |
||
| 308 | */ |
||
| 309 | public function getAudio() |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @param Audio $audio |
||
| 316 | */ |
||
| 317 | public function setAudio($audio) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @return PhotoSizeArray |
||
| 325 | */ |
||
| 326 | public function getPhoto() |
||
| 330 | |||
| 331 | /** |
||
| 332 | * @param PhotoSizeArray $photo |
||
| 333 | */ |
||
| 334 | public function setPhoto($photo) |
||
| 339 | |||
| 340 | protected function setMessageType($object) |
||
| 348 | |||
| 349 | public function getMessageTypeEntity() |
||
| 385 | |||
| 386 | public function hasBuiltinRegexpCommand($pattern) |
||
| 390 | } |
||
| 391 |
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.