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

AbstractPrice::getType()   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\EntityInterface;
14
use hiqdev\php\billing\target\TargetInterface;
15
use hiqdev\php\billing\type\TypeInterface;
16
use hiqdev\php\units\QuantityInterface;
17
18
/**
19
 * Price.
20
 * @see PriceInterface
21
 *
22
 * @author Andrii Vasyliev <[email protected]>
23
 */
24
abstract class AbstractPrice implements PriceInterface, EntityInterface
25
{
26
    /**
27
     * @var integer
28
     */
29
    protected $id;
30
31
    /**
32
     * @var Type
33
     */
34
    protected $type;
35
36
    /**
37
     * @var Target
38
     */
39
    protected $target;
40
41 7
    public function __construct($id, TypeInterface $type, TargetInterface $target)
42
    {
43 7
        $this->id = $id;
44 7
        $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 7
        $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...
46 7
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 2
    public function getId()
52
    {
53 2
        return $this->id;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 7
    public function getType()
60
    {
61 7
        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...
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 7
    public function getTarget()
68
    {
69 7
        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...
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     * Default sum calculation method: sum = price * usage.
75
     */
76 1
    public function calculateSum(QuantityInterface $quantity)
77
    {
78 1
        $usage = $this->calculateUsage($quantity);
79 1
        if ($usage === null) {
80
            return null;
81
        }
82
83 1
        $price = $this->calculatePrice($quantity);
84 1
        if ($price === null) {
85
            return null;
86
        }
87
88 1
        return $price->multiply($usage->getQuantity());
89
    }
90
91
    public static function create(array $data)
92
    {
93
        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 $target.
Loading history...
94
    }
95
96
    public function jsonSerialize()
97
    {
98
        return [
99
            'id' => $this->id,
100
            'type' => $this->type,
101
            'target' => $this->target,
102
        ];
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    abstract public function calculateUsage(QuantityInterface $quantity);
109
110
    /**
111
     * {@inheritdoc}
112
     */
113
    abstract public function calculatePrice(QuantityInterface $action);
114
}
115