Completed
Push — master ( 40a591...0978bf )
by Andrii
01:53
created

AbstractAction::hasParent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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\action;
12
13
use DateTimeImmutable;
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
 * Chargeable 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 DateTimeImmutable
63
     */
64
    protected $time;
65
66
    /**
67
     * @var ActionInterface
68
     */
69
    protected $parent;
70
71
    /**
72
     * @param TypeInterface $type
73
     * @param TargetInterface $target
74
     * @param QuantityInterface $quantity
75
     * @param CustomerInterface $customer
76
     * @param SaleInterface $sale
77
     * @param DateTimeImmutable $time
78
     * @param ActionInterface $parent
79
     */
80 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...
81
                            $id,
82
        TypeInterface       $type,
83
        TargetInterface     $target,
84
        QuantityInterface   $quantity,
85
        CustomerInterface   $customer = null,
86
        SaleInterface       $sale = null,
87
        DateTimeImmutable   $time = null,
88
        ActionInterface     $parent = null
89
    ) {
90 5
        $this->id       = $id;
91 5
        $this->type     = $type;
92 5
        $this->target   = $target;
93 5
        $this->quantity = $quantity;
94 5
        $this->customer = $customer;
95 5
        $this->sale     = $sale;
96 5
        $this->time     = $time;
97 5
        $this->parent   = $parent;
98 5
    }
99
100
    public function createSubaction(CustomerInterface $customer)
101
    {
102
        return new static(null, $this->type, $this->target, $this->quantity, $customer, $this->sale, $this->time, $this);
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function getId()
109
    {
110
        return $this->id;
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116 1
    public function getCustomer()
117
    {
118 1
        return $this->customer;
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124 4
    public function getTarget()
125
    {
126 4
        return $this->target;
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132 2
    public function getType()
133
    {
134 2
        return $this->type;
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140 5
    public function getQuantity()
141
    {
142 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...
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148
    public function getSale()
149
    {
150
        return $this->sale;
151
    }
152
153
    /**
154
     * {@inheritdoc}
155
     */
156
    public function getTime()
157
    {
158
        return $this->time;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->time; (DateTimeImmutable) is incompatible with the return type declared by the interface hiqdev\php\billing\action\ActionInterface::getTime of type hiqdev\php\billing\action\DateTimeImmutable.

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...
159
    }
160
161
    /**
162
     * {@inheritdoc}
163
     */
164
    public function getParent()
165
    {
166
        return $this->parent;
167
    }
168
169
    /**
170
     * {@inheritdoc}
171
     */
172
    public function hasParent()
173
    {
174
        return $this->parent !== null;
175
    }
176
177 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...
178
    {
179
        if ($this->id === $id) {
180
            return;
181
        }
182
        if ($this->id !== null) {
183
            throw new \Exception('cannot reassign action id');
184
        }
185
        $this->id = $id;
186
    }
187
188
    public function setSale(SaleInterface $sale)
189
    {
190
        if ($this->sale !== null) {
191
            throw new \Exception('cannot reassign sale for action');
192
        }
193
        $this->sale = $sale;
194
    }
195
196
    /**
197
     * {@inheritdoc}
198
     */
199
    public function jsonSerialize()
200
    {
201
        return get_object_vars($this);
202
    }
203
204
205
    /**
206
     * {@inheritdoc}
207 5
     */
208
    public function calculateCharge(PriceInterface $price)
209 5
    {
210 3
        if (!$this->isApplicable($price)) {
211
            return null;
212
        }
213 5
214 5
        $usage = $price->calculateUsage($this->getQuantity());
215 1
        if ($usage === null) {
216
            return null;
217
        }
218 4
219 4
        $sum = $price->calculateSum($this->getQuantity());
220
        if ($sum === null) {
221
            return null;
222
        }
223 4
224
        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...
225
    }
226
227
    /**
228
     * {@inheritdoc}
229
     */
230
    abstract public function isApplicable(PriceInterface $price);
231
}
232