Completed
Push — master ( 865c46...71786e )
by Andrii
02:06
created

FixedDiscount   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 96.55%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 6
dl 0
loc 65
ccs 28
cts 29
cp 0.9655
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getValue() 0 4 1
A isAbsolute() 0 4 1
A isRelative() 0 4 1
A calculateSum() 0 4 2
A buildPrice() 0 8 1
A getType() 0 4 1
A getTarget() 0 4 1
A modifyCharge() 0 12 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\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\Money;
21
22
/**
23
 * Fixed discount.
24
 *
25
 * @author Andrii Vasyliev <[email protected]>
26
 */
27
class FixedDiscount extends Discount
28
{
29
    /**
30
     * @var int|Money
31
     */
32
    protected $value;
33
34 5
    public function __construct($value, array $addons = [])
35
    {
36 5
        parent::__construct($addons);
37 5
        $this->value = static::ensureValidValue($value);
0 ignored issues
show
Documentation Bug introduced by
It seems like static::ensureValidValue($value) can also be of type double or 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...
38 5
    }
39
40 2
    public function getValue()
41
    {
42 2
        return $this->value;
43
    }
44
45 4
    public function isAbsolute()
46
    {
47 4
        return $this->value instanceof Money;
48
    }
49
50 2
    public function isRelative()
51
    {
52 2
        return !$this->isAbsolute();
53
    }
54
55 2
    public function calculateSum(Money $sum)
56
    {
57 2
        return $this->isAbsolute() ? $this->value : $sum->multiply($this->value*0.01);
58
    }
59
60 2
    public function buildPrice(Money $sum)
61
    {
62 2
        $type = $this->getType();
63 2
        $target = $this->getTarget();
64 2
        $prepaid = Quantity::items(0);
65
66 2
        return new SinglePrice(null, $type, $target, null, $prepaid, $sum);
67
    }
68
69 2
    public function getType()
70
    {
71 2
        return new Type(Type::ANY, 'discount');
72
    }
73
74 2
    public function getTarget()
75
    {
76 2
        return new Target(Target::ANY, Target::ANY);
77
    }
78
79 2
    public function modifyCharge(ChargeInterface $charge, ActionInterface $action): array
80
    {
81 2
        if ($charge === null) {
82
            return [];
83
        }
84 2
        $sum = $this->calculateSum($charge->getSum());
85 2
        $usage  = Quantity::items(1);
86 2
        $price = $this->buildPrice($sum);
0 ignored issues
show
Bug introduced by
It seems like $sum defined by $this->calculateSum($charge->getSum()) on line 84 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...
87 2
        $discount = new Charge(null, $action, $price, $usage, $sum);
88
89 2
        return [$charge, $discount];
90
    }
91
}
92