Completed
Push — master ( 14a2bb...4a945d )
by Andrii
02:47
created

AbstractAction::jsonSerialize()   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 hiqdev\php\billing\charge\Charge;
14
use hiqdev\php\billing\price\PriceInterface;
15
use hiqdev\php\billing\target\TargetInterface;
16
use hiqdev\php\billing\type\TypeInterface;
17
use hiqdev\php\billing\customer\CustomerInterface;
18
use DateTime;
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 getCustomer()
86
    {
87
        return $this->customer;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function getTarget()
94
    {
95
        return $this->target;
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101 3
    public function getType()
102
    {
103 3
        return $this->type;
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109 3
    public function getQuantity()
110
    {
111 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...
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function getTime()
118
    {
119
        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...
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public function jsonSerialize()
126
    {
127
        return get_object_vars($this);
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133 3
    public function calculateCharge(PriceInterface $price)
134
    {
135 3
        if (!$this->isApplicable($price)) {
136 1
            return null;
137
        }
138
139 3
        $usage = $price->calculateUsage($this->getQuantity());
140 3
        if ($usage === null) {
141 1
            return null;
142
        }
143
144 2
        $sum = $price->calculateSum($this->getQuantity());
145 2
        if ($sum === null) {
146
            return null;
147
        }
148
149 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...
150
    }
151
152
    /**
153
     * {@inheritdoc}
154
     */
155
    abstract public function isApplicable(PriceInterface $price);
156
}
157