Message   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 76
Duplicated Lines 13.16 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 52.63%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 3
dl 10
loc 76
ccs 10
cts 19
cp 0.5263
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getText() 0 4 1
A isMarkdownEnabled() 0 4 2
A hasAttachments() 0 4 2
A getAttachments() 0 4 2
A getChannel() 0 4 1
A getUser() 0 4 1
A jsonUnserialize() 10 10 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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