Completed
Push — master ( cc81ea...b18114 )
by Andrii
04:47
created

Charge   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 117
Duplicated Lines 8.55 %

Coupling/Cohesion

Components 3
Dependencies 2

Test Coverage

Coverage 41.03%

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 16
lcom 3
cbo 2
dl 10
loc 117
ccs 16
cts 39
cp 0.4103
rs 10
c 2
b 0
f 2

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
A getId() 0 4 1
A getAction() 0 4 1
A getPrice() 0 4 1
A getUsage() 0 4 1
A getSum() 0 4 1
A calculatePrice() 0 6 2
A getBill() 0 4 1
A hasBill() 0 4 1
A setBill() 0 7 2
A setId() 10 10 3
A jsonSerialize() 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-2018, 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\bill\BillInterface;
15
use hiqdev\php\billing\price\PriceInterface;
16
use hiqdev\php\billing\target\TargetInterface;
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 ChargeInterface
28
{
29
    /**
30
     * @var int
31
     */
32
    protected $id;
33
34
    /**
35
     * @var ActionInterface
36
     */
37
    protected $action;
38
39
    /**
40
     * @var PriceInterface
41
     */
42
    protected $price;
43
44
    /**
45
     * @var QuantityInterface
46
     */
47
    protected $usage;
48
49
    /**
50
     * @var Money
51
     */
52
    protected $sum;
53
54
    /**
55
     * @var BillInterface
56
     */
57
    protected $bill;
58 5
59
    public function __construct(
60
                            $id,
61
        ActionInterface     $action,
62
        PriceInterface      $price,
63
        QuantityInterface   $usage,
64
        Money               $sum,
65
        BillInterface       $bill = null
66 5
    ) {
67 5
        $this->id       = $id;
68 5
        $this->action   = $action;
69 5
        $this->price    = $price;
70 5
        $this->usage    = $usage;
71 5
        $this->sum      = $sum;
72 5
        $this->bill     = $bill;
73
    }
74
75
    public function getId()
76
    {
77
        return $this->id;
78
    }
79 5
80
    public function getAction()
81 5
    {
82
        return $this->action;
83
    }
84
85
    /**
86
     * @return PriceInterface
87 5
     */
88
    public function getPrice()
89 5
    {
90
        return $this->price;
91
    }
92 5
93
    public function getUsage()
94 5
    {
95
        return $this->usage;
96
    }
97 5
98
    public function getSum()
99 5
    {
100
        return $this->sum;
101
    }
102
103
    public function calculatePrice()
104
    {
105
        $usage = $this->usage->getQuantity();
106
107
        return $usage ? $this->sum->divide($usage) : $this->sum;
108
    }
109
110
    public function getBill()
111
    {
112
        return $this->bill;
113
    }
114
115
    public function hasBill()
116
    {
117
        return $this->bill !== null;
118
    }
119
120
    public function setBill(BillInterface $bill)
121
    {
122
        if ($this->hasBill()) {
123
            throw new \Exception('cannot reassign sale bill');
124
        }
125
        $this->bill = $bill;
126
    }
127
128 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...
129
    {
130
        if ($this->id === $id) {
131
            return;
132
        }
133
        if ($this->id !== null) {
134
            throw new \Exception('cannot reassign sale id');
135
        }
136
        $this->id = $id;
137
    }
138
139
    public function jsonSerialize()
140
    {
141
        return get_object_vars($this);
142
    }
143
}
144