Message::jsonUnserialize()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 10
loc 10
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 1
crap 12
1
<?php
2
namespace Slack\Message;
3
4
use Slack\ClientObject;
5
6
/**
7
 * Represents a chat message and its data.
8
 */
9
class Message extends ClientObject
10
{
11
    /**
12
     * Gets the message text.
13
     *
14
     * @return string
15
     */
16 2
    public function getText()
17
    {
18 2
        return $this->data['text'];
19
    }
20
21
    /**
22
     * Checks if Markdown is enabled for the message text.
23
     *
24
     * @return bool
25
     */
26
    public function isMarkdownEnabled()
27
    {
28
        return isset($this->data['mrkdwn']) ? $this->data['mrkdwn'] == true : true;
29
    }
30
31
    /**
32
     * Checks if the message has attachments.
33
     *
34
     * @return bool True if the message has attachments, otherwise false.
35
     */
36 3
    public function hasAttachments()
37
    {
38 3
        return isset($this->data['attachments']) && count($this->data['attachments']) > 0;
39
    }
40
41
    /**
42
     * Gets all message attachments.
43
     *
44
     * @return Attachment[]
45
     */
46 2
    public function getAttachments()
47
    {
48 2
        return isset($this->data['attachments']) ? $this->data['attachments'] : [];
49
    }
50
51
    /**
52
     * Gets the channel the message is in.
53
     *
54
     * @return \React\Promise\PromiseInterface
55
     */
56 2
    public function getChannel()
57
    {
58 2
        return $this->client->getChannelById($this->data['channel']);
59
    }
60
61
    /**
62
     * Gets the user that sent the message.
63
     *
64
     * @return \React\Promise\PromiseInterface
65
     */
66 2
    public function getUser()
67
    {
68 2
        return $this->client->getUserById($this->data['user']);
69
    }
70
71
    /**
72
     * {@inheritDoc}
73
     */
74 View Code Duplication
    public function jsonUnserialize(array $data)
75
    {
76
        if (!isset($this->data['attachments'])) {
77
            return;
78
        }
79
80
        for ($i = 0; $i < count($this->data['attachments']); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
81
            $this->data['attachments'][$i] = Attachment::fromData($this->data['attachments'][$i]);
82
        }
83
    }
84
}
85