Completed
Push — master ( 4a945d...f56bff )
by Andrii
02:24
created

AbstractAction::getId()   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 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\target\TargetInterface;
18
use hiqdev\php\billing\type\TypeInterface;
19
use hiqdev\php\units\QuantityInterface;
20
21
/**
22
 * Chargable Action.
23
 *
24
 * @see ActionInterface
25
 *
26
 * @author Andrii Vasyliev <[email protected]>
27
 */
28
abstract class AbstractAction implements ActionInterface
29
{
30
    /**
31
     * @var int
32
     */
33
    protected $id;
34
35
    /**
36
     * @var TypeInterface
37
     */
38
    protected $type;
39
40
    /**
41
     * @var TargetInterface
42
     */
43
    protected $target;
44
45
    /**
46
     * @var QuantityInterface
47
     */
48
    protected $quantity;
49
50
    /**
51
     * @var CustomerInterface
52
     */
53
    protected $customer;
54
55
    /**
56
     * @var DateTime
57
     */
58
    protected $time;
59
60
    /**
61
     * @param CustomerInterface $customer
62
     * @param TargetInterface $target
63
     * @param QuantityInterface $quantity
64
     * @param DateTime $time
65
     */
66 3
    public function __construct(
67
                            $id,
68
        TypeInterface       $type,
69
        TargetInterface     $target,
70
        QuantityInterface   $quantity,
71
        CustomerInterface   $customer = null,
72
        DateTime            $time = null
73
    ) {
74 3
        $this->id       = $id;
75 3
        $this->type     = $type;
76 3
        $this->target   = $target;
77 3
        $this->quantity = $quantity;
78 3
        $this->customer = $customer;
79 3
        $this->time     = $time;
80 3
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function getId()
86
    {
87
        return $this->id;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function getCustomer()
94
    {
95
        return $this->customer;
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function getTarget()
102
    {
103
        return $this->target;
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109 3
    public function getType()
110
    {
111 3
        return $this->type;
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117 3
    public function getQuantity()
118
    {
119 3
        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...
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public function getTime()
126
    {
127
        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...
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function jsonSerialize()
134
    {
135
        return get_object_vars($this);
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141 3
    public function calculateCharge(PriceInterface $price)
142
    {
143 3
        if (!$this->isApplicable($price)) {
144 1
            return null;
145
        }
146
147 3
        $usage = $price->calculateUsage($this->getQuantity());
148 3
        if ($usage === null) {
149 1
            return null;
150
        }
151
152 2
        $sum = $price->calculateSum($this->getQuantity());
153 2
        if ($sum === null) {
154
            return null;
155
        }
156
157 2
        return new Charge(null, $this, $price->getType(), $price->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...
158
    }
159
160
    /**
161
     * {@inheritdoc}
162
     */
163
    abstract public function isApplicable(PriceInterface $price);
164
}
165