Completed
Push — master ( 3fc019...d199f8 )
by Dawid
05:22
created

Definition::getSince()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiechu\SymfonyCommonsBundle\ApiFeature;
6
7
class Definition implements \JsonSerializable
8
{
9
    /**
10
     * @var string
11
     */
12
    protected $name;
13
14
    /**
15
     * @var null|string
16
     */
17
    protected $since;
18
19
    /**
20
     * @var null|string
21
     */
22
    protected $until;
23
24
    /**
25
     * @param string      $name
26
     * @param null|string $since
27
     * @param null|string $until
28
     *
29
     * @throws \InvalidArgumentException
30
     */
31 10
    public function __construct(string $name, ?string $since, ?string $until)
32
    {
33 10
        if ($since !== null && !is_numeric($since)) {
34
            throw new \InvalidArgumentException('Since parameter is not numeric');
35
        }
36 10
        if ($until !== null && !is_numeric($until)) {
37
            throw new \InvalidArgumentException('Since parameter is not numeric');
38
        }
39
40 10
        if ($since === null && $until === null) {
41
            throw new \InvalidArgumentException('No version constraints provided');
42
        }
43
44 10
        $this->name = $name;
45 10
        $this->since = $since;
0 ignored issues
show
Documentation Bug introduced by
It seems like $since can also be of type integer or double. However, the property $since 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...
46 10
        $this->until = $until;
0 ignored issues
show
Documentation Bug introduced by
It seems like $until can also be of type integer or double. However, the property $until 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...
47 10
    }
48
49 1
    public function __toString(): string
50
    {
51 1
        return $this->name;
52
    }
53
54
    /**
55
     * @return string
56
     */
57 3
    public function getName(): string
58
    {
59 3
        return $this->name;
60
    }
61
62
    /**
63
     * @return null|string
64
     */
65
    public function getSince(): ?string
66
    {
67
        return $this->since;
68
    }
69
70
    /**
71
     * @return null|string
72
     */
73
    public function getUntil(): ?string
74
    {
75
        return $this->until;
76
    }
77
78
    /**
79
     * @param string $version
80
     *
81
     * @return bool
82
     */
83 3
    public function isAvailableForVersion(string $version): bool
84
    {
85 3
        $sinceVersionMatch = $this->since === null || version_compare($version, $this->since, '>=');
86 3
        $untilVersionMatch = $this->until === null || version_compare($version, $this->until, '<=');
87
88 3
        return $sinceVersionMatch && $untilVersionMatch;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94 2
    public function jsonSerialize(): array
95
    {
96
        return [
97 2
            'name' => $this->name,
98 2
            'since' => $this->since,
99 2
            'until' => $this->until,
100
        ];
101
    }
102
}
103