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.

Reactions   F
last analyzed

Complexity

Total Complexity 61

Size/Duplication

Total Lines 267
Duplicated Lines 41.2 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 61
lcom 1
cbo 5
dl 110
loc 267
rs 3.52
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
C add() 55 55 16
D get() 0 56 18
B list_reactions() 0 47 11
C remove() 55 55 16

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Reactions 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 Reactions, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/*
4
 * This file is part of Slackify.
5
 *
6
 * (c) Strime <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Strime\Slackify\Api;
13
14
use Strime\Slackify\Exception\RuntimeException;
15
use Strime\Slackify\Exception\InvalidArgumentException;
16
use GuzzleHttp\Exception\RequestException;
17
18
class Reactions extends AbstractApi
19
{
20
    /**
21
     * {@inheritdoc}
22
     *
23
     * @param  string $name
24
     * @param  string $file
25
     * @param  string $file_comment
26
     * @param  string $channel
27
     * @param  float $timestamp
28
     * @return Reactions
29
     */
30 View Code Duplication
    public function add($name, $file = NULL, $file_comment = NULL, $channel = NULL, $timestamp = NULL) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
31
32
        // Check if the type of the variables is valid.
33
        if (!is_string($name)) {
34
            throw new InvalidArgumentException("The type of the name variable is not valid.");
35
        }
36
        if (($file != NULL) && !is_string($file)) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $file of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
37
            throw new InvalidArgumentException("The type of the file variable is not valid.");
38
        }
39
        if (($file_comment != NULL) && !is_string($file_comment)) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $file_comment of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
40
            throw new InvalidArgumentException("The type of the file_comment variable is not valid.");
41
        }
42
        if (($channel != NULL) && !is_string($channel)) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $channel of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
43
            throw new InvalidArgumentException("The type of the channel variable is not valid.");
44
        }
45
        if (($timestamp != NULL) && !is_float($timestamp)) {
46
            throw new InvalidArgumentException("The type of the timestamp variable is not valid.");
47
        }
48
49
        // Set the arguments of the request
50
        $arguments = array(
51
            "name" => $name
52
        );
53
54
        if ($file != NULL) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $file of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
55
            $arguments["file"] = $file;
56
        }
57
        if ($file_comment != NULL) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $file_comment of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
58
            $arguments["file_comment"] = $file_comment;
59
        }
60
        if ($channel != NULL) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $channel of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
61
            $arguments["channel"] = $channel;
62
        }
63
        if ($timestamp != NULL) {
64
            $arguments["timestamp"] = (float)$timestamp;
65
        }
66
67
        $this->setUrl("reactions.add", $arguments);
68
69
        // Send the request
70
        try {
71
            $client = new \GuzzleHttp\Client();
72
            $json_response = $client->request('GET', $this->getUrl(), []);
73
            $response = json_decode( $json_response->getBody() );
74
        }
75
        catch (RequestException $e) {
76
            throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e);
77
        }
78
79
        if($response->{'ok'} === FALSE) {
80
            throw new RuntimeException('The request to the API failed: '.$response->{'error'}.".");
81
        }
82
83
        return $this;
84
    }
85
86
87
88
89
    /**
90
     * {@inheritdoc}
91
     *
92
     * @param  string $file
93
     * @param  string $file_comment
94
     * @param  string $channel
95
     * @param  float $timestamp
96
     * @param  boolean $full
97
     * @return Reactions
98
     */
99
    public function get($file = NULL, $file_comment = NULL, $channel = NULL, $timestamp = NULL, $full = NULL) {
100
101
        // Check if the type of the variables is valid.
102
        if (($file != NULL) && !is_string($file)) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $file of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
103
            throw new InvalidArgumentException("The type of the file variable is not valid.");
104
        }
105
        if (($file_comment != NULL) && !is_string($file_comment)) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $file_comment of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
106
            throw new InvalidArgumentException("The type of the file_comment variable is not valid.");
107
        }
108
        if (($channel != NULL) && !is_string($channel)) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $channel of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
109
            throw new InvalidArgumentException("The type of the channel variable is not valid.");
110
        }
111
        if (($timestamp != NULL) && !is_float($timestamp)) {
112
            throw new InvalidArgumentException("The type of the timestamp variable is not valid.");
113
        }
114
        if (($full != NULL) && !is_bool($full)) {
115
            throw new InvalidArgumentException("The type of the full variable is not valid.");
116
        }
117
118
        // Set the arguments of the request
119
        $arguments = array();
120
121
        if ($file != NULL) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $file of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
122
            $arguments["file"] = $file;
123
        }
124
        if ($file_comment != NULL) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $file_comment of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
125
            $arguments["file_comment"] = $file_comment;
126
        }
127
        if ($channel != NULL) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $channel of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
128
            $arguments["channel"] = $channel;
129
        }
130
        if ($timestamp != NULL) {
131
            $arguments["timestamp"] = (string)$timestamp;
132
        }
