Completed
Push — master ( cbae82...6b14db )
by Andrii
02:38
created

AbstractPrice   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 95
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getTarget() 0 4 1
A getType() 0 4 1
A calculateCharge() 0 18 4
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
     */
68
    public function calculateCharge(ActionInterface $action)
69
    {
70
        if (!$action->isApplicable($this)) {
71
            return null;
72
        }
73
74
        $usage = $this->calculateUsage($action->getQuantity());
75
        if ($usage === null) {
76
            return null;
77
        }
78
79
        $sum = $this->calculateSum($action->getQuantity());
80
        if ($sum === null) {
81
            return null;
82
        }
83
84
        return new Charge($action, $this->target, $this->type, $usage, $sum);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \hiqdev\php\b...s->type, $usage, $sum); (hiqdev\php\billing\Charge) is incompatible with the return type declared by the interface hiqdev\php\billing\PriceInterface::calculateCharge of type hiqdev\php\billing\ChargeInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
Documentation introduced by
$usage is of type object<hiqdev\php\units\QuantityInterface>, but the function expects a object<hiqdev\php\billing\QuantityInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$sum is of type object<Money\Money>, but the function expects a object<hiqdev\php\billing\MoneyInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
85
    }
86
87
    /**
88
     * @inheritdoc
89
     * Default sum calculation method: sum = price * usage
90
     */
91
    public function calculateSum(QuantityInterface $quantity)
92
    {
93
        $usage = $this->calculateUsage($quantity);
94
        if ($usage === null) {
95
            return null;
96
        }
97
98
        $price = $this->calculatePrice($quantity);
99
        if ($price === null) {
100
            return null;
101
        }
102
103
        return $price->multiply($usage->getQuantity());
104
    }
105
106
    /**
107
     * @inheritdoc
108
     */
109
    abstract public function calculateUsage(QuantityInterface $quantity);
110
111
    /**
112
     * @inheritdoc
113
     */
114
    abstract public function calculatePrice(QuantityInterface $action);
115
}
116