Passed
Pull Request — master (#73)
by
unknown
16:36 queued 01:36
created

Plan::getSeller()   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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
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
    public function setId(int $id): void
87
    {
88
        $this->id = $id;
89
    }
90
91 5
    /**
92
     * @return string
93 5
     */
94
    public function getName(): string
95
    {
96
        return $this->name;
97
    }
98
99
    public function setName(string $name): void
100
    {
101
        $this->name = $name;
102
    }
103
104 2
    /**
105
     * @return CustomerInterface
106 2
     */
107
    public function getSeller(): ?CustomerInterface
108
    {
109
        return $this->seller;
110
    }
111
112 4
    public function getParentId(): ?int
113
    {
114 4
        return $this->parent_id;
115
    }
116
117
    public function setParentId(int $id): void
118
    {
119
        $this->parent_id = $id;
120
    }
121
122
    public function hasPrices(): bool
123
    {
124
        return $this->prices !== [];
125
    }
126
127
    /**
128
     * @return PriceInterface[]
129
     */
130
    public function getPrices(): array
131
    {
132
        return $this->prices;
133
    }
134
135
    public function setPrices(array $prices): void
136
    {
137
        if ($this->hasPrices()) {
138
            throw new CannotReassignException('plan prices');
139
        }
140
        $this->prices = $prices;
141
    }
142
143
    public function getType(): ?TypeInterface
144
    {
145
        return $this->type ?? null;
146
    }
147
148
    public function jsonSerialize(): array
149
    {
150
        return array_filter(get_object_vars($this));
151
    }
152
}
153