Completed
Push — master ( a5de38...845fca )
by Andrii
06:07
created

Charge::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\charge;
12
13
use DateTime;
14
use hiqdev\php\billing\action\ActionInterface;
15
use hiqdev\php\billing\target\TargetInterface;
16
use hiqdev\php\billing\price\PriceInterface;
17
use hiqdev\php\units\Quantity;
18
use hiqdev\php\units\QuantityInterface;
19
use Money\Money;
20
21
/**
22
 * Charge.
23
 *
24
 * [[Action]] is charged with a number of [[Charge]]s.
25
 *
26
 * @author Andrii Vasyliev <[email protected]>
27
 */
28
class Charge implements \JsonSerializable
29
{
30
    /**
31
     * @var int
32
     */
33
    protected $id;
34
35
    /**
36
     * @var ActionInterface
37
     */
38
    protected $action;
39
40
    /**
41
     * @var PriceInterface
42
     */
43
    protected $price;
44
45
    /**
46
     * @var TargetInterface
47
     */
48
    protected $target;
49
50
    /**
51
     * @var QuantityInterface
52
     */
53
    protected $usage;
54
55
    /**
56
     * @var Money
57
     */
58
    protected $sum;
59
60
    /**
61
     * @var DateTime
62
     */
63
    protected $time;
64
65 3 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...
66
                            $id,
67
        ActionInterface     $action = null,
68
        PriceInterface      $price = null,
69
        TargetInterface     $target = null,
70
        QuantityInterface   $usage,
71
        Money               $sum,
72
        DateTime            $time = null
73
    ) {
74 3
        $this->id       = $id;
75 3
        $this->action   = $action;
76 3
        $this->price    = $price;
77 3
        $this->target   = $target;
78 3
        $this->usage    = $usage;
79 3
        $this->sum      = $sum;
80 3
        $this->time     = $time;
81 3
    }
82
83
    /**
84
     * Returns charge that is sum of given charges.
85
     * @param Charge[] $charges
86
     * @return Charge
87
     */
88
    public static function sumUp(array $charges)
89
    {
90
        if (empty($charges)) {
91
            return new self(null, null, null, null, Quantity::item(0), Money::USD(0));
92
        }
93
94
        $first = array_unshift($charges);
95
        if (empty($charges)) {
96
            return $first;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $first; (integer) is incompatible with the return type documented by hiqdev\php\billing\charge\Charge::sumUp of type hiqdev\php\billing\charge\Charge.

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...
97
        }
98
99
        throw new \Exception('Not implemented Charge::sumUp');
100
    }
101
102
    public function getId()
103
    {
104
        return $this->id;
105
    }
106
107 3
    public function getAction()
108
    {
109 3
        return $this->action;
110
    }
111
112 3
    public function getTarget()
113
    {
114 3
        return $this->target;
115
    }
116
117
    /**
118
     * @return PriceInterface
119
     */
120 3
    public function getPrice()
121
    {
122 3
        return $this->price;
123
    }
124
125 3
    public function getUsage()
126
    {
127 3
        return $this->usage;
128
    }
129
130 3
    public function getSum()
131
    {
132 3
        return $this->sum;
133
    }
134
135
    public function calculatePrice()
136
    {
137
        $usage = $this->usage->getQuantity();
138
139
        return $usage ? $this->sum->divide($usage) : $this->sum;
140
    }
141
142
    public function getTime()
143
    {
144
        return $this->time;
145
    }
146
147
    public function jsonSerialize()
148
    {
149
        return get_object_vars($this);
150
    }
151
}
152