Passed
Push — master ( 819f30...8ceff2 )
by Thorsten
02:22
created

Url::equals()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Daikon\Entity\ValueObject;
4
5
use Daikon\Entity\Assert\Assertion;
6
7
final class Url implements ValueObjectInterface
8
{
9
    /**
10
     * @var string
11
     */
12
    private const NIL = '';
13
14
    /**
15
     * @var string
16
     */
17
    private const DEFAULT_PATH = '/';
18
19
    /**
20
     * @var Text
21
     */
22
    private $fragment;
23
24
    /**
25
     * @var Text
26
     */
27
    private $host;
28
29
    /*
30
     * @var Text
31
     */
32
    private $scheme;
33
34
    /**
35
     * @var Text
36
     */
37
    private $query;
38
39
    /**
40
     * @var Integer
41
     */
42
    private $port;
43
44
    /**
45
     * @var Text
46
     */
47
    private $path;
48
49
    /**
50
     * @param string|null $nativeValue
51
     * @return self
52
     */
53 22
    public static function fromNative($nativeValue): self
54
    {
55 22
        Assertion::nullOrString($nativeValue);
56 22
        return empty($nativeValue) ? new self : new self($nativeValue);
57
    }
58
59 4
    public function toNative(): string
60
    {
61 4
        if ($this->host->isEmpty()) {
62 1
            return self::NIL;
63
        }
64 4
        return sprintf(
65 4
            '%s://%s%s%s%s%s',
66 4
            $this->scheme,
67 4
            $this->host,
68 4
            $this->port->toNative() ? ':'.$this->port : '',
0 ignored issues
show
Bug introduced by
The method toNative cannot be called on $this->port (of type integer).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
69 4
            $this->path,
70 4
            $this->query->isEmpty() ? '' : '?'.$this->query,
71 4
            $this->fragment->isEmpty() ? '' : '#'.$this->fragment
72
        );
73
    }
74
75 1
    public function equals(ValueObjectInterface $otherValue): bool
76
    {
77 1
        return $otherValue instanceof self && $otherValue->toNative() === $this->toNative();
78
    }
79
80 1
    public function __toString(): string
81
    {
82 1
        return $this->toNative();
83
    }
84
85 1
    public function getPath(): Text
86
    {
87 1
        return $this->path;
88
    }
89
90 1
    public function getPort(): Integer
91
    {
92 1
        return $this->port;
93
    }
94
95 1
    public function getFragment(): Text
96
    {
97 1
        return $this->fragment;
98
    }
99
100 1
    public function getHost(): Text
101
    {
102 1
        return $this->host;
103
    }
104
105 1
    public function getQuery(): Text
106
    {
107 1
        return $this->query;
108
    }
109
110 1
    public function getScheme(): Text
111
    {
112 1
        return $this->scheme;
113
    }
114
115 22
    private function __construct(string $url = self::NIL)
116
    {
117 22
        $this->host = Text::fromNative(parse_url($url, PHP_URL_HOST) ?? self::NIL);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Daikon\Entity\ValueObje...URL_HOST) ?? self::NIL) of type object<self> is incompatible with the declared type object<Daikon\Entity\ValueObject\Text> of property $host.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
118 22
        $this->scheme = Text::fromNative(parse_url($url, PHP_URL_SCHEME) ?? self::NIL);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Daikon\Entity\ValueObje...L_SCHEME) ?? self::NIL) of type object<self> is incompatible with the declared type object<Daikon\Entity\ValueObject\Text> of property $scheme.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
119 22
        $this->query = Text::fromNative(parse_url($url, PHP_URL_QUERY) ?? self::NIL);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Daikon\Entity\ValueObje...RL_QUERY) ?? self::NIL) of type object<self> is incompatible with the declared type object<Daikon\Entity\ValueObject\Text> of property $query.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
120 22
        $this->port = Integer::fromNative(parse_url($url, PHP_URL_PORT));
0 ignored issues
show
Security Bug introduced by
It seems like parse_url($url, PHP_URL_PORT) targeting parse_url() can also be of type false; however, Daikon\Entity\ValueObject\Integer::fromNative() does only seem to accept integer|null, did you maybe forget to handle an error condition?
Loading history...
Documentation Bug introduced by
It seems like \Daikon\Entity\ValueObje...rl($url, PHP_URL_PORT)) of type object<self> is incompatible with the declared type integer of property $port.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
121 22
        $this->fragment = Text::fromNative(parse_url($url, PHP_URL_FRAGMENT) ?? self::NIL);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Daikon\Entity\ValueObje...FRAGMENT) ?? self::NIL) of type object<self> is incompatible with the declared type object<Daikon\Entity\ValueObject\Text> of property $fragment.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
122 22
        $this->path = Text::fromNative(parse_url($url, PHP_URL_PATH) ?? self::DEFAULT_PATH);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Daikon\Entity\ValueObje... ?? self::DEFAULT_PATH) of type object<self> is incompatible with the declared type object<Daikon\Entity\ValueObject\Text> of property $path.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
123 22
    }
124
}
125