Completed
Push — master ( b864d9...3fa98b )
by Dmitry
07:42 queued 03:17
created

BillGridView   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 190
Duplicated Lines 11.58 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 10
dl 22
loc 190
ccs 0
cts 147
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
C columns() 22 139 11
A tariffLink() 0 4 1
A objectTag() 0 4 2
A objectLink() 0 6 2
A formatQuantity() 0 10 2

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-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\finance\grid;
12
13
use hipanel\grid\CurrencyColumn;
14
use hipanel\grid\MainColumn;
15
use hipanel\helpers\StringHelper;
16
use hipanel\helpers\Url;
17
use hipanel\modules\finance\logic\bill\QuantityFormatterFactoryInterface;
18
use hipanel\modules\finance\menus\BillActionsMenu;
19
use hipanel\modules\finance\models\Bill;
20
use hipanel\modules\finance\widgets\BillTypeFilter;
21
use hipanel\modules\finance\widgets\ColoredBalance;
22
use hipanel\widgets\ArraySpoiler;
23
use hiqdev\yii2\menus\grid\MenuColumn;
24
use Yii;
25
use yii\helpers\Html;
26
27
/**
28
 * Class BillGridView
29
 *
30
 * @author Dmytro Naumenko <[email protected]>
31
 */
32
class BillGridView extends \hipanel\grid\BoxedGridView
33
{
34
    /**
35
     * @var QuantityFormatterFactoryInterface
36
     */
37
    private $quantityFactory;
38
39
    public function __construct(QuantityFormatterFactoryInterface $quantityFactory, array $config = [])
40
    {
41
        parent::__construct($config);
42
43
        $this->quantityFactory = $quantityFactory;
44
    }
45
46
    public $currencies = [];
47
48
    public function columns()
49
    {
50
        return array_merge(parent::columns(), [
51
            'bill' => [
52
                'class' => MainColumn::class,
53
                'attribute' => 'bill',
54
                'filterAttribute' => 'bill_like',
55
            ],
56
            'time' => [
57
                'format' => 'html',
58
                'filter' => false,
59
                'contentOptions' => ['class' => 'text-nowrap'],
60 View Code Duplication
                'value' => function ($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...
61
                    list($date, $time) = explode(' ', $model->time, 2);
62
63
                    if (\in_array($model->gtype, [
64
                        'discount', 'domain', 'monthly', 'overuse', 'premium_package',
65
                        'feature', 'intercept', 'periodic',
66
                    ], true)) {
67
                        return Yii::$app->formatter->asDate($date, 'LLLL y');
68
                    }
69
70
                    return $time === '00:00:00' ? Yii::$app->formatter->asDate($date) : Yii::$app->formatter->asDateTime($model->time);
71
                },
72
            ],
73
            'sum' => [
74
                'class' => CurrencyColumn::class,
75
                'attribute' => 'sum',
76
                'colors' => ['danger' => 'warning'],
77
                'headerOptions' => ['class' => 'text-right'],
78
                'contentOptions' => function ($model) {
79
                    return ['class' => 'text-right' . ($model->sum > 0 ? ' text-bold' : '')];
80
                },
81
            ],
82
            'sum_editable' => [
83
                'class' => CurrencyColumn::class,
84
                'format' => 'raw',
85
                'attribute' => 'sum',
86
                'colors' => ['danger' => 'warning'],
87
                'headerOptions' => ['class' => 'text-right'],
88
                'urlCallback' => function ($model, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key 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...
89
                    return Yii::$app->user->can('bill.read')
90
                        ? Url::to(['@bill/view', 'id' => $model->id])
91
                        : null;
92
                },
93
                'contentOptions' => function ($model) {
94
                    return ['class' => 'text-right' . ($model->sum > 0 ? ' text-bold' : '')];
95
                },
96
            ],
97
            'quantity' => [
98
                'format' => 'raw',
99
                'headerOptions' => ['class' => 'text-right'],
100
                'contentOptions' => ['class' => 'text-right text-bold'],
101
                'value' => function (Bill $bill) {
102
                    return $this->formatQuantity($bill);
103
                }
104
            ],
105
            'balance' => [
106
                'attribute' => 'balance',
107
                'format' => 'raw',
108
                'headerOptions' => ['class' => 'text-right'],
109
                'contentOptions' => function ($model, $key, $index) {
110
                    return ['class' => 'text-right' . ($index ? '' : ' text-bold')];
111
                },
112
                'value' => function ($model) {
113
                    return ColoredBalance::widget(compact('model'));
114
                },
115
                'filterAttribute' => 'currency_in',
116
                'filterOptions' => ['class' => 'narrow-filter'],
117
                'filter' => function ($column, $filterModel) {
118
                    $currencies = array_combine(array_keys($this->currencies), array_map(function ($k) {
119
                        return StringHelper::getCurrencySymbol($k);
120
                    }, array_keys($this->currencies)));
121
122
                    return Html::activeDropDownList($filterModel, 'currency_in', $currencies, ['class' => 'form-control', 'prompt' => '--']);
123
                }
124
            ],
125
            'gtype' => [
126
                'attribute' => 'gtype',
127
            ],
128
            'type_label' => [
129
                'filter' => function ($column, $filterModel) {
130
                    return BillTypeFilter::widget([
131
                        'options' => ['class' => 'form-control text-right'],
132
                        'attribute' => 'ftype',
133
                        'model' => $filterModel,
134
                    ]);
135
                },
136
                'format' => 'raw',
137
                'headerOptions' => ['class' => 'text-right'],
138
                'contentOptions' => function ($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...
139
                    return ['class' => 'text-right'];
140
                },
141 View Code Duplication
                'value' => function ($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...
142
                    static $colors = [
143
                        'correction' => 'normal',
144
                        'exchange' => 'warning',
145
                        'deposit' => 'success',
146
                    ];
147
                    $color = $colors[$model->gtype] ?: 'muted';
148
149
                    return Html::tag('b', Yii::t('hipanel:finance', $model->type_label), ['class' => "text-$color"]);
150
                },
151
            ],
152
            'description' => [
153
                'attribute' => 'descr',
154
                'format' => 'raw',
155
                'value' => function ($model) {
156
                    $descr = $model->descr ?: $model->label;
157
                    $text = mb_strlen($descr) > 70 ? ArraySpoiler::widget(['data' => $descr]) : $descr;
158
                    $tariff = $model->tariff ? Html::tag('span',
159
                        Yii::t('hipanel', 'Tariff') . ': ' . Html::a($model->tariff,
160
                            ['@tariff/view', 'id' => $model->tariff_id]), ['class' => 'pull-right']) : '';
161
                    $amount = $this->formatQuantity($model);
162
                    $object = $this->objectTag($model);
163
164
                    return $tariff . $amount . ' ' . implode('<br>', array_filter([$object, $text]));
165
                },
166
            ],
167
            'tariff_link' => [
168
                'attribute' => 'tariff',
169
                'format' => 'html',
170
                'value' => function ($model) {
171
                    return $this->tariffLink($model);
172
                },
173
            ],
174
            'object' => [
175
                'attribute' => 'object',
176
                'format' => 'html',
177
                'value' => function ($model) {
178
                    return $this->objectTag($model);
179
                },
180
            ],
181
            'actions' => [
182
                'class' => MenuColumn::class,
183
                'menuClass' => BillActionsMenu::class,
184
            ],
185
        ]);
186
    }
187
188
    public function tariffLink($model): string
189
    {
190
        return Html::a($model->tariff, ['@tariff/view', 'id' => $model->tariff_id]);
191
    }
192
193
    public function objectTag($model): string
194
    {
195
        return $model->object ? implode(':&nbsp;', [$model->class_label, $this->objectLink($model)]) : '';
196
    }
197
198
    /**
199
     * Creates link to object details page.
200
     *
201
     * @param Bill $model
202
     * @return string
203
     */
204
    public function objectLink(Bill $model): string
205
    {
206
        return $model->class === 'device'
0 ignored issues
show
Documentation introduced by
The property class does not exist on object<hipanel\modules\finance\models\Bill>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
207
            ? Html::a($model->object, ['@server/view', 'id' => $model->object_id])
0 ignored issues
show
Documentation introduced by
The property object does not exist on object<hipanel\modules\finance\models\Bill>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property object_id does not exist on object<hipanel\modules\finance\models\Bill>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
208
            : Html::tag('b', $model->object);
0 ignored issues
show
Documentation introduced by
The property object does not exist on object<hipanel\modules\finance\models\Bill>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
209
    }
210
211
    private function formatQuantity(Bill $bill): string
212
    {
213
        $billQty = $this->quantityFactory->forBill($bill);
214
215
        if ($billQty !== null) {
216
            return Html::tag('nobr', Html::tag('b', $billQty->format()));
217
        }
218
219
        return '';
220
    }
221
}
222