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 | protected $messageType = self::ENTITY_TYPE; |
||
| 10 | |||
| 11 | protected $message_id; |
||
| 12 | |||
| 13 | /** @var User $from */ |
||
| 14 | protected $from; |
||
| 15 | |||
| 16 | protected $date; |
||
| 17 | |||
| 18 | /** @var Chat $chat */ |
||
| 19 | protected $chat; |
||
| 20 | |||
| 21 | /** @var User $forward_from */ |
||
| 22 | protected $forward_from; |
||
| 23 | |||
| 24 | protected $forward_date; |
||
| 25 | |||
| 26 | /** @var Message $reply_to_message */ |
||
| 27 | protected $reply_to_message; |
||
| 28 | |||
| 29 | /** @var Command $command */ |
||
| 30 | protected $command; |
||
| 31 | |||
| 32 | protected $text; |
||
| 33 | |||
| 34 | /** @var Audio $audio */ |
||
| 35 | protected $audio; |
||
| 36 | |||
| 37 | /** @var Document $document */ |
||
| 38 | protected $document; |
||
| 39 | |||
| 40 | /** @var PhotoSize $photo */ |
||
| 41 | protected $photo; |
||
| 42 | |||
| 43 | /** @var Sticker $sticker */ |
||
| 44 | protected $sticker; |
||
| 45 | |||
| 46 | /** @var Video $video */ |
||
| 47 | protected $video; |
||
| 48 | |||
| 49 | /** @var Voice $voice */ |
||
| 50 | protected $voice; |
||
| 51 | |||
| 52 | protected $caption; |
||
| 53 | |||
| 54 | /** @var Contact $contact */ |
||
| 55 | protected $contact; |
||
| 56 | |||
| 57 | /** @var Location $location */ |
||
| 58 | protected $location; |
||
| 59 | |||
| 60 | /** @var User $new_chat_participant */ |
||
| 61 | protected $new_chat_participant; |
||
| 62 | |||
| 63 | /** @var User $left_chat_participant */ |
||
| 64 | protected $left_chat_participant; |
||
| 65 | |||
| 66 | protected $new_chat_title; |
||
| 67 | |||
| 68 | /** @var PhotoSize[] $new_chat_photo */ |
||
| 69 | protected $new_chat_photo; |
||
| 70 | |||
| 71 | protected $delete_chat_photo; |
||
| 72 | |||
| 73 | protected $group_chat_created; |
||
| 74 | |||
| 75 | protected $supergroup_chat_created; |
||
| 76 | |||
| 77 | protected $channel_chat_created; |
||
| 78 | |||
| 79 | protected $migrate_to_chat_id; |
||
| 80 | |||
| 81 | protected $migrate_from_chat_id; |
||
| 82 | |||
| 83 | protected $builtInEntities = [ |
||
| 84 | 'from' => User::class, |
||
| 85 | 'chat' => Chat::class, |
||
| 86 | 'location' => Location::class, |
||
| 87 | 'document' => Document::class, |
||
| 88 | 'sticker' => Sticker::class, |
||
| 89 | 'video' => Video::class, |
||
| 90 | 'voice' => Voice::class, |
||
| 91 | 'contact' => Contact::class, |
||
| 92 | 'audio' => Audio::class, |
||
| 93 | 'photo' => PhotoSizeArray::class, |
||
| 94 | 'command' => Command::class, |
||
| 95 | ]; |
||
| 96 | |||
| 97 | public function __construct(array $data) |
||
| 103 | |||
| 104 | public function isCommand() { |
||
| 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) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @return null |
||
| 159 | */ |
||
| 160 | public function getText() |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @return Command |
||
| 167 | */ |
||
| 168 | public function getCommand() |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @param Command $command |
||
| 175 | */ |
||
| 176 | public function setCommand($command) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @return Chat |
||
| 184 | */ |
||
| 185 | public function getChat() |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @return string |
||
| 192 | */ |
||
| 193 | public function getMessageType() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @return Location |
||
| 200 | */ |
||
| 201 | public function getLocation() |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @param Location $location |
||
| 208 | */ |
||
| 209 | protected function setLocation($location) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @return Document |
||
| 217 | */ |
||
| 218 | public function getDocument() |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @param Document $document |
||
| 225 | */ |
||
| 226 | public function setDocument($document) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @return Sticker |
||
| 234 | */ |
||
| 235 | public function getSticker() |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @param Sticker $sticker |
||
| 242 | */ |
||
| 243 | public function setSticker($sticker) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @return Video |
||
| 251 | */ |
||
| 252 | public function getVideo() |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @param Video $video |
||
| 259 | */ |
||
| 260 | public function setVideo($video) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @return Voice |
||
| 268 | */ |
||
| 269 | public function getVoice() |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @param Voice $voice |
||
| 276 | */ |
||
| 277 | public function setVoice($voice) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @return Contact |
||
| 285 | */ |
||
| 286 | public function getContact() |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @param Contact $contact |
||
| 293 | */ |
||
| 294 | public function setContact($contact) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @return Audio |
||
| 302 | */ |
||
| 303 | public function getAudio() |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @param Audio $audio |
||
| 310 | */ |
||
| 311 | public function setAudio($audio) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * @return PhotoSizeArray |
||
| 319 | */ |
||
| 320 | public function getPhoto() |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @param PhotoSizeArray $photo |
||
| 327 | */ |
||
| 328 | public function setPhoto($photo) |
||
| 333 | |||
| 334 | protected function setMessageType($object) |
||
| 342 | |||
| 343 | public function getMessageTypeEntity() |
||
| 379 | } |
||
| 380 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..