Channel   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 226
Duplicated Lines 18.58 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 24.66%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 18
lcom 1
cbo 4
dl 42
loc 226
ccs 18
cts 73
cp 0.2466
rs 10
c 1
b 0
f 1

17 Methods

Rating   Name   Duplication   Size   Complexity  
A getPurpose() 0 4 1
A getTopic() 0 4 1
A getId() 0 4 1
A getName() 0 4 1
A getMembers() 0 9 2
A getTimeCreated() 0 6 1
A getCreator() 0 4 1
A getUnreadCount() 0 4 1
A isArchived() 0 4 1
A rename() 0 10 1
A setPurpose() 0 10 1
A setTopic() 0 10 1
A archive() 8 8 1
A unarchive() 8 8 1
A inviteUser() 9 9 1
A kickUser() 9 9 1
A close() 8 8 1

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;
3
4
use React\Promise;
5
6
/**
7
 * Represents a single Slack channel.
8
 */
9
class Channel extends ClientObject implements ChannelInterface
10
{
11
    /**
12
     * {@inheritDoc}
13
     */
14 3
    public function getId()
15
    {
16 3
        return $this->data['id'];
17
    }
18
19
    /**
20
     * Gets the channel name.
21
     *
22
     * @return string The name of the channel.
23
     */
24 1
    public function getName()
25
    {
26 1
        return $this->data['name'];
27
    }
28
29
    /**
30
     * Gets the channel's purpose text.
31
     *
32
     * @return string The channel's purpose text.
33
     */
34 1
    public function getPurpose()
35
    {
36 1
        return $this->data['purpose']['value'];
37
    }
38
39
    /**
40
     * Gets the channel topic text.
41
     *
42
     * @return string The channel's topic text.
43
     */
44 1
    public function getTopic()
45
    {
46 1
        return $this->data['topic']['value'];
47
    }
48
49
    /**
50
     * Gets an iterator over all users in the channel.
51
     *
52
     * @return \React\Promise\PromiseInterface A promise for an array of user
53
     *                                         objects for each member in the channel.
54
     */
55
    public function getMembers()
56
    {
57
        $memberPromises = [];
58
        foreach ($this->data['members'] as $memberId) {
59
            $memberPromises[] = $this->client->getUserById($memberId);
60
        }
61
62
        return Promise\all($memberPromises);
63
    }
64
65
    /**
66
     * Gets the time the channel was created.
67
     *
68
     * @return \DateTime The time the channel was created.
69
     */
70 1
    public function getTimeCreated()
71
    {
72 1
        $time = new \DateTime();
73 1
        $time->setTimestamp($this->data['created']);
74 1
        return $time;
75
    }
76
77
    /**
78
     * Gets the creator of the channel.
79
     *
80
     * @return \React\Promise\PromiseInterface The user who created the channel.
81
     */
82 1
    public function getCreator()
83
    {
84 1
        return $this->client->getUserById($this->data['creator']);
85
    }
86
87
    /**
88
     * Gets the number of message unread by the authenticated user.
89
     *
90
     * @return int The number of unread messages.
91
     */
92 1
    public function getUnreadCount()
93
    {
94 1
        return $this->data['unread_count'];
95
    }
96
97
    /**
98
     * Checks if the channel has been archived.
99
     *
100
     * @return bool True if the channel has been archived, otherwise false.
101
     */
102 1
    public function isArchived()
103
    {
104 1
        return $this->data['is_archived'];
105
    }
106
107
    /**
108
     * Renames the channel.
109
     *
110
     * @param string $name The name to set to.
111
     *
112
     * @return \React\Promise\PromiseInterface
113
     */
114
    public function rename($name)
115
    {
116
        return $this->client->apiCall('channels.rename', [
117
            'channel' => $this->getId(),
118
            'name' => $name,
119
        ])->then(function () use ($name) {
120
            $this->data['name'] = $name;
121
            return $name;
122
        });
123
    }
124
125
    /**
126
     * Sets the channel's purpose text.
127
     *
128
     * @param string $text The new purpose text to set to.
129
     *
130
     * @return \React\Promise\PromiseInterface
131
     */
132
    public function setPurpose($text)
133
    {
134
        return $this->client->apiCall('channels.setPurpose', [
135
            'channel' => $this->getId(),
136
            'purpose' => $text,
137
        ])->then(function () use ($text) {
138
            $this->data['purpose']['value'] = $text;
139
            return $text;
140
        });
141
    }
142
143
    /**
144
     * Sets the channel topic text.
145
     *
146
     * @param string $text The new topic text to set to.
147
     *
148
     * @return \React\Promise\PromiseInterface
149
     */
150
    public function setTopic($text)
151
    {
152
        return $this->client->apiCall('channels.setTopic', [
153
            'channel' => $this->getId(),
154
            'topic' => $text,
155
        ])->then(function () use ($text) {
156
            $this->data['topic']['value'] = $text;
157
            return $text;
158
        });
159
    }
160
161
    /**
162
     * Archives the channel.
163
     *
164
     * @return \React\Promise\PromiseInterface
165
     */
166 View Code Duplication
    public function archive()
167
    {
168
        return $this->client->apiCall('channels.archive', [
169
            'channel' => $this->getId(),
170
        ])->then(function () {
171
            $this->data['is_archived'] = true;
172
        });
173
    }
174
175
    /**
176
     * Un-archives the channel.
177
     *
178
     * @return \React\Promise\PromiseInterface
179
     */
180 View Code Duplication
    public function unarchive()
181
    {
182
        return $this->client->apiCall('channels.unarchive', [
183
            'channel' => $this->getId(),
184
        ])->then(function () {
185
            $this->data['is_archived'] = false;
186
        });
187
    }
188
189
    /**
190
     * Invites a user to the channel.
191
     *
192
     * @param User The user to invite.
193
     *
194
     * @return \React\Promise\PromiseInterface
195
     */
196 View Code Duplication
    public function inviteUser(User $user)
197
    {
198
        return $this->client->apiCall('channels.invite', [
199
            'channel' => $this->getId(),
200
            'user' => $user->getId(),
201
        ])->then(function () use ($user) {
202
            $this->data['members'][] = $user->getId();
203
        });
204
    }
205
206
    /**
207
     * Kicks a user from the channel.
208
     *
209
     * @param User The user to kick.
210
     *
211
     * @return \React\Promise\PromiseInterface
212
     */
213 View Code Duplication
    public function kickUser(User $user)
214
    {
215
        return $this->client->apiCall('channels.kick', [
216
            'channel' => $this->getId(),
217
            'user' => $user->getId(),
218
        ])->then(function () use ($user) {
219
            unset($this->data['members'][$user->getId()]);
220
        });
221
    }
222
223
    /**
224
     * {@inheritDoc}
225
     */
226 View Code Duplication
    public function close()
227
    {
228
        return $this->client->apiCall('channels.close', [
229
            'channel' => $this->getId(),
230
        ])->then(function ($response) {
231
            return !isset($response['no_op']);
232
        });
233
    }
234
}
235