Completed
Push — master ( 77b660...93750f )
by Andrii
02:18
created

AbstractPrice::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 1
eloc 2
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, 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
    }
49
50
    /**
51
     * {@inheritdoc}
52 2
     */
53
    public function getTarget()
54 2
    {
55
        return $this->target;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60 2
     */
61
    public function getType()
62 2
    {
63
        return $this->type;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     * Default sum calculation method: sum = price * usage.
69 1
     */
70
    public function calculateSum(QuantityInterface $quantity)
71 1
    {
72 1
        $usage = $this->calculateUsage($quantity);
73
        if ($usage === null) {
74
            return null;
75
        }
76 1
77 1
        $price = $this->calculatePrice($quantity);
78
        if ($price === null) {
79
            return null;
80
        }
81 1
82
        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