Passed
Pull Request — master (#70)
by
unknown
11:42
created

Plan   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Test Coverage

Coverage 59.26%

Importance

Changes 5
Bugs 0 Features 3
Metric Value
eloc 27
dl 0
loc 126
ccs 16
cts 27
cp 0.5926
rs 10
c 5
b 0
f 3
wmc 14

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getUniqueId() 0 3 1
A setName() 0 3 1
A hasPrices() 0 3 1
A getId() 0 3 1
A getPrices() 0 3 1
A getType() 0 3 1
A getParent() 0 3 1
A setPrices() 0 6 2
A jsonSerialize() 0 3 1
A setParent() 0 3 1
A __construct() 0 14 1
A getName() 0 3 1
A getSeller() 0 3 1
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
     */
38
    protected $parent;
39
40
    /**
41
     * @var CustomerInterface
42
     */
43
    protected $seller;
44
45
    /**
46
     * @var PriceInterface[]
47
     */
48
    protected $prices = [];
49
50
    /**
51
     * @var ?TypeInterface
52
     */
53
    protected $type;
54
55 14
    /**
56
     * @param int|string|null $id
57
     * @param string $name
58
     * @param PriceInterface[] $prices
59
     */
60
    public function __construct(
61 14
        $id,
62 14
        $name,
63 14
        CustomerInterface $seller = null,
64 14
        $prices = [],
65 14
        TypeInterface $type = null,
66
        PlanInterface $parent = 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
        $this->parent = $parent;
74
    }
75 4
76
    public function getUniqueId()
77 4
    {
78
        return $this->getId();
79
    }
80
81
    /**
82
     * @return int|string
83 5
     */
84
    public function getId()
85 5
    {
86
        return $this->id;
87
    }
88
89
    /**
90
     * @return string
91 5
     */
92
    public function getName(): string
93 5
    {
94
        return $this->name;
95
    }
96
97
    public function setName(string $name): void
98
    {
99
        $this->name = $name;
100
    }
101
102
    /**
103
     * @return CustomerInterface
104 2
     */
105
    public function getSeller(): ?CustomerInterface
106 2
    {
107
        return $this->seller;
108
    }
109
110
    public function getParent(): ?PlanInterface
111
    {
112 4
        return $this->parent;
113
    }
114 4
115
    public function hasPrices(): bool
116
    {
117
        return $this->prices !== [];
118
    }
119
120
    /**
121
     * @return PriceInterface[]
122
     */
123
    public function getPrices(): array
124
    {
125
        return $this->prices;
126
    }
127
128
    public function setPrices(array $prices): void
129
    {
130
        if ($this->hasPrices()) {
131
            throw new CannotReassignException('plan prices');
132
        }
133
        $this->prices = $prices;
134
    }
135
136
    public function getType(): ?TypeInterface
137
    {
138
        return $this->type ?? null;
139
    }
140
141
    public function setParent(PlanInterface $parent): void
142
    {
143
        $this->parent = $parent;
0 ignored issues
show
Documentation Bug introduced by
$parent is of type hiqdev\php\billing\plan\PlanInterface, but the property $parent was declared to be of type hiqdev\php\billing\plan\Plan|null. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof 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 given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
144
    }
145
146
    public function jsonSerialize(): array
147
    {
148
        return array_filter(get_object_vars($this));
149
    }
150
}
151