GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 64a210...2d6991 )
by Stan
02:42
created

Message   B

Complexity

Total Complexity 42

Size/Duplication

Total Lines 375
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2
Metric Value
wmc 42
lcom 2
cbo 2
dl 0
loc 375
rs 8.295

30 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A isCommand() 0 4 1
A getMessageId() 0 4 1
A getFrom() 0 4 1
A getChatId() 0 8 2
A getDate() 0 4 1
A setText() 0 8 2
A getText() 0 4 1
A getCommand() 0 4 1
A setCommand() 0 5 1
A getChat() 0 4 1
A getMessageType() 0 4 1
A getLocation() 0 4 1
A setLocation() 0 5 1
A getDocument() 0 4 1
A setDocument() 0 5 1
A getSticker() 0 4 1
A setSticker() 0 5 1
A getVideo() 0 4 1
A setVideo() 0 5 1
A getVoice() 0 4 1
A setVoice() 0 5 1
A getContact() 0 4 1
A setContact() 0 5 1
A getAudio() 0 4 1
A setAudio() 0 5 1
A getPhoto() 0 4 1
A setPhoto() 0 5 1
A setMessageType() 0 8 2
D getMessageTypeEntity() 0 36 10

How to fix   Complexity   

Complex Class

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
2
3
namespace Teebot\Entity;
4
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)
98
    {
99
        $data = $data['message'] ?? $data;
100
101
        parent::__construct($data);
102
    }
103
104
    public function isCommand() {
105
106
        return preg_match(Command::PATTERN, (string) $this->text);
107
    }
108
109
    /**
110
     * @return null
111
     */
112
    public function getMessageId()
113
    {
114
        return $this->message_id;
115
    }
116
117
    /**
118
     * @return User
119
     */
120
    public function getFrom()
121
    {
122
        return $this->from;
123
    }
124
125
    /**
126
     * @return null
127
     */
128
    public function getChatId()
129
    {
130
        if ($this->chat instanceof Chat) {
131
            return $this->chat->getId();
132
        }
133
134
        return null;
135
    }
136
137
    /**
138
     * @return null
139
     */
140
    public function getDate()
141
    {
142
        return $this->date;
143
    }
144
145
    /**
146
     * @param mixed $text
147
     */
148
    public function setText($text)
149
    {
150
        $this->text = $text;
151
152
        if ($this->isCommand()) {
153
            $this->command = ['text' => $text];
0 ignored issues
show
Documentation Bug introduced by
It seems like array('text' => $text) of type array<string,*,{"text":"*"}> is incompatible with the declared type object<Teebot\Entity\Command> of property $command.

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..

Loading history...
154
        }
155
    }
156
157
    /**
158
     * @return null
159
     */
160
    public function getText()
161
    {
162
        return $this->text;
163
    }
164
165
    /**
166
     * @return Command
167
     */
168
    public function getCommand()
169
    {
170
        return $this->command;
171
    }
172
173
    /**
174
     * @param Command $command
175
     */
176
    public function setCommand($command)
177
    {
178
        $this->command = $command;
179
        $this->setMessageType($command);
180
    }
181
182
    /**
183
     * @return Chat
184
     */
185
    public function getChat()
186
    {
187
        return $this->chat;
188
    }
189
190
    /**
191
     * @return string
192
     */
193
    public function getMessageType()
194
    {
195
        return $this->messageType;
196
    }
197
198
    /**
199
     * @return Location
200
     */
201
    public function getLocation()
202
    {
203
        return $this->location;
204
    }
205
206
    /**
207
     * @param Location $location
208
     */
209
    protected function setLocation($location)
210
    {
211
        $this->location = $location;
212
        $this->setMessageType($location);
213
    }
214
215
    /**
216
     * @return Document
217
     */
218
    public function getDocument()
219
    {
220
        return $this->document;
221
    }
222
223
    /**
224
     * @param Document $document
225
     */
226
    public function setDocument($document)
227
    {
228
        $this->document = $document;
229
        $this->setMessageType($document);
230
    }
