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

AbstractAction::calculateCharge()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 18
ccs 0
cts 15
cp 0
rs 9.2
cc 4
eloc 10
nc 4
nop 1
crap 20
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 DateTime;
14
use hiqdev\php\units\QuantityInterface;
15
16
/**
17
 * Chargable Action.
18
 *
19
 * @see ActionInterface
20
 *
21
 * @author Andrii Vasyliev <[email protected]>
22
 */
23
abstract class AbstractAction implements ActionInterface
24
{
25
    /**
26
     * @var int
27
     */
28
    protected $id;
29
30
    /**
31
     * @var CustomerInterface
32
     */
33
    protected $customer;
34
35
    /**
36
     * @var TargetInterface
37
     */
38
    protected $target;
39
40
    /**
41
     * @var QuantityInterface
42
     */
43
    protected $quantity;
44
45
    /**
46
     * @var DateTime
47
     */
48
    protected $time;
49
50
    /**
51
     * @param CustomerInterface $customer
52
     * @param TargetInterface $target
53
     * @param QuantityInterface $quantity
54
     * @param DateTime $time
55
     */
56
    public function __construct(
57
        CustomerInterface $customer,
58
        TargetInterface $target,
59
        QuantityInterface $quantity,
60
        DateTime $time
61
    ) {
62
        $this->customer = $customer;
63
        $this->target = $target;
64
        $this->quantity = $quantity;
65
        $this->time = $time;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function getCustomer()
72
    {
73
        return $this->customer;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function getTarget()
80
    {
81
        return $this->target;
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function getQuantity()
88
    {
89
        return $this->quantity;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->quantity; (hiqdev\php\units\QuantityInterface) is incompatible with the return type declared by the interface hiqdev\php\billing\ActionInterface::getQuantity of type hiqdev\php\billing\QuantityInterface.

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...
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function getTime()
96
    {
97
        return $this->time;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->time; (DateTime) is incompatible with the return type declared by the interface hiqdev\php\billing\ActionInterface::getTime of type hiqdev\php\billing\DateTime.

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...
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function calculateCharge(PriceInterface $price)
104
    {
105
        if (!$this->isApplicable($price)) {
106
            return null;
107
        }
108
109
        $usage = $price->calculateUsage($this->getQuantity());
110
        if ($usage === null) {
111
            return null;
112
        }
113
114
        $sum = $price->calculateSum($this->getQuantity());
115
        if ($sum === null) {
116
            return null;
117
        }
118
119
        return new Charge($this, $price->getTarget(), $price->getType(), $usage, $sum);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \hiqdev\php\b...tType(), $usage, $sum); (hiqdev\php\billing\Charge) is incompatible with the return type declared by the interface hiqdev\php\billing\Actio...erface::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...
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    abstract public function isApplicable(PriceInterface $price);
126
}
127