StatisticGridView::columns()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 0
cts 35
cp 0
rs 9.312
c 0
b 0
f 0
cc 2
nc 1
nop 0
crap 6
1
<?php
2
/**
3
 * HiPanel tickets module
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-ticket
6
 * @package   hipanel-module-ticket
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\ticket\grid;
12
13
use hipanel\modules\client\grid\ClientColumn;
14
use hipanel\widgets\ClientSellerLink;
15
use yii\helpers\Html;
16
use yii\helpers\Url;
17
use Yii;
18
19
class StatisticGridView extends \hipanel\grid\BoxedGridView
20
{
21
    public function columns()
22
    {
23
        return array_merge(parent::columns(), [
24
            'client_id' => [
25
                'class' => ClientColumn::class,
26
                'label' => Yii::t('hipanel', 'Client'),
27
                'idAttribute' => 'client_id',
28
                'sortAttribute' => 'client',
29
                'attribute' => 'client_id',
30
                'value' => function ($model) {
31
                    return ClientSellerLink::widget(compact('model'));
32
                },
33
            ],
34
            'spent' => [
35
                'label' => Yii::t('hipanel:ticket', 'Spent'),
36
                'filter' => false,
37
                'contentOptions' => ['nowrap' => true],
38
                'value' => function ($model) {
39
                    return Yii::t('hipanel:ticket', '{d, plural, =0{ } one{# day} other{# days}} {h}:{m}', [
40
                        'd' => floor($model->spent / 60 / 24),
41
                        'h' => sprintf('%02d', floor($model->spent / 60) % 24),
42
                        'm' => sprintf('%02d', floor($model->spent % 60)),
43
                    ]);
44
                },
45
            ],
46
            'tickets' => [
47
                'format' => 'html',
48
                'label' => Yii::t('hipanel:ticket', 'Tickets'),
49
                'value' => function($model) {
50
                    foreach (explode(",", $model->thread_ids) as $thread) {
51
                        $threads[$thread] = Html::a($thread, Url::to(['@ticket/view', 'id' => $thread]));
0 ignored issues
show
Coding Style Comprehensibility introduced by
$threads was never initialized. Although not strictly required by PHP, it is generally a good practice to add $threads = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
52
                    }
53
                    ksort($threads);
54
                    return implode(" ", $threads);
55
                }
56
            ],
57
        ]);
58
    }
59
}
60