Completed
Push — master ( 2eb122...6a0424 )
by Andrii
02:35
created

GrowingDiscount::isAbsolute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 2
cp 0
cc 1
eloc 2
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-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\billing\charge\modifiers;
12
13
use DateInterval;
14
use Money\Money;
15
16
/**
17
 * Growing discount.
18
 *
19
 * @author Andrii Vasyliev <[email protected]>
20
 */
21
class GrowingDiscount extends FixedDiscount
22
{
23
    /**
24
     * @var int|Money
25
     */
26
    protected $step;
27
28
    /**
29
     * @var int|Money
30
     */
31
    protected $min;
32
33
    /**
34
     * @var int|Money
35
     */
36
    protected $max;
37
38
    /**
39
     * @var DateInterval
40
     */
41
    protected $period;
42
43 1
    public function __construct($step, $min = null, array $addons = [])
44
    {
45 1
        Modifier::__construct($addons);
46 1
        $this->step = static::ensureValidValue($step);
0 ignored issues
show
Documentation Bug introduced by
It seems like static::ensureValidValue($step) can also be of type string. However, the property $step is declared as type integer|object<Money\Money>. 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...
47 1
        if ($min) {
48
            $this->min($min);
49
        }
50 1
    }
51
52
    public function isAbsolute()
53
    {
54
        return $this->step instanceof Money;
55
    }
56
57
    public function isRelative()
58
    {
59
        return !$this->isAbsolute();
60
    }
61
62
    public function min($min)
63
    {
64
        $this->min = $this->ensureValidLimit($min, 'min');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->ensureValidLimit($min, 'min') can also be of type string. However, the property $min is declared as type integer|object<Money\Money>. 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...
65
66
        return $this;
67
    }
68
69
    public function max($max)
70
    {
71
        $this->max = $this->ensureValidLimit($max, 'max');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->ensureValidLimit($max, 'max') can also be of type string. However, the property $max is declared as type integer|object<Money\Money>. 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...
72
73
        return $this;
74
    }
75
76
    public function ensureValidLimit($limit, $name)
77
    {
78
        $limit = static::ensureValidValue($limit);
79
        if ($limit instanceof Money) {
80
            if ($this->isRelative()) {
81
                throw new \Exception("$name must be given as percentage because step is percentage");
82
            }
83
        } elseif ($this->isAbsolute()) {
84
            throw new \Exception("$name must be money because step is money");
85
        }
86
87
        return $limit;
88
    }
89
90
    public function everyMonth($num = 1)
91
    {
92
        if ($this->period !== null) {
93
            throw new \Exception('periodicity is already set');
94
        }
95
        if (empty($num)) {
96
            $num = 1;
97
        }
98
        if (filter_var($num, FILTER_VALIDATE_INT) === false) {
99
            throw new \Exception('periodicity must be integer number');
100
        }
101
        $this->period = new DateInterval("P${num}M");
102
103
        return $this;
104
    }
105
}
106