Completed
Push — master ( 00bad6...c7c8a3 )
by Dmitry
06:29
created

DomainServicePriceGridView   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 49
Duplicated Lines 59.18 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 29
loc 49
ccs 0
cts 27
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A columns() 0 7 1
B getPriceGrid() 29 29 6

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
namespace hipanel\modules\finance\grid;
4
5
use hipanel\modules\finance\models\DomainServicePrice;
6
use hipanel\modules\finance\widgets\PriceDifferenceWidget;
7
use hipanel\modules\finance\widgets\ResourcePriceWidget;
8
use yii\helpers\Html;
9
10
class DomainServicePriceGridView extends PriceGridView
11
{
12
    /**
13
     * @var DomainServicePrice[]
14
     */
15
    public $parentPrices;
16
17
    public function columns()
18
    {
19
        return array_merge(parent::columns(), [
20
            'purchase' => $this->getPriceGrid('feature,premium_dns_purchase'),
21
            'renewal' => $this->getPriceGrid('feature,premium_dns_renew'),
22
        ]);
23
    }
24
25
    /**
26
     * @param string $type
27
     * @return array
28
     */
29 View Code Duplication
    private function getPriceGrid(string $type): array
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...
30
    {
31
        return [
32
            'label' =>  DomainServicePrice::getOperations()[$type],
33
            'contentOptions' => ['class' => 'text-center'],
34
            'format' => 'raw',
35
            'value' => function($prices) use ($type) {
36
                /** @var DomainServicePrice[] $prices */
37
                if (!isset($prices[$type])) {
38
                    return '';
39
                }
40
                $price = $prices[$type];
41
                $parent = $this->parentPrices[$type] ?? null;
42
                $parentValue = $parent ? PriceDifferenceWidget::widget([
43
                    'new' => $price->price,
44
                    'old' => $parent->price,
45
                ]) : '';
46
                $priceValue = floatval($price->price) ||
47
                (!floatval($price->price) && $parentValue) ?
48
                    ResourcePriceWidget::widget([
49
                        'price' => $price->price,
50
                        'currency' => $price->currency
51
                    ]) : '';
52
                $options = ['class' => 'col-md-6'];
53
                return Html::tag('div', $priceValue, $options) .
54
                    Html::tag('div', $parentValue, $options);
55
            }
56
        ];
57
    }
58
}
59