Completed
Push — master ( bc0444...122bd3 )
by Andrii
01:23
created

Charge::getTarget()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 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\charge;
12
13
use hiqdev\php\billing\action\ActionInterface;
14
use hiqdev\php\billing\target\TargetInterface;
15
use hiqdev\php\billing\type\TypeInterface;
16
use hiqdev\php\units\Quantity;
17
use hiqdev\php\units\QuantityInterface;
18
use Money\Money;
19
20
/**
21
 * Charge.
22
 *
23
 * [[Action]] is charged with a number of [[Charge]]s.
24
 *
25
 * @author Andrii Vasyliev <[email protected]>
26
 */
27
class Charge implements \JsonSerializable
28
{
29
    /**
30
     * @var int
31
     */
32
    protected $id;
33
34
    /**
35
     * @var ActionInterface
36
     */
37
    protected $action;
38
39
    /**
40
     * @var TypeInterface
41
     */
42
    protected $type;
43
44
    /**
45
     * @var TargetInterface
46
     */
47
    protected $target;
48
49
    /**
50
     * @var QuantityInterface
51
     */
52
    protected $usage;
53
54
    /**
55
     * @var Money
56
     */
57
    protected $sum;
58
59 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...
60
                            $id,
61
        ActionInterface     $action = null,
62
        TypeInterface       $type = null,
63
        TargetInterface     $target = null,
64
        QuantityInterface   $usage,
65
        Money               $sum
66
    ) {
67 3
        $this->id       = $id;
68 3
        $this->action   = $action;
69 3
        $this->type     = $type;
70 3
        $this->target   = $target;
71 3
        $this->usage    = $usage;
72 3
        $this->sum      = $sum;
73 3
    }
74
75
    /**
76
     * Returns charge that is sum of given charges.
77
     * @param Charge[] $charges
78
     * @return Charge
79
     */
80
    public static function sumUp(array $charges)
81
    {
82
        if (empty($charges)) {
83
            return new self(null, null, null, null, Quantity::item(0), Money::USD(0));
84
        }
85
86
        $first = array_unshift($charges);
87
        if (empty($charges)) {
88
            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...
89
        }
90
91
        throw new \Exception('Not implemented Charge::sumUp');
92
    }
93
94
    public function getId()
95
    {
96
        return $this->id;
97
    }
98
99 3
    public function getAction()
100
    {
101 3
        return $this->action;
102
    }
103
104 3
    public function getTarget()
105
    {
106 3
        return $this->target;
107
    }
108
109 3
    public function getType()
110
    {
111 3
        return $this->type;
112
    }
113
114 3
    public function getUsage()
115
    {
116 3
        return $this->usage;
117
    }
118
119 3
    public function getSum()
120
    {
121 3
        return $this->sum;
122
    }
123
124
    public function getPrice()
125
    {
126
        $usage = $this->usage->getQuantity();
127
128
        return $usage ? $this->sum->divide($usage) : $this->sum;
129
    }
130
131
    public function jsonSerialize()
132
    {
133
        return get_object_vars($this);
134
    }
135
}
136