Passed
Push — master ( 7940cf...71924e )
by Dmitry
12:35
created

Plan::getParentId()   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
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * PHP Billing Library
4
 *
5
 * @link      https://github.com/hiqdev/php-billing
6
 * @package   php-billing
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2017-2020, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\billing\plan;
12
13
use hiqdev\php\billing\customer\CustomerInterface;
14
use hiqdev\php\billing\Exception\CannotReassignException;
15
use hiqdev\php\billing\price\PriceInterface;
16
use hiqdev\php\billing\type\TypeInterface;
17
18
/**
19
 * Tariff Plan.
20
 *
21
 * @author Andrii Vasyliev <[email protected]>
22
 */
23
class Plan implements PlanInterface
24
{
25
    /**
26
     * @var int
27
     */
28
    protected $id;
29
30
    /**
31
     * @var string
32
     */
33
    protected $name;
34
35
    protected ?int $parent_id = null;
36
37
    /**
38
     * @var CustomerInterface
39
     */
40
    protected $seller;
41
42
    /**
43
     * @var PriceInterface[]
44
     */
45
    protected $prices = [];
46
47
    /**
48
     * @var ?TypeInterface
49
     */
50
    protected $type;
51
52
    /**
53
     * @param int|string|null $id
54
     * @param string $name
55 14
     * @param PriceInterface[] $prices
56
     */
57
    public function __construct(
58
        $id,
59
        $name,
60
        CustomerInterface $seller = null,
61 14
        $prices = [],
62 14
        TypeInterface $type = null,
63 14
        $parent_id = null
64 14
    ) {
65 14
        $this->id = $id;
0 ignored issues
show
Documentation Bug introduced by
It seems like $id can also be of type string. However, the property $id is declared as type integer. 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...
66
        $this->name = $name;
67
        $this->seller = $seller;
68
        $this->prices = $prices;
69
        $this->type = $type;
70
        $this->parent_id = $parent_id;
71
    }
72
73
    public function getUniqueId()
74
    {
75 4
        return $this->getId();
76
    }
77 4
78
    /**
79
     * @return int|string
80
     */
81
    public function getId()
82
    {
83 5
        return $this->id;
84
    }
85 5
86
    /**
87
     * @return string
88
     */
89
    public function getName(): string
90
    {
91 5
        return $this->name;
92
    }
93 5
94
    public function setName(string $name): void
95
    {
96
        $this->name = $name;
97
    }
98
99
    /**
100
     * @return CustomerInterface
101
     */
102
    public function getSeller(): ?CustomerInterface
103
    {
104 2
        return $this->seller;
105
    }
106 2
107
    public function getParentId(): ?int
108
    {
109
        return $this->parent_id;
110
    }
111
112 4
    public function setParentId(int $id): void
113
    {
114 4
        $this->parent_id = $id;
115
    }
116
117
    public function hasPrices(): bool
118
    {
119
        return $this->prices !== [];
120
    }
121
122
    /**
123
     * @return PriceInterface[]
124
     */
125
    public function getPrices(): array
126
    {
127
        return $this->prices;
128
    }
129
130
    public function setPrices(array $prices): void
131
    {
132
        if ($this->hasPrices()) {
133
            throw new CannotReassignException('plan prices');
134
        }
135
        $this->prices = $prices;
136
    }
137
138
    public function getType(): ?TypeInterface
139
    {
140
        return $this->type ?? null;
141
    }
142
143
    public function jsonSerialize(): array
144
    {
145
        return array_filter(get_object_vars($this));
146
    }
147
}
148