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

AbstractAction::isApplicable()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 1
ccs 0
cts 0
cp 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\action;
12
13
use hiqdev\php\billing\target\TargetInterface;
14
use hiqdev\php\billing\customer\CustomerInterface;
15
use DateTime;
16
use hiqdev\php\units\QuantityInterface;
17
18
/**
19
 * Chargable Action.
20
 *
21
 * @see ActionInterface
22
 *
23
 * @author Andrii Vasyliev <[email protected]>
24
 */
25
abstract class AbstractAction implements ActionInterface
26
{
27
    /**
28
     * @var int
29
     */
30
    protected $id;
31
32
    /**
33
     * @var CustomerInterface
34
     */
35
    protected $customer;
36
37
    /**
38
     * @var TargetInterface
39
     */
40
    protected $target;
41
42
    /**
43
     * @var QuantityInterface
44
     */
45
    protected $quantity;
46
47
    /**
48
     * @var DateTime
49
     */
50
    protected $time;
51
52
    /**
53
     * @param CustomerInterface $customer
54
     * @param TargetInterface $target
55
     * @param QuantityInterface $quantity
56
     * @param DateTime $time
57
     */
58
    public function __construct(
59
        CustomerInterface $customer,
60
        TargetInterface $target,
61
        QuantityInterface $quantity,
62
        DateTime $time
63
    ) {
64
        $this->customer = $customer;
65
        $this->target = $target;
66
        $this->quantity = $quantity;
67
        $this->time = $time;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function getCustomer()
74
    {
75
        return $this->customer;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function getTarget()
82
    {
83
        return $this->target;
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function getQuantity()
90
    {
91
        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\actio...nInterface::getQuantity of type hiqdev\php\billing\action\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...
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function getTime()
98
    {
99
        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\action\ActionInterface::getTime of type hiqdev\php\billing\action\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...
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function calculateCharge(PriceInterface $price)
106
    {
107
        if (!$this->isApplicable($price)) {
108
            return null;
109
        }
110
111
        $usage = $price->calculateUsage($this->getQuantity());
112
        if ($usage === null) {
113
            return null;
114
        }
115
116
        $sum = $price->calculateSum($this->getQuantity());
117
        if ($sum === null) {
118
            return null;
119
        }
120
121
        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\action\Charge) is incompatible with the return type declared by the interface hiqdev\php\billing\actio...erface::calculateCharge of type hiqdev\php\billing\action\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...
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127
    abstract public function isApplicable(PriceInterface $price);
128
}
129