Passed
Push — master ( dd7560...07e9af )
by Dmitry
44:49 queued 29:41
created

Plan::getType()   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
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
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
    /**
36
     * @var Plan|null
37
     * XXX not sure to implement
38
     */
39
    protected $parent;
40
41
    /**
42
     * @var CustomerInterface
43
     */
44
    protected $seller;
45
46
    /**
47
     * @var PriceInterface[]
48
     */
49
    protected $prices = [];
50
51
    /**
52
     * @var ?TypeInterface
53
     */
54
    protected $type;
55 14
56
    /**
57
     * @param int|string|null $id
58
     * @param string $name
59
     * @param PriceInterface[] $prices
60
     */
61 14
    public function __construct(
62 14
        $id,
63 14
        $name,
64 14
        CustomerInterface $seller = null,
65 14
        $prices = [],
66
        TypeInterface $type = null
67
    ) {
68
        $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...
69
        $this->name = $name;
70
        $this->seller = $seller;
71
        $this->prices = $prices;
72
        $this->type = $type;
73
    }
74
75 4
    public function getUniqueId()
76
    {
77 4
        return $this->getId();
78
    }
79
80
    /**
81
     * @return int|string
82
     */
83 5
    public function getId()
84
    {
85 5
        return $this->id;
86
    }
87
88
    /**
89
     * @return string
90
     */
91 5
    public function getName(): string
92
    {
93 5
        return $this->name;
94
    }
95
96
    public function setName(string $name): void
97
    {
98
        $this->name = $name;
99
    }
100
101
    /**
102
     * @return CustomerInterface
103
     */
104 2
    public function getSeller(): ?CustomerInterface
105
    {
106 2
        return $this->seller;
107
    }
108
109
    public function getParent(): ?PlanInterface
110
    {
111
        return $this->parent;
112 4
    }
113
114 4
    public function hasPrices(): bool
115
    {
116
        return $this->prices !== [];
117
    }
118
119
    /**
120
     * @return PriceInterface[]
121
     */
122
    public function getPrices(): array
123
    {
124
        return $this->prices;
125
    }
126
127
    public function setPrices(array $prices): void
128
    {
129
        if ($this->hasPrices()) {
130
            throw new CannotReassignException('plan prices');
131
        }
132
        $this->prices = $prices;
133
    }
134
135
    public function getType(): ?TypeInterface
136
    {
137
        return $this->type ?? null;
138
    }
139
140
    public function jsonSerialize()
141
    {
142
        return array_filter(get_object_vars($this));
143
    }
144
}
145