Passed
Pull Request — master (#500)
by Alejandro
06:23
created

ShortUrlMeta::getDomain()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace Shlinkio\Shlink\Core\Model;
5
6
use Cake\Chronos\Chronos;
7
use Shlinkio\Shlink\Core\Exception\ValidationException;
8
use Shlinkio\Shlink\Core\Validation\ShortUrlMetaInputFilter;
9
10
final class ShortUrlMeta
11
{
12
    /** @var Chronos|null */
13
    private $validSince;
14
    /** @var Chronos|null */
15
    private $validUntil;
16
    /** @var string|null */
17
    private $customSlug;
18
    /** @var int|null */
19
    private $maxVisits;
20
    /** @var bool|null */
21
    private $findIfExists;
22
    /** @var string|null */
23
    private $domain;
24
25
    // Force named constructors
26
    private function __construct()
27
    {
28
    }
29
30 46
    public static function createEmpty(): self
31
    {
32 46
        return new self();
33
    }
34
35
    /**
36
     * @param array $data
37
     * @throws ValidationException
38
     */
39 8
    public static function createFromRawData(array $data): self
40
    {
41 8
        $instance = new self();
42 8
        $instance->validate($data);
43 5
        return $instance;
44
    }
45
46
    /**
47
     * @param string|Chronos|null $validSince
48
     * @param string|Chronos|null $validUntil
49
     * @param string|null $customSlug
50
     * @param int|null $maxVisits
51
     * @param bool|null $findIfExists
52
     * @param string|null $domain
53
     * @throws ValidationException
54
     */
55 9
    public static function createFromParams(
56
        $validSince = null,
57
        $validUntil = null,
58
        $customSlug = null,
59
        $maxVisits = null,
60
        $findIfExists = null,
61
        $domain = null
62
    ): self {
63
        // We do not type hint the arguments because that will be done by the validation process and we would get a
64
        // type error if any of them do not match
65 9
        $instance = new self();
66 9
        $instance->validate([
67 9
            ShortUrlMetaInputFilter::VALID_SINCE => $validSince,
68 9
            ShortUrlMetaInputFilter::VALID_UNTIL => $validUntil,
69 9
            ShortUrlMetaInputFilter::CUSTOM_SLUG => $customSlug,
70 9
            ShortUrlMetaInputFilter::MAX_VISITS => $maxVisits,
71 9
            ShortUrlMetaInputFilter::FIND_IF_EXISTS => $findIfExists,
72 9
            ShortUrlMetaInputFilter::DOMAIN => $domain,
73
        ]);
74 9
        return $instance;
75
    }
76
77
    /**
78
     * @param array $data
79
     * @throws ValidationException
80
     */
81 16
    private function validate(array $data): void
82
    {
83 16
        $inputFilter = new ShortUrlMetaInputFilter($data);
84 16
        if (! $inputFilter->isValid()) {
85 3
            throw ValidationException::fromInputFilter($inputFilter);
86
        }
87
88 13
        $this->validSince = $this->parseDateField($inputFilter->getValue(ShortUrlMetaInputFilter::VALID_SINCE));
0 ignored issues
show
Bug introduced by
It seems like $inputFilter->getValue(S...putFilter::VALID_SINCE) can also be of type array; however, parameter $date of Shlinkio\Shlink\Core\Mod...lMeta::parseDateField() does only seem to accept Cake\Chronos\Chronos|null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

88
        $this->validSince = $this->parseDateField(/** @scrutinizer ignore-type */ $inputFilter->getValue(ShortUrlMetaInputFilter::VALID_SINCE));
Loading history...
89 13
        $this->validUntil = $this->parseDateField($inputFilter->getValue(ShortUrlMetaInputFilter::VALID_UNTIL));
90 13
        $this->customSlug = $inputFilter->getValue(ShortUrlMetaInputFilter::CUSTOM_SLUG);
0 ignored issues
show
Documentation Bug introduced by
It seems like $inputFilter->getValue(S...putFilter::CUSTOM_SLUG) can also be of type array. However, the property $customSlug is declared as type null|string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
91 13
        $this->maxVisits = $inputFilter->getValue(ShortUrlMetaInputFilter::MAX_VISITS);
0 ignored issues
show
Documentation Bug introduced by
It seems like $inputFilter->getValue(S...nputFilter::MAX_VISITS) can also be of type array. However, the property $maxVisits is declared as type integer|null. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
92 13
        $this->maxVisits = $this->maxVisits !== null ? (int) $this->maxVisits : null;
93 13
        $this->findIfExists = $inputFilter->getValue(ShortUrlMetaInputFilter::FIND_IF_EXISTS);
0 ignored issues
show
Documentation Bug introduced by
It seems like $inputFilter->getValue(S...Filter::FIND_IF_EXISTS) can also be of type array. However, the property $findIfExists is declared as type boolean|null. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
94 13
        $this->domain = $inputFilter->getValue(ShortUrlMetaInputFilter::DOMAIN);
0 ignored issues
show
Documentation Bug introduced by
It seems like $inputFilter->getValue(S...etaInputFilter::DOMAIN) can also be of type array. However, the property $domain is declared as type null|string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
95
    }
96
97
    /**
98
     * @param string|Chronos|null $date
99
     */
100 13
    private function parseDateField($date): ?Chronos
101
    {
102 13
        if ($date === null || $date instanceof Chronos) {
103 12
            return $date;
104
        }
105
106 2
        return Chronos::parse($date);
107
    }
108
109 48
    public function getValidSince(): ?Chronos
110
    {
111 48
        return $this->validSince;
112
    }
113
114 10
    public function hasValidSince(): bool
115
    {
116 10
        return $this->validSince !== null;
117
    }
118
119 49
    public function getValidUntil(): ?Chronos
120
    {
121 49
        return $this->validUntil;
122
    }
123
124 10
    public function hasValidUntil(): bool
125
    {
126 10
        return $this->validUntil !== null;
127
    }
128
129 50
    public function getCustomSlug(): ?string
130
    {
131 50
        return $this->customSlug;
132
    }
133
134 13
    public function hasCustomSlug(): bool
135
    {
136 13
        return $this->customSlug !== null;
137
    }
138
139 49
    public function getMaxVisits(): ?int
140
    {
141 49
        return $this->maxVisits;
142
    }
143
144 10
    public function hasMaxVisits(): bool
145
    {
146 10
        return $this->maxVisits !== null;
147
    }
148
149 13
    public function findIfExists(): bool
150
    {
151 13
        return (bool) $this->findIfExists;
152
    }
153
154 47
    public function getDomain(): ?string
155
    {
156 47
        return $this->domain;
157
    }
158
}
159