231
232
    /**
233
     * @return Sticker
234
     */
235
    public function getSticker()
236
    {
237
        return $this->sticker;
238
    }
239
240
    /**
241
     * @param Sticker $sticker
242
     */
243
    public function setSticker($sticker)
244
    {
245
        $this->sticker = $sticker;
246
        $this->setMessageType($sticker);
247
    }
248
249
    /**
250
     * @return Video
251
     */
252
    public function getVideo()
253
    {
254
        return $this->video;
255
    }
256
257
    /**
258
     * @param Video $video
259
     */
260
    public function setVideo($video)
261
    {
262
        $this->video = $video;
263
        $this->setMessageType($video);
264
    }
265
266
    /**
267
     * @return Voice
268
     */
269
    public function getVoice()
270
    {
271
        return $this->voice;
272
    }
273
274
    /**
275
     * @param Voice $voice
276
     */
277
    public function setVoice($voice)
278
    {
279
        $this->voice = $voice;
280
        $this->setMessageType($voice);
281
    }
282
283
    /**
284
     * @return Contact
285
     */
286
    public function getContact()
287
    {
288
        return $this->contact;
289
    }
290
291
    /**
292
     * @param Contact $contact
293
     */
294
    public function setContact($contact)
295
    {
296
        $this->contact = $contact;
297
        $this->setMessageType($contact);
298
    }
299
300
    /**
301
     * @return Audio
302
     */
303
    public function getAudio()
304
    {
305
        return $this->audio;
306
    }
307
308
    /**
309
     * @param Audio $audio
310
     */
311
    public function setAudio($audio)
312
    {
313
        $this->audio = $audio;
314
        $this->setMessageType($audio);
315
    }
316
317
    /**
318
     * @return PhotoSizeArray
319
     */
320
    public function getPhoto()
321
    {
322
        return $this->photo;
323
    }
324
325
    /**
326
     * @param PhotoSizeArray $photo
327
     */
328
    public function setPhoto($photo)
329
    {
330
        $this->photo = $photo;
0 ignored issues
show
Documentation Bug introduced by
It seems like $photo of type object<Teebot\Entity\PhotoSizeArray> is incompatible with the declared type object<Teebot\Entity\PhotoSize> of property $photo.

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..

Loading history...
331
        $this->setMessageType($photo);
332
    }
333
334
    protected function setMessageType($object)
335
    {
336
        $this->messageType = static::ENTITY_TYPE;
337
338
        if ($object instanceof AbstractEntity) {
339
            $this->messageType = $object->getEntityType();
340
        }
341
    }
342
343
    public function getMessageTypeEntity()
344
    {
345
        $messageTypeEntity = null;
346
347
        switch ($this->getMessageType()) {
348
            case Location::ENTITY_TYPE:
0 ignored issues
show
Coding Style introduced by
CASE statements must be defined using a colon

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
349
                $messageTypeEntity = $this->location;
350
                break;
351
            case Document::ENTITY_TYPE:
0 ignored issues
show
Coding Style introduced by
CASE statements must be defined using a colon

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
352
                $messageTypeEntity = $this->document;
353
                break;
354
            case Sticker::ENTITY_TYPE:
355
                $messageTypeEntity = $this->sticker;
356
                break;
357
            case Video::ENTITY_TYPE:
358
                $messageTypeEntity = $this->video;
359
                break;
360
            case Voice::ENTITY_TYPE:
361
                $messageTypeEntity = $this->voice;
362
                break;
363
            case Contact::ENTITY_TYPE:
364
                $messageTypeEntity = $this->contact;
365
                break;
366
            case Audio::ENTITY_TYPE:
367
                $messageTypeEntity = $this->audio;
368
                break;
369
            case PhotoSizeArray::ENTITY_TYPE:
370
                $messageTypeEntity = $this->photo;
371
                break;
372
            case Command::ENTITY_TYPE:
373
                $messageTypeEntity = $this->command;
374
                break;
375
        }
376
377
        return $messageTypeEntity;
378
    }
379
}
380