Completed
Push — master ( 3c8880...e2c218 )
by Dmitry
06:42 queued 02:46
created

IndexAction::getView()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 6
1
<?php
2
3
/*
4
 * HiPanel core package
5
 *
6
 * @link      https://hipanel.com/
7
 * @package   hipanel-core
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hipanel\actions;
13
14
use hipanel\base\FilterStorage;
15
use Yii;
16
use yii\helpers\ArrayHelper;
17
use yii\helpers\Inflector;
18
19
/**
20
 * Class IndexAction.
21
 */
22
class IndexAction extends SearchAction
23
{
24
    /**
25
     * @var string view to render.
26
     */
27
    protected $_view;
28
29
    public function setView($value)
30
    {
31
        $this->_view = $value;
32
    }
33
34
    public function getView()
35
    {
36
        if ($this->_view === null) {
37
            $this->_view = lcfirst(Inflector::id2camel($this->id));
38
        }
39
40
        return $this->_view;
41
    }
42
43
    /**
44
     * @var array The map of filters for the [[hipanel\base\FilterStorage|FilterStorage]]
45
     */
46
    public $filterStorageMap = [];
47
48
    public function init()
49
    {
50
        parent::init();
51
52
        $this->dataProviderOptions = ArrayHelper::merge($this->dataProviderOptions, [
53
            'pagination' => [
54
                'pageSize' => Yii::$app->request->get('per_page') ?: 25,
55
            ],
56
        ]);
57
    }
58
59
    protected function getDefaultRules()
60
    {
61
        return array_merge([
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array_merge(array...nt::getDefaultRules()); (array<string,array>) is incompatible with the return type of the parent method hipanel\actions\SearchAction::getDefaultRules of type array<string,array<strin...rray<string,callable>>>.

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...
62
            'html | pjax' => [
63
                'save' => false,
64
                'flash' => false,
65
                'success' => [
66
                    'class'  => RenderAction::class,
67
                    'view'   => $this->getView(),
68
                    'data'   => $this->data,
69
                    'params' => function () {
70
                        return [
71
                            'model'        => $this->getSearchModel(),
72
                            'dataProvider' => $this->getDataProvider(),
73
                        ];
74
                    },
75
                ],
76
            ],
77
        ], parent::getDefaultRules());
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function getDataProvider()
84
    {
85
        if ($this->dataProvider === null) {
86
            $request = Yii::$app->request;
87
88
            $formName = $this->getSearchModel()->formName();
89
            $requestFilters = Yii::$app->request->get($formName) ?: Yii::$app->request->get() ?: Yii::$app->request->post();
90
91
            // Don't save filters for ajax requests, because
92
            // the request is probably triggered with select2 or smt similar
93
            if ($request->getIsPjax() || !$request->getIsAjax()) {
94
                $filterStorage = new FilterStorage(['map' => $this->filterStorageMap]);
95
96
                if ($request->getIsPost() && $request->post('clear-filters')) {
97
                    $filterStorage->clearFilters();
98
                }
99
100
                $filters = $filterStorage->get();
101
                $filterStorage->set($requestFilters);
102
                $search = ArrayHelper::merge($this->findOptions, $filters, $requestFilters);
103
            } else {
104
                $search = ArrayHelper::merge($this->findOptions, $requestFilters);
105
            }
106
107
            $this->returnOptions[$this->controller->modelClassName()] = ArrayHelper::merge(
108
                ArrayHelper::remove($search, 'return', []),
109
                ArrayHelper::remove($search, 'rename', [])
110
            );
111
112
            if ($formName !== '') {
113
                $search = [$formName => $search];
114
            }
115
            $this->dataProvider = $this->getSearchModel()->search($search, $this->dataProviderOptions);
116
        }
117
118
        return $this->dataProvider;
119
    }
120
}
121