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

Definition   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 85.71%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 1
dl 0
loc 98
ccs 24
cts 28
cp 0.8571
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 19 7
A __toString() 0 4 1
A getName() 0 4 1
A getSince() 0 4 1
A getUntil() 0 4 1
A isAvailableForVersion() 0 7 4
A jsonSerialize() 0 8 1
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