Completed
Push — master ( 866870...6584d5 )
by Andrii
15:02
created

AbstractPrice::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
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\price;
12
13
use hiqdev\php\billing\target\TargetInterface;
14
use hiqdev\php\billing\type\TypeInterface;
15
use hiqdev\php\units\QuantityInterface;
16
17
/**
18
 * Price.
19
 * @see PriceInterface
20
 *
21
 * @author Andrii Vasyliev <[email protected]>
22
 */
23
abstract class AbstractPrice implements PriceInterface, EntityInterface
24
{
25
    /**
26
     * @var integer
27
     */
28
    protected $id;
29
30
    /**
31
     * @var Target
32
     */
33
    protected $target;
34
35
    /**
36
     * @var Type
37
     */
38
    protected $type;
39
40
    public function __construct($id, TargetInterface $target, TypeInterface $type)
41
    {
42
        $this->id = $id;
43
        $this->target = $target;
0 ignored issues
show
Documentation Bug introduced by
It seems like $target of type object<hiqdev\php\billing\target\TargetInterface> is incompatible with the declared type object<hiqdev\php\billing\price\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...
44
        $this->type = $type;
0 ignored issues
show
Documentation Bug introduced by
It seems like $type of type object<hiqdev\php\billing\type\TypeInterface> is incompatible with the declared type object<hiqdev\php\billing\price\Type> of property $type.

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...
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function getTarget()
51
    {
52
        return $this->target;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->target; (hiqdev\php\billing\price\Target) is incompatible with the return type declared by the interface hiqdev\php\billing\price\PriceInterface::getTarget of type hiqdev\php\billing\target\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...
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function getType()
59
    {
60
        return $this->type;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->type; (hiqdev\php\billing\price\Type) is incompatible with the return type declared by the interface hiqdev\php\billing\price\PriceInterface::getType of type hiqdev\php\billing\type\TypeInterface.

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