Completed
Push — master ( 7a34c1...e07e94 )
by Dmitry
02:52
created

GrowingDiscount::getValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
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 hiqdev\php\billing\charge\ChargeInterface;
14
use DateInterval;
15
use Money\Money;
16
17
/**
18
 * Growing discount.
19
 *
20
 * @author Andrii Vasyliev <[email protected]>
21
 */
22
class GrowingDiscount extends FixedDiscount
23
{
24
    /**
25
     * @var int|Money
26
     */
27
    protected $step;
28
29
    /**
30
     * @var int|Money
31
     */
32
    protected $min;
33
34
    /**
35
     * @var int|Money
36
     */
37
    protected $max;
38
39
    /**
40
     * @var DateInterval
41
     */
42
    protected $period;
43
44 1
    public function __construct($step, $min = null, array $addons = [])
45
    {
46 1
        Modifier::__construct($addons);
47 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...
48 1
        if ($min) {
49
            $this->min($min);
50
        }
51 1
    }
52
53
    public function isAbsolute()
54
    {
55
        return $this->step instanceof Money;
56
    }
57
58
    public function isRelative()
59
    {
60
        return !$this->isAbsolute();
61
    }
62
63
    public function min($min)
64
    {
65
        $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...
66
67
        return $this;
68
    }
69
70
    public function max($max)
71
    {
72
        $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...
73
74
        return $this;
75
    }
76
77
    public function getValue(ChargeInterface $charge = null)
78
    {
79
        $time = $charge->getAction()->getTime();
0 ignored issues
show
Bug introduced by
It seems like $charge is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
80
        var_dump($time);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($time); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
81
        die;
0 ignored issues
show
Coding Style Compatibility introduced by
The method getValue() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
82
83
    }
84
85
    public function ensureValidLimit($limit, $name)
86
    {
87
        $limit = static::ensureValidValue($limit);
88
        if ($limit instanceof Money) {
89
            if ($this->isRelative()) {
90
                throw new \Exception("$name must be given as percentage because step is percentage");
91
            }
92
        } elseif ($this->isAbsolute()) {
93
            throw new \Exception("$name must be money because step is money");
94
        }
95
96
        return $limit;
97
    }
98
99
    public function everyMonth($num = 1)
100
    {
101
        if ($this->period !== null) {
102
            throw new \Exception('periodicity is already set');
103
        }
104
        if (empty($num)) {
105
            $num = 1;
106
        }
107
        if (filter_var($num, FILTER_VALIDATE_INT) === false) {
108
            throw new \Exception('periodicity must be integer number');
109
        }
110
        $this->period = new DateInterval("P${num}M");
111
112
        return $this;
113
    }
114
}
115