Completed
Push — master ( d02bdd...0a5fc3 )
by Andrii
02:11
created

SinglePrice::getPrice()   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\price;
12
13
use hiqdev\php\billing\target\TargetInterface;
14
use hiqdev\php\billing\type\TypeInterface;
15
use hiqdev\php\units\Quantity;
16
use hiqdev\php\units\QuantityInterface;
17
use Money\Money;
18
19
/**
20
 * Single Price.
21
 *
22
 * - no charge for quantity less then prepaid
23
 * - same price for any quantity above prepaid
24
 *
25
 * @see PriceInterface
26
 *
27
 * @author Andrii Vasyliev <[email protected]>
28
 */
29
class SinglePrice extends AbstractPrice
30
{
31
    /**
32
     * @var Quantity prepaid quantity also implies Unit
33
     */
34
    protected $prepaid;
35
36
    /**
37
     * @var Money
38
     */
39
    protected $price;
40
41 5
    public function __construct(
42
                            $id,
43
        TypeInterface       $type,
44
        TargetInterface     $target,
45
        QuantityInterface   $prepaid,
46
        Money               $price
47
    ) {
48 5
        parent::__construct($id, $type, $target);
49 5
        $this->prepaid  = $prepaid;
0 ignored issues
show
Documentation Bug introduced by
$prepaid is of type object<hiqdev\php\units\QuantityInterface>, but the property $prepaid was declared to be of type object<hiqdev\php\units\Quantity>. 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...
50 5
        $this->price    = $price;
51 5
    }
52
53 1
    public function getPrepaid()
54
    {
55 1
        return $this->prepaid;
56
    }
57
58 1
    public function getPrice()
59
    {
60 1
        return $this->price;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 3
    public function calculateUsage(QuantityInterface $quantity)
67
    {
68 3
        $usage = $quantity->convert($this->prepaid->getUnit())->subtract($this->prepaid);
69
70 3
        return $usage->isPositive() ? $usage : null;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     * Same price for any usage.
76
     */
77 2
    public function calculatePrice(QuantityInterface $usage)
78
    {
79 2
        return $this->price;
80
    }
81
}
82