Completed
Push — master ( 1de050...162d05 )
by Alejandro
46:23 queued 09:21
created

IpAddress::getObfuscatedCopy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
ccs 6
cts 6
cp 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Shlinkio\Shlink\Common\Util;
5
6
use Shlinkio\Shlink\Common\Exception\WrongIpException;
7
use function count;
8
use function explode;
9
use function implode;
10
use function trim;
11
12
final class IpAddress
13
{
14
    private const IPV4_PARTS_COUNT = 4;
15
    private const OBFUSCATED_OCTET = '0';
16
    public const LOCALHOST = '127.0.0.1';
17
18
    /**
19
     * @var string
20
     */
21
    private $firstOctet;
22
    /**
23
     * @var string
24
     */
25
    private $secondOctet;
26
    /**
27
     * @var string
28
     */
29
    private $thirdOctet;
30
    /**
31
     * @var string
32
     */
33
    private $fourthOctet;
34
35 4
    private function __construct(string $firstOctet, string $secondOctet, string $thirdOctet, string $fourthOctet)
36
    {
37 4
        $this->firstOctet = $firstOctet;
38 4
        $this->secondOctet = $secondOctet;
39 4
        $this->thirdOctet = $thirdOctet;
40 4
        $this->fourthOctet = $fourthOctet;
41 4
    }
42
43
    /**
44
     * @param string $address
45
     * @return IpAddress
46
     * @throws WrongIpException
47
     */
48 4
    public static function fromString(string $address): self
49
    {
50 4
        $address = trim($address);
51 4
        $parts = explode('.', $address);
52 4
        if (count($parts) !== self::IPV4_PARTS_COUNT) {
53 1
            throw WrongIpException::fromIpAddress($address);
54
        }
55
56 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...
57
    }
58
59 4
    public function getObfuscatedCopy(): self
60
    {
61 4
        return new self(
62 4
            $this->firstOctet,
63 4
            $this->secondOctet,
64 4
            $this->thirdOctet,
65 4
            self::OBFUSCATED_OCTET
66
        );
67
    }
68
69 4
    public function __toString(): string
70
    {
71 4
        return implode('.', [
72 4
            $this->firstOctet,
73 4
            $this->secondOctet,
74 4
            $this->thirdOctet,
75 4
            $this->fourthOctet,
76
        ]);
77
    }
78
}
79