Completed
Push — master ( 93750f...2ef50e )
by Andrii
02:27
created

AbstractPrice::getTarget()   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
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 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 Plan
30
     */
31
    protected $plan;
32
33
    /**
34
     * @var Target
35
     */
36
    protected $target;
37
38
    /**
39
     * @var Type
40
     */
41
    protected $type;
42
43 3
    public function __construct($id, TargetInterface $target, TypeInterface $type)
44
    {
45 3
        $this->id = $id;
46 3
        $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...
47 3
        $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...
48 3
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 2
    public function getTarget()
54
    {
55 2
        return $this->target;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 2
    public function getType()
62
    {
63 2
        return $this->type;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     * Default sum calculation method: sum = price * usage.
69
     */
70 1
    public function calculateSum(QuantityInterface $quantity)
71
    {
72 1
        $usage = $this->calculateUsage($quantity);
73 1
        if ($usage === null) {
74
            return null;
75
        }
76
77 1
        $price = $this->calculatePrice($quantity);
78 1
        if ($price === null) {
79
            return null;
80
        }
81
82 1
        return $price->multiply($usage->getQuantity());
83
    }
84
85
    public static function create(array $data)
86
    {
87
        return new SinglePrice($data['id'], $data['target']);
0 ignored issues
show
Bug introduced by
The call to SinglePrice::__construct() misses some required arguments starting with $type.
Loading history...
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    abstract public function calculateUsage(QuantityInterface $quantity);
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    abstract public function calculatePrice(QuantityInterface $action);
99
}
100