Completed
Push — master ( 41f74f...866870 )
by Andrii
03:39
created

SinglePrice::calculateUsage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
crap 6
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\Quantity;
14
use hiqdev\php\units\QuantityInterface;
15
use Money\Money;
16
17
/**
18
 * Single Price.
19
 *
20
 * - no charge for quantity less then prepaid
21
 * - same price for any quantity above prepaid
22
 *
23
 * @see PriceInterface
24
 *
25
 * @author Andrii Vasyliev <[email protected]>
26
 */
27
class SinglePrice extends AbstractPrice
28
{
29
    /**
30
     * @var Quantity prepaid quantity also implies Unit
31
     */
32
    protected $prepaid;
33
34
    /**
35
     * @var Money
36
     */
37
    protected $price;
38
39
    public function __construct(
40
                            $id,
41
        TargetInterface     $target,
42
        TypeInterface       $type,
43
        QuantityInterface   $prepaid,
44
        Money               $price
45
    ) {
46
        parent::__construct($id, $target, $type);
47
        $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...
48
        $this->price    = $price;
49
    }
50
51
/*
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
52
    protected $properties = [
53
        'id'        => '',
54
        'target'    => [AbstractTarget::class, 'create'],
55
        'type'      => [Type::class, 'create'],
56
        'prepaid'   => [Quantity::class, 'create'],
57
        'price'     => [MoneyFactory::class, 'create'],
58
    ];
59
*/
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function calculateUsage(QuantityInterface $quantity)
65
    {
66
        $usage = $quantity->convert($this->prepaid->getUnit())->subtract($this->prepaid);
67
68
        return $usage->isPositive() ? $usage : null;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     * Same price for any usage.
74
     */
75
    public function calculatePrice(QuantityInterface $usage)
76
    {
77
        return $this->price;
78
    }
79
}
80