Completed
Push — master ( 428142...9f94b9 )
by Andrii
02:14
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 DateTime;
14
use hiqdev\php\billing\charge\Charge;
15
use hiqdev\php\billing\customer\CustomerInterface;
16
use hiqdev\php\billing\price\PriceInterface;
17
use hiqdev\php\billing\sale\SaleInterface;
18
use hiqdev\php\billing\target\TargetInterface;
19
use hiqdev\php\billing\type\TypeInterface;
20
use hiqdev\php\units\QuantityInterface;
21
22
/**
23
 * Chargable Action.
24
 *
25
 * @see ActionInterface
26
 *
27
 * @author Andrii Vasyliev <[email protected]>
28
 */
29
abstract class AbstractAction implements ActionInterface
30
{
31
    /**
32
     * @var int
33
     */
34
    protected $id;
35
36
    /**
37
     * @var TypeInterface
38
     */
39
    protected $type;
40
41
    /**
42
     * @var TargetInterface
43
     */
44
    protected $target;
45
46
    /**
47
     * @var QuantityInterface
48
     */
49
    protected $quantity;
50
51
    /**
52
     * @var CustomerInterface
53
     */
54
    protected $customer;
55
56
    /**
57
     * @var SaleInterface
58
     */
59
    protected $sale;
60
61
    /**
62
     * @var DateTime
63
     */
64
    protected $time;
65
66
    /**
67
     * @param TypeInterface $type
68
     * @param TargetInterface $target
69
     * @param QuantityInterface $quantity
70
     * @param CustomerInterface $customer
71
     * @param SaleInterface $sale
72
     * @param DateTime $time
73
     */
74 5 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
                            $id,
76
        TypeInterface       $type,
77
        TargetInterface     $target,
78
        QuantityInterface   $quantity,
79
        CustomerInterface   $customer = null,
80
        SaleInterface       $sale = null,
81
        DateTime            $time = null
82
    ) {
83 5
        $this->id       = $id;
84 5
        $this->type     = $type;
85 5
        $this->target   = $target;
86 5
        $this->quantity = $quantity;
87 5
        $this->customer = $customer;
88 5
        $this->sale     = $sale;
89 5
        $this->time     = $time;
90 5
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function getId()
96
    {
97
        return $this->id;
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103 1
    public function getCustomer()
104
    {
105 1
        return $this->customer;
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111 4
    public function getTarget()
112
    {
113 4
        return $this->target;
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119 2
    public function getType()
120
    {
121 2
        return $this->type;
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127 5
    public function getQuantity()
128
    {
129 5
        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...
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135
    public function getSale()
136
    {
137
        return $this->sale;
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143
    public function getTime()
144
    {
145
        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...
146
    }
147
148 View Code Duplication
    public function setId($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
149
    {
150
        if ($this->id === $id) {
151
            return;
152
        }
153
        if ($this->id !== null) {
154
            throw new \Exception('cannot reassign action id');
155
        }
156
        $this->id = $id;
157
    }
158
159
    public function setSale(SaleInterface $sale)
160
    {
161
        if ($this->sale !== null) {
162
            throw new \Exception('cannot reassign sale for action');
163
        }
164
        $this->sale = $sale;
165
    }
166
167
    /**
168
     * {@inheritdoc}
169
     */
170
    public function jsonSerialize()
171
    {
172
        return get_object_vars($this);
173
    }
174
175
    /**
176
     * {@inheritdoc}
177
     */
178 5
    public function calculateCharge(PriceInterface $price)
179
    {
180 5
        if (!$this->isApplicable($price)) {
181 3
            return null;
182
        }
183
184 5
        $usage = $price->calculateUsage($this->getQuantity());
185 5
        if ($usage === null) {
186 1
            return null;
187
        }
188
189 4
        $sum = $price->calculateSum($this->getQuantity());
190 4
        if ($sum === null) {
191
            return null;
192
        }
193
194 4
        return new Charge(null, $this, $price, $this->getTarget(), $usage, $sum);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \hiqdev\php\b...arget(), $usage, $sum); (hiqdev\php\billing\charge\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...
195
    }
196
197
    /**
198
     * {@inheritdoc}
199
     */
200
    abstract public function isApplicable(PriceInterface $price);
201
}
202