Passed
Push — master ( 8634b9...ba407f )
by Chris
14:07
created

Service   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 46
Duplicated Lines 34.78 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 16
loc 46
ccs 0
cts 29
cp 0
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 16 16 5
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
     * @param int $port
17
     * @throws InvalidValueException
18
     */
19 View Code Duplication
    public function __construct(int $typeId, int $port)
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...
20
    {
21
        if ($typeId < 0 || $typeId > 255) {
22
            throw new InvalidValueException("Service type ID {$typeId} outside allowable range of 0 - 255");
23
        }
24
25
        // Protocol spec states this is a uint32 rather than a uint16, so allow any uint32 value at the protocol level
26
        if ($port < UINT32_MIN || $port > UINT32_MAX) {
27
            throw new InvalidValueException(
28
                "Port {$port} outside allowable range of " . UINT32_MIN . " - " . UINT32_MAX
29
            );
30
        }
31
32
        $this->typeId = $typeId;
33
        $this->port = $port;
34
    }
35
36
    public function getTypeId(): int
37
    {
38
        return $this->typeId;
39
    }
40
41
    public function getPort(): int
42
    {
43
        return $this->port;
44
    }
45
46
    public function getName(): string
47
    {
48
        try {
49
            return ServiceTypes::parseValue($this->typeId);
50
        } catch (\InvalidArgumentException $e) {
51
            return "Unknown({$this->typeId})";
52
        }
53
    }
54
}
55