PriceGridView   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 145
Duplicated Lines 5.52 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 9
dl 8
loc 145
ccs 0
cts 106
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B columns() 8 131 3

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
 * Finance module for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-finance
6
 * @package   hipanel-module-finance
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\finance\grid;
12
13
use hipanel\grid\RefColumn;
14
use hipanel\modules\finance\grid\presenters\price\PricePresenter;
15
use hipanel\modules\finance\grid\presenters\price\PricePresenterFactory;
16
use hipanel\modules\finance\menus\PriceActionsMenu;
17
use hipanel\modules\finance\models\Price;
18
use hipanel\modules\finance\widgets\BillType;
19
use hipanel\modules\finance\widgets\LinkToObjectResolver;
20
use hiqdev\yii2\menus\grid\MenuColumn;
21
use Yii;
22
use yii\bootstrap\Html;
23
24
/**
25
 * Class PriceGridView.
26
 *
27
 * @author Dmytro Naumenko <[email protected]>
28
 */
29
class PriceGridView extends \hipanel\grid\BoxedGridView
30
{
31
    /**
32
     * @var \hipanel\modules\finance\grid\presenters\price\PricePresenterFactory
33
     */
34
    private $presenterFactory;
35
36
    public function __construct(PricePresenterFactory $presenterFactory, array $config = [])
37
    {
38
        parent::__construct($config);
39
        $this->presenterFactory = $presenterFactory;
40
    }
41
42
    public function columns()
43
    {
44
        return array_merge(parent::columns(), [
45
            'plan' => [
46
                'format' => 'raw',
47
                'filterAttribute' => 'plan_name_ilike',
48
                'filterOptions' => ['class' => 'narrow-filter'],
49
                'value' => function (Price $model) {
50
                    return Html::a($model->plan->name, ['@plan/view', 'id' => $model->plan->id]);
51
                },
52
            ],
53
            'price' => [
54
                'label' => Yii::t('hipanel.finance.price', 'Price'),
55
                'format' => 'raw',
56
                'value' => function (Price $model) {
57
                    return $this->presenterFactory->build(\get_class($model))->renderPrice($model);
58
                },
59
            ],
60
            'old_price' => [
61
                'label' => Yii::t('hipanel.finance.price', 'Old price'),
62
                'format' => 'raw',
63
                'value' => function (Price $model): string {
64
                    /** @var PricePresenter $presenter */
65
                    $presenter = $this->presenterFactory->build(\get_class($model));
66
                    return $presenter
67
                            ->setPriceAttribute('old_price')
68
                            ->renderPrice($model);
69
                },
70
            ],
71
            'object->name_clear' => [
72
                'label' => Yii::t('hipanel', 'Object'),
73
                'format' => 'raw',
74
                'value' => function (Price $model) {
75
                    return $model->object->name ?: Yii::t('hipanel.finance.price', 'Any');
76
                },
77
            ],
78
            'object->name' => [
79
                'label' => Yii::t('hipanel', 'Object'),
80
                'format' => 'raw',
81 View Code Duplication
                'value' => function (Price $model) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
82
                    $link = LinkToObjectResolver::widget([
83
                        'model' => $model->object,
84
                        'labelAttribute' => 'name',
85
                    ]);
86
87
                    return $link ?: Yii::t('hipanel.finance.price', 'Any');
88
                },
89
            ],
90
            'object->name-any' => [
91
                'label' => Yii::t('hipanel', 'Object'),
92
                'value' => function (Price $model) {
0 ignored issues
show
Unused Code introduced by
The parameter $model is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
93
                    return Yii::t('hipanel.finance.price', 'Any');
94
                },
95
            ],
96
            'object->label' => [
97
                'format' => 'raw',
98
                'label' => Yii::t('hipanel', 'Details'),
99
                'value' => function (Price $model) {
100
                    return $model->object->label;
101
                },
102
            ],
103
            'type' => [
104
                'class' => RefColumn::class,
105
                'label' => Yii::t('hipanel', 'Type'),
106
                'attribute' => 'type',
107
                'filterAttribute' => 'type',
108
                'filterOptions' => ['class' => 'narrow-filter'],
109
                'format' => 'raw',
110
                'gtype' => 'type,bill',
111
                'findOptions' => [
112
                    'select' => 'name',
113
                    'pnames' => 'monthly,overuse',
114
                    'with_recursive' => 1,
115
                ],
116
                'value' => function ($model) {
117
                    return BillType::widget(['model' => $model]);
118
                },
119
            ],
120
            'unit' => [
121
                'class' => RefColumn::class,
122
                'attribute' => 'unit',
123
                'filterAttribute' => 'unit',
124
                'filterOptions' => ['class' => 'narrow-filter'],
125
                'format' => 'raw',
126
                'gtype' => 'type,unit',
127
                'i18nDictionary' => 'hipanel.finance.units',
128
                'findOptions' => [
129
                    'with_recursive' => 1,
130
                    'select' => 'name_label',
131
                    'mapOptions' => ['from' => 'name'],
132
                ],
133
                'value' => function (Price $model) {
134
                    return $model->getUnitLabel();
135
                },
136
            ],
137
            'currency' => [
138
                'class' => RefColumn::class,
139
                'attribute' => 'currency',
140
                'filterAttribute' => 'currency',
141
                'filterOptions' => ['class' => 'narrow-filter'],
142
                'format' => 'raw',
143
                'gtype' => 'type,currency',
144
            ],
145
            'actions' => [
146
                'class' => MenuColumn::class,
147
                'menuClass' => PriceActionsMenu::class,
148
            ],
149
            'info' => [
150
                'format' => 'raw',
151
                'label' => Yii::t('hipanel', 'Details'),
152
                'value' => function (Price $model) {
153
                    return $this->presenterFactory->build(\get_class($model))->renderInfo($model);
154
                },
155
            ],
156
            'old_quantity' => [
157
                'format' => 'raw',
158
                'label' => Yii::t('hipanel.finance.price', 'Old quantity'),
159
                'value' => function (Price $model) {
160
                    return $this->presenterFactory->build(\get_class($model))->renderInfo($model, 'old_quantity');
161
                },
162
            ],
163
            'value' => [
164
                'class' => ValueColumn::class,
165
            ],
166
            'rate' => [
167
                'label' => Yii::t('hipanel.finance.price', 'Referral rate'),
168
                'attribute' => 'rate',
169
                'filter' => false,
170
            ],
171
        ]);
172
    }
173
}
174