Completed
Push — master ( d123db...236d99 )
by Dmitry
09:19
created

TemplateGridView::defaultColumns()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 30
ccs 0
cts 29
cp 0
rs 8.8571
cc 1
eloc 20
nc 1
nop 0
crap 2
1
<?php
2
3
/*
4
 * Hosting Plugin for HiPanel
5
 *
6
 * @link      https://github.com/hiqdev/hipanel-module-hosting
7
 * @package   hipanel-module-hosting
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
/**
13
 * @link    http://hiqdev.com/hipanel-module-hosting
14
 * @license http://hiqdev.com/hipanel-module-hosting/license
15
 * @copyright Copyright (c) 2015 HiQDev
16
 */
17
18
namespace hipanel\modules\ticket\grid;
19
20
use hipanel\grid\ActionColumn;
21
use hipanel\grid\SwitchColumn;
22
use hipanel\modules\client\grid\ClientColumn;
23
use Yii;
24
use yii\helpers\Html;
25
26
class TemplateGridView extends \hipanel\grid\BoxedGridView
27
{
28
    public static function defaultColumns()
29
    {
30
        return [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('author_id'...bute' => 'name_like')); (array<string,array>) is incompatible with the return type of the parent method hipanel\grid\GridView::defaultColumns of type array<string,array>.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
31
            'author_id' => [
32
                'class' => ClientColumn::class,
33
                'idAttribute' => 'author_id',
34
                'attribute' => 'author_id',
35
                'nameAttribute' => 'author',
36
            ],
37
            'post_date' => [
38
                'value' => function ($model) {
39
                    return Yii::$app->formatter->asDatetime($model->post_date);
40
                }
41
            ],
42
            'actions' => [
43
                'class' => ActionColumn::className(),
44
                'template' => '{view}{delete}',
45
                'header' => Yii::t('hipanel', 'Actions'),
46
            ],
47
            'is_published' => [
48
                'class' => SwitchColumn::class,
49
                'switchInputOptions' => [
50
                    'disabled' => true
51
                ]
52
            ],
53
            'name' => [
54
                'filterAttribute' => 'name_like',
55
            ]
56
        ];
57
    }
58
}
59