Completed
Push — master ( 79451c...b20886 )
by Dawid
04:40
created

Definition::isAvailableForVersion()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 4
nc 8
nop 1
crap 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiechu\SymfonyCommonsBundle\ApiFeature;
6
7
class Definition
8
{
9
    /**
10
     * @var string
11
     */
12
    protected $name;
13
14
    /**
15
     * @var string|null
16
     */
17
    protected $since;
18
19
    /**
20
     * @var string|null
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 1
    public function __construct(string $name, ?string $since, ?string $until)
32
    {
33 1
        if ($since !== null && !is_numeric($since)) {
34
            throw new \InvalidArgumentException('Since parameter is not numeric');
35
        }
36 1
        if ($until !== null && !is_numeric($until)) {
37
            throw new \InvalidArgumentException('Since parameter is not numeric');
38
        }
39
40 1
        if ($since === null && $until === null) {
41
            throw new \InvalidArgumentException('No version constraints provided');
42
        }
43
44 1
        $this->name = $name;
45 1
        $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 string|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...
46 1
        $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 string|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...
47 1
    }
48
49
    /**
50
     * @return string
51
     */
52 1
    public function getName(): string
53
    {
54 1
        return $this->name;
55
    }
56
57
    /**
58
     * @return null|string
59
     */
60
    public function getSince(): ?string
61
    {
62
        return $this->since;
63
    }
64
65
    /**
66
     * @return null|string
67
     */
68
    public function getUntil(): ?string
69
    {
70
        return $this->until;
71
    }
72
73
    /**
74
     * @param string $version
75
     * @return bool
76
     */
77 1
    public function isAvailableForVersion(string $version): bool
78
    {
79 1
        $sinceVersionMatch = $this->since === null || version_compare($version, $this->since, '>=');
80 1
        $untilVersionMatch = $this->until === null || version_compare($version, $this->until, '<=');
81
82 1
        return $sinceVersionMatch && $untilVersionMatch;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88 1
    public function __toString()
89
    {
90 1
        return $this->name;
91
    }
92
}
93