Passed
Pull Request — master (#343)
by Alejandro
05:15
created

ShortUrlMeta::findIfExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 0
cts 2
cp 0
crap 2
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
use function is_string;
10
11
final class ShortUrlMeta
12
{
13
    /** @var Chronos|null */
14
    private $validSince;
15
    /** @var Chronos|null */
16
    private $validUntil;
17
    /** @var string|null */
18
    private $customSlug;
19
    /** @var int|null */
20
    private $maxVisits;
21
    /** @var bool */
22
    private $findIfExists;
23
24
    // Force named constructors
25
    private function __construct()
26
    {
27
    }
28
29 40
    public static function createEmpty(): self
30
    {
31 40
        return new self();
32
    }
33
34
    /**
35
     * @param array $data
36
     * @throws ValidationException
37
     */
38 8
    public static function createFromRawData(array $data): self
39
    {
40 8
        $instance = new self();
41 8
        $instance->validate($data);
42 5
        return $instance;
43
    }
44
45
    /**
46
     * @param string|Chronos|null $validSince
47
     * @param string|Chronos|null $validUntil
48
     * @param string|null $customSlug
49
     * @param int|null $maxVisits
50
     * @param bool|null $findIfExists
51
     * @throws ValidationException
52
     */
53 9
    public static function createFromParams(
54
        $validSince = null,
55
        $validUntil = null,
56
        $customSlug = null,
57
        $maxVisits = null,
58
        $findIfExists = null
59
    ): self {
60
        // We do not type hint the arguments because that will be done by the validation process and we would get a
61
        // type error if any of them do not match
62 9
        $instance = new self();
63 9
        $instance->validate([
64 9
            ShortUrlMetaInputFilter::VALID_SINCE => $validSince,
65 9
            ShortUrlMetaInputFilter::VALID_UNTIL => $validUntil,
66 9
            ShortUrlMetaInputFilter::CUSTOM_SLUG => $customSlug,
67 9
            ShortUrlMetaInputFilter::MAX_VISITS => $maxVisits,
68 9
            ShortUrlMetaInputFilter::FIND_IF_EXISTS => $findIfExists,
69
        ]);
70 9
        return $instance;
71
    }
72
73
    /**
74
     * @param array $data
75
     * @throws ValidationException
76
     */
77 16
    private function validate(array $data): void
78
    {
79 16
        $inputFilter = new ShortUrlMetaInputFilter($data);
80 16
        if (! $inputFilter->isValid()) {
81 3
            throw ValidationException::fromInputFilter($inputFilter);
82
        }
83
84 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

84
        $this->validSince = $this->parseDateField(/** @scrutinizer ignore-type */ $inputFilter->getValue(ShortUrlMetaInputFilter::VALID_SINCE));
Loading history...
85 13
        $this->validUntil = $this->parseDateField($inputFilter->getValue(ShortUrlMetaInputFilter::VALID_UNTIL));
86 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...
87 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...
88 13
        $this->maxVisits = $this->maxVisits !== null ? (int) $this->maxVisits : null;
89 13
        $this->findIfExists = (bool) $inputFilter->getValue(ShortUrlMetaInputFilter::FIND_IF_EXISTS);
90
    }
91
92
    /**
93
     * @param string|Chronos|null $date
94
     * @return Chronos|null
95
     */
96 13
    private function parseDateField($date): ?Chronos
97
    {
98 13
        if ($date === null || $date instanceof Chronos) {
99 12
            return $date;
100
        }
101
102 2
        if (is_string($date)) {
0 ignored issues
show
introduced by
The condition is_string($date) is always true.
Loading history...
103 2
            return Chronos::parse($date);
104
        }
105
106
        return null;
107
    }
108
109 41
    public function getValidSince(): ?Chronos
110
    {
111 41
        return $this->validSince;
112
    }
113
114 2
    public function hasValidSince(): bool
115
    {
116 2
        return $this->validSince !== null;
117
    }
118
119 41
    public function getValidUntil(): ?Chronos
120
    {
121 41
        return $this->validUntil;
122
    }
123
124 2
    public function hasValidUntil(): bool
125
    {
126 2
        return $this->validUntil !== null;
127
    }
128
129 43
    public function getCustomSlug(): ?string
130
    {
131 43
        return $this->customSlug;
132
    }
133
134 5
    public function hasCustomSlug(): bool
135
    {
136 5
        return $this->customSlug !== null;
137
    }
138
139 41
    public function getMaxVisits(): ?int
140
    {
141 41
        return $this->maxVisits;
142
    }
143
144 2
    public function hasMaxVisits(): bool
145
    {
146 2
        return $this->maxVisits !== null;
147
    }
148
149
    public function findIfExists(): bool
150
    {
151
        return $this->findIfExists;
152
    }
153
154 1
    public function withCustomSlug(string $customSlug): self
155
    {
156 1
        $clone = clone $this;
157 1
        $clone->customSlug = $customSlug;
158
159 1
        return $clone;
160
    }
161
}
162