Completed
Push — master ( 428142...9f94b9 )
by Andrii
02:14
created

Charge   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 135
Duplicated Lines 20 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 50%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 17
lcom 2
cbo 2
dl 27
loc 135
ccs 21
cts 42
cp 0.5
rs 10
c 1
b 0
f 1

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 17 17 1
A sumUp() 0 13 3
A getId() 0 4 1
A getAction() 0 4 1
A getTarget() 0 4 1
A getPrice() 0 4 1
A getUsage() 0 4 1
A getSum() 0 4 1
A calculatePrice() 0 6 2
A setId() 10 10 3
A jsonSerialize() 0 4 1
A getTime() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\price\PriceInterface;
16
use hiqdev\php\billing\target\TargetInterface;
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 ChargeInterface
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 4 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 4
        $this->id       = $id;
75 4
        $this->action   = $action;
76 4
        $this->price    = $price;
77 4
        $this->target   = $target;
78 4
        $this->usage    = $usage;
79 4
        $this->sum      = $sum;
80 4
        $this->time     = $time;
81 4
    }
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 4
    public function getAction()
108
    {
109 4
        return $this->action;
110
    }
111
112 4
    public function getTarget()
113
    {
114 4
        return $this->target;
115
    }
116
117
    /**
118
     * @return PriceInterface
119
     */
120 4
    public function getPrice()
121
    {
122 4
        return $this->price;
123
    }
124
125 4
    public function getUsage()
126
    {
127 4
        return $this->usage;
128
    }
129
130 4
    public function getSum()
131
    {
132 4
        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 1
    public function getTime()
143
    {
144 1
        return $this->time;
145
    }
146
147 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...
148
    {
149
        if ($this->id === $id) {
150
            return;
151
        }
152
        if ($this->id !== null) {
153
            throw new \Exception('cannot reassign sale id');
154
        }
155
        $this->id = $id;
156
    }
157
158
    public function jsonSerialize()
159
    {
160
        return get_object_vars($this);
161
    }
162
}
163