Completed
Push — master ( ba407f...6dbf2d )
by Chris
02:29
created

Service   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 64
Duplicated Lines 17.19 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 11
loc 64
ccs 0
cts 37
cp 0
rs 10
c 0
b 0
f 0
wmc 11
lcom 2
cbo 2

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setTypeId() 0 8 3
A setPort() 11 11 3
A __construct() 0 5 1
A getTypeId() 0 4 1
A getPort() 0 4 1
A getName() 0 8 2

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 declare(strict_types=1);
2
3
namespace DaveRandom\LibLifxLan\DataTypes;
4
5
use DaveRandom\LibLifxLan\Exceptions\InvalidValueException;
6
use const DaveRandom\LibLifxLan\UINT32_MAX;
7
use const DaveRandom\LibLifxLan\UINT32_MIN;
8
9
final class Service
10
{
11
    private $typeId;
12
    private $port;
13
14
    /**
15
     * @param int $typeId
16
     * @throws InvalidValueException
17
     */
18
    private function setTypeId(int $typeId): void
19
    {
20
        if ($typeId < 0 || $typeId > 255) {
21
            throw new InvalidValueException("Service type ID {$typeId} outside allowable range of 0 - 255");
22
        }
23
24
        $this->typeId = $typeId;
25
    }
26
27
    /**
28
     * @param int $port
29
     * @throws InvalidValueException
30
     */
31 View Code Duplication
    private function setPort(int $port): void
1 ignored issue
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...
32
    {
33
        // Protocol spec states this is a uint32 rather than a uint16, so allow any uint32 value at the protocol level
34
        if ($port < UINT32_MIN || $port > UINT32_MAX) {
35
            throw new InvalidValueException(
36
                "Port {$port} outside allowable range of " . UINT32_MIN . " - " . UINT32_MAX
37
            );
38
        }
39
40
        $this->port = $port;
41
    }
42
43
    /**
44
     * @param int $typeId
45
     * @param int $port
46
     * @throws InvalidValueException
47
     */
48
    public function __construct(int $typeId, int $port)
49
    {
50
        $this->setTypeId($typeId);
51
        $this->setPort($port);
52
    }
53
54
    public function getTypeId(): int
55
    {
56
        return $this->typeId;
57
    }
58
59
    public function getPort(): int
60
    {
61
        return $this->port;
62
    }
63
64
    public function getName(): string
65
    {
66
        try {
67
            return ServiceTypes::parseValue($this->typeId);
68
        } catch (\InvalidArgumentException $e) {
69
            return "Unknown({$this->typeId})";
70
        }
71
    }
72
}
73