Completed
Push — master ( 6b14db...a7e110 )
by Andrii
01:47
created

AbstractPrice   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 2
dl 0
loc 73
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getTarget() 0 4 1
A getType() 0 4 1
A calculateSum() 0 14 3
calculateUsage() 0 1 ?
calculatePrice() 0 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, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\billing;
12
13
use hiqdev\php\units\QuantityInterface;
14
15
/**
16
 * Price.
17
 * @see PriceInterface
18
 *
19
 * @author Andrii Vasyliev <[email protected]>
20
 */
21
abstract class AbstractPrice implements PriceInterface
22
{
23
    /**
24
     * @var integer
25
     */
26
    protected $id;
27
28
    /**
29
     * @var Tariff
30
     */
31
    protected $tariff;
32
33
    /**
34
     * @var Target
35
     */
36
    protected $target;
37
38
    /**
39
     * @var Type
40
     */
41
    protected $type;
42
43
    public function __construct(TargetInterface $target, TypeInterface $type)
44
    {
45
        $this->target = $target;
0 ignored issues
show
Documentation Bug introduced by
$target is of type object<hiqdev\php\billing\TargetInterface>, but the property $target was declared to be of type object<hiqdev\php\billing\Target>. 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...
46
        $this->type = $type;
0 ignored issues
show
Documentation Bug introduced by
$type is of type object<hiqdev\php\billing\TypeInterface>, but the property $type was declared to be of type object<hiqdev\php\billing\Type>. 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...
47
    }
48
49
    /**
50
     * @inheritdoc
51
     */
52
    public function getTarget()
53
    {
54
        return $this->target;
55
    }
56
57
    /**
58
     * @inheritdoc
59
     */
60
    public function getType()
61
    {
62
        return $this->type;
63
    }
64
65
    /**
66
     * @inheritdoc
67
     * Default sum calculation method: sum = price * usage
68
     */
69
    public function calculateSum(QuantityInterface $quantity)
70
    {
71
        $usage = $this->calculateUsage($quantity);
72
        if ($usage === null) {
73
            return null;
74
        }
75
76
        $price = $this->calculatePrice($quantity);
77
        if ($price === null) {
78
            return null;
79
        }
80
81
        return $price->multiply($usage->getQuantity());
82
    }
83
84
    /**
85
     * @inheritdoc
86
     */
87
    abstract public function calculateUsage(QuantityInterface $quantity);
88
89
    /**
90
     * @inheritdoc
91
     */
92
    abstract public function calculatePrice(QuantityInterface $action);
93
}
94