Completed
Push — master ( 663ac6...abd7c7 )
by Andrii
01:58
created

AbstractPrice   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 118
Duplicated Lines 21.19 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 74.19%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 5
dl 25
loc 118
ccs 23
cts 31
cp 0.7419
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 11 11 1
A getId() 0 4 1
A getType() 0 4 1
A getTarget() 0 4 1
A getPlan() 0 4 1
A calculateSum() 14 14 3
A create() 0 4 1
A jsonSerialize() 0 8 1
A isApplicable() 0 5 2
calculateUsage() 0 1 ?
calculatePrice() 0 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\price;
12
13
use hiqdev\php\billing\action\ActionInterface;
14
use hiqdev\php\billing\EntityInterface;
15
use hiqdev\php\billing\plan\PlanInterface;
16
use hiqdev\php\billing\target\TargetInterface;
17
use hiqdev\php\billing\type\TypeInterface;
18
use hiqdev\php\units\QuantityInterface;
19
20
/**
21
 * Price.
22
 * @see PriceInterface
23
 * By default Price is applicable when same target and same type as Action.
24
 * But it can be different e.g. same price for all targets when certain type.
25
 *
26
 * @author Andrii Vasyliev <[email protected]>
27
 */
28
abstract class AbstractPrice implements PriceInterface, EntityInterface
29
{
30
    /**
31
     * @var integer
32
     */
33
    protected $id;
34
35
    /**
36
     * @var TypeInterface
37
     */
38
    protected $type;
39
40
    /**
41
     * @var TargetInterface
42
     */
43
    protected $target;
44
45
    /**
46
     * @var PlanInterface
47
     */
48
    protected $plan;
49
50 7 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...
51
                            $id,
52
        TypeInterface       $type,
53
        TargetInterface     $target,
54
        PlanInterface       $plan = null
55
    ) {
56 7
        $this->id = $id;
57 7
        $this->type = $type;
58 7
        $this->target = $target;
59 7
        $this->plan = $plan;
60 7
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 2
    public function getId()
66
    {
67 2
        return $this->id;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 6
    public function getType()
74
    {
75 6
        return $this->type;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 3
    public function getTarget()
82
    {
83 3
        return $this->target;
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 1
    public function getPlan()
90
    {
91 1
        return $this->plan;
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     * Default sum calculation method: sum = price * usage.
97
     */
98 1 View Code Duplication
    public function calculateSum(QuantityInterface $quantity)
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...
99
    {
100 1
        $usage = $this->calculateUsage($quantity);
101 1
        if ($usage === null) {
102
            return null;
103
        }
104
105 1
        $price = $this->calculatePrice($quantity);
106 1
        if ($price === null) {
107
            return null;
108
        }
109
110 1
        return $price->multiply($usage->getQuantity());
111
    }
112
113
    public static function create(array $data)
114
    {
115
        return new SinglePrice($data['id'], $data['target']);
0 ignored issues
show
Bug introduced by
The call to SinglePrice::__construct() misses some required arguments starting with $target.
Loading history...
116
    }
117
118
    public function jsonSerialize()
119
    {
120
        return [
121
            'id' => $this->id,
122
            'type' => $this->type,
123
            'target' => $this->target,
124
        ];
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130 5
    public function isApplicable(ActionInterface $action)
131
    {
132 5
        return $this->target->equals($action->getTarget()) &&
133 5
               $this->type->equals($action->getType());
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139
    abstract public function calculateUsage(QuantityInterface $quantity);
140
141
    /**
142
     * {@inheritdoc}
143
     */
144
    abstract public function calculatePrice(QuantityInterface $action);
145
}
146