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.

Bulb   A
last analyzed

Complexity

Total Complexity 27

Size/Duplication

Total Lines 534
Duplicated Lines 32.4 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
wmc 27
lcom 2
cbo 3
dl 173
loc 534
rs 10
c 0
b 0
f 0

24 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A getAddress() 0 4 1
A getIp() 0 4 1
A getPort() 0 4 1
A getProp() 0 11 1
A getId() 0 4 1
A send() 0 5 1
A read() 0 15 2
A setCtAbx() 15 15 1
A setRgb() 15 15 1
A setHsv() 16 16 1
A setBright() 15 15 1
A setPower() 15 15 1
A toggle() 11 11 1
A setDefault() 11 11 1
A startCf() 0 18 1
A stopCf() 11 11 1
A setScene() 11 11 1
A cronAdd() 0 14 1
A cronGet() 13 13 1
A cronDel() 13 13 1
A setAdjust() 14 14 1
A setMusic() 0 23 3
A setName() 13 13 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
3
namespace Yeelight\Bulb;
4
5
use React\Promise\Promise;
6
use Socket\Raw\Socket;
7
8
class Bulb
9
{
10
    const PACKET_LENGTH = 4096;
11
    const NO_FLAG = 0;
12
13
    const EFFECT_SUDDEN = 'sudden';
14
    const EFFECT_SMOOTH = 'smooth';
15
    const ON = 'on';
16
    const OFF = 'off';
17
    const ACTION_BEFORE = 0;
18
    const ACTION_AFTER = 1;
19
    const ACTION_TURN_OFF = 2;
20
    const ADJUST_ACTION_INCREASE = 'increase';
21
    const ADJUST_ACTION_DECREASE = 'decrease';
22
    const ADJUST_ACTION_CIRCLE = 'circle';
23
    const ADJUST_PROP_BRIGHTNESS = 'bright';
24
    const ADJUST_PROP_COLOR_TEMP = 'ct';
25
    const ADJUST_PROP_COLOR = 'color';
26
27
    /**
28
     * @var Socket
29
     */
30
    private $socket;
31
32
    /**
33
     * @var string
34
     */
35
    private $ip;
36
37
    /**
38
     * @var int
39
     */
40
    private $port;
41
42
    /**
43
     * @var string
44
     */
45
    private $id;
46
47
    /**
48
     * Bulb constructor.
49
     *
50
     * @param Socket $socket
51
     * @param string $ip
52
     * @param int    $port
53
     * @param string $id
54
     */
55
    public function __construct(Socket $socket, string $ip, int $port, string $id)
56
    {
57
        $this->socket = $socket;
58
        $this->ip = $ip;
59
        $this->port = $port;
60
        $this->id = $id;
61
62
        $this->socket->connect($this->getAddress());
63
    }
64
65
    /**
66
     * @return string
67
     */
68
    public function getAddress(): string
69
    {
70
        return sprintf('%s:%d', $this->getIp(), $this->getPort());
71
    }
72
73
    /**
74
     * @return string
75
     */
76
    public function getIp(): string
77
    {
78
        return $this->ip;
79
    }
80
81
    /**
82
     * @return int
83
     */
84
    public function getPort(): int
85
    {
86
        return $this->port;
87
    }
88
89
    /**
90
     * This method is used to retrieve current property of smart LED
91
     *
92
     * @param array $properties The parameter is a list of property (consts from BulbProperties) names and the response
93
     *                          contains a list of corresponding property values. If the requested property name is not
94
     *                          recognized by smart LED, then a empty string value ("") will be returned
95
     *
96
     * @return Promise
97
     */
98
    public function getProp(array $properties)
99
    {
100
        $data = [
101
            'id' => hexdec($this->getId()),
102
            'method' => 'get_prop',
103
            'params' => $properties,
104
        ];
105
        $this->send($data);
106
107
        return $this->read();
108
    }
109
110
    /**
111
     * @return string
112
     */
113
    public function getId(): string
114
    {
115
        return $this->id;
116
    }
117
118
    /**
119
     * @param array $data
120
     */
121
    private function send(array $data)
122
    {
123
        $data = json_encode($data) . "\r\n";
124
        $this->socket->send($data, self::NO_FLAG);
125
    }
126
127
    /**
128
     * @return Promise
129
     */
130
    private function read(): Promise
131
    {
132
        return new Promise(function (callable $resolve, callable $reject) {
133
            $response = new Response(
134
                json_decode($this->socket->read(self::PACKET_LENGTH), true)
135
            );
136
137
            if ($response->isSuccess()) {
138
                $resolve($response);
139
140
                return;
141
            }
142
            $reject($response->getException());
143
        });
144
    }
145
146
    /**
147
     * This method is used to change the color temperature of a smart LED
148
     *
149
     * @param int    $ctValue  is the target color temperature
150
     * @param string $effect   support two values: "sudden" (Bulb::EFFECT_SUDDEN) and "smooth" (Bulb::EFFECT_SMOOTH)
151
     * @param int    $duration specifies the total time of the gradual changing. The unit is milliseconds
152
     *
153
     * @return Promise
154
     */
155 View Code Duplication
    public function setCtAbx(int $ctValue, string $effect, int $duration)
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...
156
    {
157
        $data = [
158
            'id' => hexdec($this->getId()),
159
            'method' => 'set_ct_abx',
160
            'params' => [
161
                $ctValue,
162
                $effect,
163
                $duration,
164
            ],
165
        ];
166
        $this->send($data);
167
168
        return $this->read();
169
    }
170
171
    /**
172
     * This method is used to change the color of a smart LED
173
     *
174
     * @param int    $rgbValue is the target color, whose type is integer. It should be expressed in decimal integer
175
     *                         ranges from 0 to 16777215 (hex: 0xFFFFFF).
176
     * @param string $effect   support two values: "sudden" (Bulb::EFFECT_SUDDEN) and "smooth" (Bulb::EFFECT_SMOOTH)
177
     * @param int    $duration specifies the total time of the gradual changing. The unit is milliseconds
178
     *
179
     * @return Promise
180
     */
181 View Code Duplication
    public function setRgb(int $rgbValue, string $effect, int $duration)
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...
182
    {
183
        $data = [
184
            'id' => hexdec($this->getId()),
185
            'method' => 'set_rgb',
186
            'params' => [
187
                $rgbValue,
188
                $effect,
189
                $duration,
190
            ],
191
        ];
192
        $this->send($data);
193
194
        return $this->read();
195
    }
196
197
    /**
198
     * This method is used to change the color of a smart LED
199
     *
200
     * @param int    $hue      is the target hue value, whose type is integer
201
     * @param int    $sat      is the target saturation value whose type is integer
202
     * @param string $effect   support two values: "sudden" (Bulb::EFFECT_SUDDEN) and "smooth" (Bulb::EFFECT_SMOOTH)
203
     * @param int    $duration specifies the total time of the gradual changing. The unit is milliseconds
204
     *
205
     * @return Promise
206
     */
207 View Code Duplication
    public function setHsv(int $hue, int $sat, string $effect, int $duration)
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...
208
    {
209
        $data = [
210
            'id' => hexdec($this->getId()),
211
            'method' => 'set_hsv',
212
            'params' => [
213
                $hue,
214
                $sat,
215
                $effect,
216
                $duration,
217
            ],
218
        ];
219
        $this->send($data);
220
221
        return $this->read();
222
    }
223
224
    /**
225
     * This method is used to change the brightness of a smart LED
226
     *
227
     * @param int    $brightness is the target brightness. The type is integer and ranges from 1 to 100. The brightness
228
     *                           is a percentage instead of a absolute value.
229
     * @param string $effect     support two values: "sudden" (Bulb::EFFECT_SUDDEN) and "smooth" (Bulb::EFFECT_SMOOTH)
230
     * @param int    $duration   specifies the total time of the gradual changing. The unit is milliseconds
231
     *
232
     * @return Promise
233
     */
234 View Code Duplication
    public function setBright(int $brightness, string $effect, int $duration)
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...
235
    {
236
        $data = [
237
            'id' => hexdec($this->getId()),
238
            'method' => 'set_bright',
239
            'params' => [
240
                $brightness,
241
                $effect,
242
                $duration,
243
            ],
244
        ];
245
        $this->send($data);
246
247
        return $this->read();
248
    }
249
250
    /**
251
     * This method is used to switch on or off the smart LED (software managed on/off)
252
     *
253
     * @param string $power    can only be "on" or "off". "on" means turn on the smart LED, "off" means turn off the
254
     *                         smart LED
255
     * @param string $effect   support two values: "sudden" (Bulb::EFFECT_SUDDEN) and "smooth" (Bulb::EFFECT_SMOOTH)
256
     * @param int    $duration specifies the total time of the gradual changing. The unit is milliseconds
257
     *
258
     * @return Promise
259
     */
260 View Code Duplication
    public function setPower(string $power, string $effect, int $duration)
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...
261
    {
262
        $data = [
263
            'id' => hexdec($this->getId()),
264
            'method' => 'set_power',
265
            'params' => [
266
                $power,
267
                $effect,
268
                $duration,
269
            ],
270
        ];
271
        $this->send($data);
272
273
        return $this->read();
274
    }
275
276
    /**
277
     * This method is used to toggle the smart LED
278
     *
279
     * @return Promise
280
     */
281 View Code Duplication
    public function toggle()
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...
282
    {
283
        $data = [
284
            'id' => hexdec($this->getId()),
285
            'method' => 'toggle',
286
            'params' => [],
287
        ];
288
        $this->send($data);
289
290
        return $this->read();
291
    }
292
293
    /**
294
     * This method is used to save current state of smart LED in persistent memory. So if user powers off and then
295
     * powers on the smart LED again (hard power reset), the smart LED will show last saved state
296
     *
297
     * @return Promise
298
     */
299 View Code Duplication
    public function setDefault()
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...
300
    {
301
        $data = [
302
            'id' => hexdec($this->getId()),
303
            'method' => 'set_default',
304
            'params' => [],
305
        ];
306
        $this->send($data);
307
308
        return $this->read();
309
    }
310
311
    /**
312
     * This method is used to start a color flow. Color flow is a series of smart LED visible state changing. It can be
313
     * brightness changing, color changing or color temperature changing
314
     *
315
     * @param int   $count              is the total number of visible state changing before color flow stopped. 0
316
     *                                  means infinite loop on the state changing
317
     * @param int   $action             is the action taken after the flow is stopped
318
     *                                  0 means smart LED recover to the state before the color flow started
319
     *                                  1 means smart LED stay at the state when the flow is stopped
320
     *                                  2 means turn off the smart LED after the flow is stopped
321
     * @param array $flowExpression     is the expression of the state changing series in format
322
     *                                  [
323
     *                                  [duration, mode, value, brightness],
324
     *                                  [duration, mode, value, brightness]
325
     *                                  ]
326
     *
327
     * @return Promise
328
     */
329
    public function startCf(int $count, int $action, array $flowExpression)
330
    {
331
        $state = implode(",", array_map(function ($item) {
332
            return implode(",", $item);
333
        }, $flowExpression));
334
        $data = [
335
            'id' => hexdec($this->getId()),
336
            'method' => 'start_cf',
337
            'params' => [
338
                $count,
339
                $action,
340
                $state,
341
            ],
342
        ];
343
        $this->send($data);
344
345
        return $this->read();
346
    }
347
348
    /**
349
     * This method is used to stop a running color flow
350
     *
351
     * @return Promise
352
     */
353 View Code Duplication
    public function stopCf()
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...
354
    {
355
        $data = [
356
            'id' => hexdec($this->getId()),
357
            'method' => 'stop_cf',
358
            'params' => [],
359
        ];
360
        $this->send($data);
361
362
        return $this->read();
363
    }
364
365
    /**
366
     * This method is used to set the smart LED directly to specified state. If the smart LED is off, then it will turn
367
     * on the smart LED firstly and then apply the specified command
368
     *
369
     * @param array $params array that firs element is a class (color, hsv, ct, cf, auto_dealy_off) and next 3 are
370
     *                      class specific eg.
371
     *                      ['color', 65280, 70]
372
     *                      ['hsv', 300, 70, 100]
373
     *                      ['ct', 5400, 100]
374
     *                      ['cf',0,0,"500,1,255,100,1000,1,16776960,70"]
375
     *                      ['auto_delay_off', 50, 5]
376
     *
377
     * @return Promise
378
     */
379 View Code Duplication
    public function setScene(array $params)
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...
380
    {
381
        $data = [
382
            'id' => hexdec($this->getId()),
383
            'method' => 'set_scene',
384
            'params' => $params,
385
        ];
386
        $this->send($data);
387
388
        return $this->read();
389
    }
390
391
    /**
392
     * This method is used to start a timer job on the smart LED
393
     *
394
     * @param int $type  type of the cron job
395
     * @param int $value length of the timer (in minutes)
396
     *
397
     * @return Promise
398
     */
399
    public function cronAdd(int $type, int $value)
400
    {
401
        $data = [
402
            'id' => hexdec($this->getId()),
403
            'method' => 'cron_add',
404
            'params' => [
405
                $type,
406
                $value,
407
            ],
408
        ];
409
        $this->send($data);
410
411
        return $this->read();
412
    }
413
414
    /**
415
     * This method is used to retrieve the setting of the current cron job of the specified type
416
     *
417
     * @param int $type type of the cron job
418
     *
419
     * @return Promise
420
     */
421 View Code Duplication
    public function cronGet(int $type)
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...
422
    {
423
        $data = [
424
            'id' => hexdec($this->getId()),
425
            'method' => 'cron_get',
426
            'params' => [
427
                $type,
428
            ],
429
        ];
430
        $this->send($data);
431
432
        return $this->read();
433
    }
434
435
    /**
436
     * This method is used to stop the specified cron job
437
     *
438
     * @param int $type type of the cron job
439
     *
440
     * @return Promise
441
     */
442 View Code Duplication
    public function cronDel(int $type)
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...
443
    {
444
        $data = [
445
            'id' => hexdec($this->getId()),
446
            'method' => 'cron_del',
447
            'params' => [
448
                $type,
449
            ],
450
        ];
451
        $this->send($data);
452
453
        return $this->read();
454
    }
455
456
    /**
457
     * This method is used to change brightness, CT or color of a smart LED without knowing the current value, it's
458
     * main used by controllers.
459
     *
460
     * @param string $action the direction of the adjustment The valid value can be:
461
     *                       “increase": increase the specified property (Bulb::ADJUST_ACTION_INCREASE)
462
     *                       “decrease": decrease the specified property (Bulb::ADJUST_ACTION_DECREASE)
463
     *                       “circle": increase the specified property, after it reaches the max
464
     *                       (Bulb::ADJUST_ACTION_CIRCLE)
465
     * @param string $prop   the property to adjust. The valid value can be:
466
     *                       “bright": adjust brightness (Bulb::ADJUST_PROP_BRIGHTNESS)
467
     *                       “ct": adjust color temperature (Bulb::ADJUST_PROP_COLOR_TEMP)
468
     *                       “color": adjust color. (Bulb::ADJUST_PROP_COLOR) (When “prop" is “color", the “action" can
469
     *                       only be “circle", otherwise, it will be deemed as invalid request.)
470
     *
471
     * @return Promise
472
     */
473 View Code Duplication
    public function setAdjust(string $action, string $prop)
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...
474
    {
475
        $data = [
476
            'id' => hexdec($this->getId()),
477
            'method' => 'set_adjust',
478
            'params' => [
479
                $action,
480
                $prop,
481
            ],
482
        ];
483
        $this->send($data);
484
485
        return $this->read();
486
    }
487
488
    /**
489
     * This method is used to start or stop music mode on a device
490
     *
491
     * @param int         $action the action of set_music command
492
     * @param string|null $host   the IP address of the music server
493
     * @param int|null    $port   the TCP port music application is listening on
494
     *
495
     * @return Promise
496
     */
497
    public function setMusic(int $action, string $host = null, int $port = null)
498
    {
499
        $params = [
500
            $action,
501
        ];
502
503
        if (!is_null($host)) {
504
            $params[] = $host;
505
        }
506
507
        if (!is_null($port)) {
508
            $params[] = $port;
509
        }
510
511
        $data = [
512
            'id' => hexdec($this->getId()),
513
            'method' => 'set_music',
514
            'params' => $params,
515
        ];
516
        $this->send($data);
517
518
        return $this->read();
519
    }
520
521
    /**
522
     * This method is used to name the device
523
     *
524
     * @param string $name name of the device
525
     *
526
     * @return Promise
527
     */
528 View Code Duplication
    public function setName(string $name)
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...
529
    {
530
        $data = [
531
            'id' => hexdec($this->getId()),
532
            'method' => 'set_name',
533
            'params' => [
534
                $name,
535
            ],
536
        ];
537
        $this->send($data);
538
539
        return $this->read();
540
    }
541
}
542