Completed
Push — master ( c32e20...5b9784 )
by Alejandro
15s
created

IpAddress   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 95.83%

Importance

Changes 0
Metric Value
dl 0
loc 67
rs 10
c 0
b 0
f 0
ccs 23
cts 24
cp 0.9583
wmc 5
lcom 1
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 9 1
A __construct() 0 7 1
A fromString() 0 10 2
A getObfuscatedCopy() 0 9 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Shlinkio\Shlink\Common\Util;
5
6
use Shlinkio\Shlink\Common\Exception\WrongIpException;
7
8
final class IpAddress
9
{
10
    private const IPV4_PARTS_COUNT = 4;
11
    private const OBFUSCATED_OCTET = '0';
12
    public const LOCALHOST = '127.0.0.1';
13
14
    /**
15
     * @var string
16
     */
17
    private $firstOctet;
18
    /**
19
     * @var string
20
     */
21
    private $secondOctet;
22
    /**
23
     * @var string
24
     */
25
    private $thirdOctet;
26
    /**
27
     * @var string
28
     */
29
    private $fourthOctet;
30
31 4
    private function __construct(string $firstOctet, string $secondOctet, string $thirdOctet, string $fourthOctet)
32
    {
33 4
        $this->firstOctet = $firstOctet;
34 4
        $this->secondOctet = $secondOctet;
35 4
        $this->thirdOctet = $thirdOctet;
36 4
        $this->fourthOctet = $fourthOctet;
37 4
    }
38
39
    /**
40
     * @param string $address
41
     * @return IpAddress
42
     * @throws WrongIpException
43
     */
44 4
    public static function fromString(string $address): self
45
    {
46 4
        $address = \trim($address);
47 4
        $parts = \explode('.', $address);
48 4
        if (\count($parts) !== self::IPV4_PARTS_COUNT) {
49
            throw WrongIpException::fromIpAddress($address);
50
        }
51
52 4
        return new self(...$parts);
0 ignored issues
show
Bug introduced by
The call to IpAddress::__construct() misses some required arguments starting with $secondOctet.
Loading history...
Documentation introduced by
$parts is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
53
    }
54
55 4
    public function getObfuscatedCopy(): self
56
    {
57 4
        return new self(
58 4
            $this->firstOctet,
59 4
            $this->secondOctet,
60 4
            $this->thirdOctet,
61 4
            self::OBFUSCATED_OCTET
62
        );
63
    }
64
65 4
    public function __toString(): string
66
    {
67 4
        return \implode('.', [
68 4
            $this->firstOctet,
69 4
            $this->secondOctet,
70 4
            $this->thirdOctet,
71 4
            $this->fourthOctet,
72
        ]);
73
    }
74
}
75