Completed
Push — master ( 427141...dd7e2a )
by Dmitry
05:54
created

Charge   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 116
Duplicated Lines 8.62 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 60%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 14
lcom 2
cbo 2
dl 10
loc 116
ccs 21
cts 35
cp 0.6
rs 10
c 1
b 0
f 1

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 1
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 getTime() 0 4 1
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, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\billing\charge;
12
13
use DateTimeImmutable;
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\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 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
    /**
60
     * @var DateTimeImmutable
61
     */
62
    protected $time;
63
64 4
    public function __construct(
65
                            $id,
66
        ActionInterface     $action = null,
67
        PriceInterface      $price = null,
68
        TargetInterface     $target = null,
69
        QuantityInterface   $usage,
70
        Money               $sum,
71
        DateTimeImmutable   $time = null
72
    ) {
73 4
        $this->id       = $id;
74 4
        $this->action   = $action;
75 4
        $this->price    = $price;
76 4
        $this->target   = $target;
77 4
        $this->usage    = $usage;
78 4
        $this->sum      = $sum;
79 4
        $this->time     = $time;
80 4
    }
81
82
    public function getId()
83
    {
84
        return $this->id;
85
    }
86
87 4
    public function getAction()
88
    {
89 4
        return $this->action;
90
    }
91
92 4
    public function getTarget()
93
    {
94 4
        return $this->target;
95
    }
96
97
    /**
98
     * @return PriceInterface
99
     */
100 4
    public function getPrice()
101
    {
102 4
        return $this->price;
103
    }
104
105 4
    public function getUsage()
106
    {
107 4
        return $this->usage;
108
    }
109
110 4
    public function getSum()
111
    {
112 4
        return $this->sum;
113
    }
114
115
    public function calculatePrice()
116
    {
117
        $usage = $this->usage->getQuantity();
118
119
        return $usage ? $this->sum->divide($usage) : $this->sum;
120
    }
121
122 1
    public function getTime()
123
    {
124 1
        return $this->time;
125
    }
126
127 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...
128
    {
129
        if ($this->id === $id) {
130
            return;
131
        }
132
        if ($this->id !== null) {
133
            throw new \Exception('cannot reassign sale id');
134
        }
135
        $this->id = $id;
136
    }
137
138
    public function jsonSerialize()
139
    {
140
        return get_object_vars($this);
141
    }
142
}
143