Completed
Push — master ( 99dba7...2516f4 )
by Dawid
17:28 queued 02:42
created

Definition::__construct()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 7

Importance

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