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

AbstractPrice::calculateUsage()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
ccs 0
cts 0
cp 0
c 0
b 0
f 0
nc 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, EntityInterface
22
{
23
    /**
24
     * @var integer
25
     */
26
    protected $id;
27
28
    /**
29
     * @var Target
30
     */
31
    protected $target;
32
33
    /**
34
     * @var Type
35
     */
36
    protected $type;
37
38
    public function __construct($id, TargetInterface $target, TypeInterface $type)
39
    {
40
        $this->id = $id;
41
        $this->target = $target;
0 ignored issues
show
Documentation Bug introduced by
It seems like $target of type object<hiqdev\php\billing\TargetInterface> is incompatible with the declared type object<hiqdev\php\billing\Target> of property $target.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
42
        $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...
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function getTarget()
49
    {
50
        return $this->target;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->target; (hiqdev\php\billing\Target) is incompatible with the return type declared by the interface hiqdev\php\billing\PriceInterface::getTarget of type hiqdev\php\billing\TargetInterface.

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...
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function getType()
57
    {
58
        return $this->type;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     * Default sum calculation method: sum = price * usage.
64
     */
65
    public function calculateSum(QuantityInterface $quantity)
66
    {
67
        $usage = $this->calculateUsage($quantity);
68
        if ($usage === null) {
69
            return null;
70
        }
71
72
        $price = $this->calculatePrice($quantity);
73
        if ($price === null) {
74
            return null;
75
        }
76
77
        return $price->multiply($usage->getQuantity());
78
    }
79
80
    public static function create(array $data)
81
    {
82
        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...
83
    }
84
85
    public function jsonSerialize()
86
    {
87
        return [
88
            'id' => $this->id,
89
            'type' => $this->type,
90
            'target' => $this->target,
91
        ];
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    abstract public function calculateUsage(QuantityInterface $quantity);
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    abstract public function calculatePrice(QuantityInterface $action);
103
}
104