133
        if ($full != NULL) {
134
            $arguments["full"] = $full;
135
        }
136
137
        $this->setUrl("reactions.get", $arguments);
138
139
        // Send the request
140
        try {
141
            $client = new \GuzzleHttp\Client();
142
            $json_response = $client->request('GET', $this->getUrl(), []);
143
            $response = json_decode( $json_response->getBody() );
144
        }
145
        catch (RequestException $e) {
146
            throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e);
147
        }
148
149
        if($response->{'ok'} === FALSE) {
150
            throw new RuntimeException('The request to the API failed: '.$response->{'error'}.".");
151
        }
152
153
        return $json_response->getBody();
154
    }
155
156
157
158
159
    /**
160
     * {@inheritdoc}
161
     *
162
     * @param  string $user
163
     * @param  boolean $full
164
     * @param  string $count
165
     * @param  string $page
166
     * @return Reactions
167
     */
168
    public function list_reactions($user = NULL, $full = NULL, $count = 100, $page = 1) {
169
170
        // Check if the type of the variables is valid.
171
        if (($user != NULL) && !is_string($user)) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $user of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
172
            throw new InvalidArgumentException("The type of the user variable is not valid.");
173
        }
174
        if (($full != NULL) && !is_bool($full)) {
175
            throw new InvalidArgumentException("The type of the full variable is not valid.");
176
        }
177
        if (!is_integer($count)) {
178
            throw new InvalidArgumentException("The type of the count variable is not valid.");
179
        }
180
        if (!is_integer($page)) {
181
            throw new InvalidArgumentException("The type of the page variable is not valid.");
182
        }
183
184
        // Set the arguments of the request
185
        $arguments = array(
186
            "count" => $count,
187
            "page" => $page
188
        );
189
190
        if($user != NULL) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $user of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
191
            $arguments["user"] = $user;
192
        }
193
        if($full != NULL) {
194
            $arguments["full"] = $full;
195
        }
196
197
        $this->setUrl("reactions.list");
198
199
        // Send the request
200
        try {
201
            $client = new \GuzzleHttp\Client();
202
            $json_response = $client->request('GET', $this->getUrl(), []);
203
            $response = json_decode( $json_response->getBody() );
204
        }
205
        catch (RequestException $e) {
206
            throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e);
207
        }
208
209
        if($response->{'ok'} === FALSE) {
210
            throw new RuntimeException('The request to the API failed: '.$response->{'error'}.".");
211
        }
212
213
        return $json_response->getBody();
214
    }
215
216
217
218
219
    /**
220
     * {@inheritdoc}
221
     *
222
     * @param  string $name
223
     * @param  string $file
224
     * @param  string $file_comment
225
     * @param  string $channel
226
     * @param  float $timestamp
227
     * @return Reactions
228
     */
229 View Code Duplication
    public function remove($name, $file = NULL, $file_comment = NULL, $channel = NULL, $timestamp = NULL) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
230
231
        // Check if the type of the variables is valid.
232
        if (!is_string($name)) {
233
            throw new InvalidArgumentException("The type of the name variable is not valid.");
234
        }
235
        if (($file != NULL) && !is_string($file)) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $file of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
236
            throw new InvalidArgumentException("The type of the file variable is not valid.");
237
        }
238
        if (($file_comment != NULL) && !is_string($file_comment)) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $file_comment of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
239
            throw new InvalidArgumentException("The type of the file_comment variable is not valid.");
240
        }
241
        if (($channel != NULL) && !is_string($channel)) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $channel of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
242
            throw new InvalidArgumentException("The type of the channel variable is not valid.");
243
        }
244
        if (($timestamp != NULL) && !is_float($timestamp)) {
245
            throw new InvalidArgumentException("The type of the timestamp variable is not valid.");
246
        }
247
248
        // Set the arguments of the request
249
        $arguments = array(
250
            "name" => $name
251
        );
252
253
        if ($file != NULL) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $file of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
254
            $arguments["file"] = $file;
255
        }
256
        if ($file_comment != NULL) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $file_comment of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
257
            $arguments["file_comment"] = $file_comment;
258
        }
259
        if ($channel != NULL) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $channel of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
260
            $arguments["channel"] = $channel;
261
        }
262
        if ($timestamp != NULL) {
263
            $arguments["timestamp"] = (string)$timestamp;
264
        }
265
266
        $this->setUrl("reactions.remove", $arguments);
267
268
        // Send the request
269
        try {
270
            $client = new \GuzzleHttp\Client();
271
            $json_response = $client->request('GET', $this->getUrl(), []);
272
            $response = json_decode( $json_response->getBody() );
273
        }
274
        catch (RequestException $e) {
275
            throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e);
276
        }
277
278
        if($response->{'ok'} === FALSE) {
279
            throw new RuntimeException('The request to the API failed: '.$response->{'error'}.".");
280
        }
281
282
        return $this;
283
    }
284
}
285