Completed
Push — master ( e1b51a...ef7e86 )
by Andrii
05:09
created

FixedDiscount::isRelative()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
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-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\billing\charge\modifiers;
12
13
use hiqdev\php\billing\action\ActionInterface;
14
use hiqdev\php\billing\charge\Charge;
15
use hiqdev\php\billing\charge\ChargeInterface;
16
use hiqdev\php\billing\price\SinglePrice;
17
use hiqdev\php\billing\target\Target;
18
use hiqdev\php\billing\type\Type;
19
use hiqdev\php\units\Quantity;
20
use Money\Currency;
21
use Money\Money;
22
23
/**
24
 * Fixed discount.
25
 *
26
 * @author Andrii Vasyliev <[email protected]>
27
 */
28
class FixedDiscount extends Modifier
29
{
30
    /**
31
     * @var int|Money
32
     */
33
    protected $value;
34
35 6
    public function __construct($value, array $addons = [])
36
    {
37 6
        parent::__construct($addons);
38 6
        $this->value = static::ensureValidValue($value);
0 ignored issues
show
Documentation Bug introduced by
It seems like static::ensureValidValue($value) can also be of type string. However, the property $value 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...
39 6
    }
40
41 3
    public function getValue()
42
    {
43 3
        return $this->value;
44
    }
45
46 5
    public function isAbsolute()
47
    {
48 5
        return $this->value instanceof Money;
49
    }
50
51 3
    public function isRelative()
52
    {
53 3
        return !$this->isAbsolute();
54
    }
55
56 2
    public function calculateSum(Money $sum)
57
    {
58 2
        return $this->isAbsolute() ? $this->value : $sum->multiply($this->value*0.01);
59
    }
60
61 2
    public function buildPrice(Money $sum)
62
    {
63 2
        $type = $this->getType();
64 2
        $target = $this->getTarget();
65 2
        $prepaid = Quantity::items(0);
66
67 2
        return new SinglePrice(null, $type, $target, null, $prepaid, $sum);
68
    }
69
70 2
    public function getType()
71
    {
72 2
        return new Type(Type::ANY, 'discount');
73
    }
74
75 2
    public function getTarget()
76
    {
77 2
        return new Target(Target::ANY, Target::ANY);
78
    }
79
80 2
    public function modifyCharge(?ChargeInterface $charge, ActionInterface $action): array
81
    {
82 2
        if ($charge === null) {
83
            return [];
84
        }
85
86 2
        $month = $action->getTime()->modify('first day of this month midnight');
87 2
        if (!$this->checkPeriod($month)) {
88
            return [$charge];
89
        }
90
91 2
        $sum = $this->calculateSum($charge->getSum());
92 2
        $usage  = Quantity::items(1);
93 2
        $price = $this->buildPrice($sum);
0 ignored issues
show
Bug introduced by
It seems like $sum defined by $this->calculateSum($charge->getSum()) on line 91 can also be of type integer; however, hiqdev\php\billing\charg...dDiscount::buildPrice() does only seem to accept object<Money\Money>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
94 2
        $discount = new Charge(null, $action, $price, $usage, $sum);
95 2
        $reason = $this->getReason();
96 2
        if ($reason) {
97
            $discount->setComment($reason->getValue());
98
        }
99
100 2
        return [$charge, $discount];
101
    }
102
103 7
    public static function ensureValidValue($value)
104
    {
105 7
        if ($value instanceof Money) {
106 2
            return $value;
107
        }
108
109 6
        if (is_numeric($value)) {
110 4
            return (string) $value;
111
        }
112
113 3
        if (is_string($value) && preg_match('/^(\d{1,5}(\.\d+)?)%$/', $value, $ms)) {
114 2
            return $ms[1];
115
        }
116
117 1
        if (is_string($value) && preg_match('/^(\d{1,5}(\.\d+)?) ([A-Z]{3})$/', $value, $ms)) {
118 1
            return new Money($ms[1]*100, new Currency($ms[3]));
119
        }
120
121
        /// TODO: add special exception
122
        var_dump($value);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($value); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
123
        throw new \Exception("invalid discount value: $value");
124
    }
125
}
126