Completed
Push — master ( ad173d...04760c )
by Klochok
14:14
created

Controller   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 195
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 31
lcom 3
cbo 6
dl 0
loc 195
ccs 2
cts 2
cp 1
rs 9.8
c 0
b 0
f 0

20 Methods

Rating   Name   Duplication   Size   Complexity  
A setCache() 0 4 1
A getCache() 0 8 2
A modelClassName() 0 10 1
A newModel() 0 5 1
A searchModel() 0 4 1
A formName() 0 4 1
A searchFormName() 0 4 1
A modelId() 0 4 1
A moduleId() 0 4 1
A controllerId() 0 4 1
A findModel() 0 10 3
C findModels() 0 22 7
A renderJson() 0 6 1
A renderJsonp() 0 6 1
A actionIndex() 0 4 1
A setInternalAction() 0 4 1
A hasInternalAction() 0 4 1
A createAction() 0 5 2
A getActionUrl() 0 5 2
A getSearchUrl() 0 4 1
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\base;
13
14
use hipanel\components\Cache;
15
use hipanel\components\Response;
16
use hiqdev\hiart\ActiveRecord;
17
use Yii;
18
use yii\di\Instance;
19
use yii\filters\AccessControl;
20
use yii\helpers\Inflector;
21
use yii\web\NotFoundHttpException;
22
23
/**
24
 * Site controller.
25
 */
26
class Controller extends \yii\web\Controller
27
{
28
    /**
29
     * @var Cache|array|string the cache object or the application component ID of the cache object.
30
     */
31
    protected $_cache = 'cache';
32
33
    public function setCache($cache)
34
    {
35
        $this->_cache = $cache;
36
    }
37
38
    public function getCache()
39
    {
40
        if (!is_object($this->_cache)) {
41
            $this->_cache = Instance::ensure($this->_cache, Cache::class);
42
        }
43
44
        return $this->_cache;
45
    }
46
47
    /**
48
     * @var array internal actions.
49
     */
50
    protected $_internalActions;
51
52
    /**
53
     * @param string $submodel the submodel that will be added to the ClassName
0 ignored issues
show
Bug introduced by
There is no parameter named $submodel. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
54
     * @return string Main Model class name
55 1
     */
56
    public static function modelClassName()
57
    {
58
        $parts = explode('\\', static::class);
59
        $last  = array_pop($parts);
60
        array_pop($parts);
61
        $parts[] = 'models';
62
        $parts[] = substr($last, 0, -10);
63
64
        return implode('\\', $parts);
65
    }
66
67
    /**
68
     * @param array $config config to be used to create the [[Model]]
69 1
     * @return ActiveRecord
70
     */
71
    public static function newModel($config = [], $submodel = '')
72
    {
73
        $config['class'] = static::modelClassName() . $submodel;
74
        return Yii::createObject($config);
75
    }
76
77
    /**
78
     * @param array $config config to be used to create the [[Model]]
79
     * @return ActiveRecord|SearchModelTrait Search Model object
80
     */
81
    public static function searchModel($config = [])
82
    {
83
        return static::newModel($config, 'Search');
84
    }
85
86
    /**
87
     * @return string main model's formName()
88
     */
89
    public static function formName()
90
    {
91
        return static::newModel()->formName();
92
    }
93
94
    /**
95
     * @return string search model's formName()
96
     */
97
    public static function searchFormName()
98
    {
99
        return static::newModel()->formName() . 'Search';
100
    }
101
102
    /**
103
     * @param string $separator
104
     * @return string Main model's camel2id'ed formName()
105
     */
106
    public static function modelId($separator = '-')
107
    {
108
        return Inflector::camel2id(static::formName(), $separator);
109
    }
110
111
    /**
112
     * Returns the module ID based on the namespace of the controller.
113
     * @return mixed
114
     */
115
    public static function moduleId()
116
    {
117
        return explode('\\', get_called_class())[2];
118
    }
119
120
    public static function controllerId()
121
    {
122
        return strtolower(substr(explode('\\', get_called_class())[4], 0, -10));
123
    }
124
125
    /**
126
     * @param int|array $condition scalar ID or array to be used for searching
127
     * @param array $config config to be used to create the [[Model]]
128
     * @throws NotFoundHttpException
129
     * @return array|ActiveRecord|null|static
130
     */
131
    public static function findModel($condition, $config = [])
132
    {
133
        /* @noinspection PhpVoidFunctionResultUsedInspection */
134
        $model = static::newModel($config)->findOne(is_array($condition) ? $condition : ['id' => $condition]);
135
        if ($model === null) {
136
            throw new NotFoundHttpException('The requested object not found.');
137
        }
138
139
        return $model;
140
    }
141
142
    public static function findModels($condition, $config = [])
143
    {
144
        $containsIntKeys = 0;
145
        if (is_array($condition)) {
146
            foreach (array_keys($condition) as $item) {
147
                if (is_numeric($item)) {
148
                    $containsIntKeys = true;
149
                    break;
150
                }
151
            }
152
        }
153
154
        if (!is_array($condition) || $containsIntKeys) {
155
            $condition = ['id' => $condition];
156
        }
157
        $models = static::searchModel($config)->search([static::searchFormName() => $condition], ['pagination' => false])->getModels();
0 ignored issues
show
Bug introduced by
The method search does only exist in hipanel\base\SearchModelTrait, but not in hiqdev\hiart\ActiveRecord.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
158
        if ($models === null) {
159
            throw new NotFoundHttpException('The requested object not found.');
160
        }
161
162
        return $models;
163
    }
164
165
    public static function renderJson($data)
166
    {
167
        Yii::$app->response->format = Response::FORMAT_JSON;
168
169
        return $data;
170
    }
171
172
    public static function renderJsonp($data)
173
    {
174
        Yii::$app->response->format = Response::FORMAT_JSONP;
175
176
        return $data;
177
    }
178
179
    public function actionIndex()
180
    {
181
        return $this->render('index');
182
    }
183
184
    public function setInternalAction($id, $action)
185
    {
186
        $this->_internalActions[$id] = $action;
187
    }
188
189
    public function hasInternalAction($id)
190
    {
191
        return array_key_exists($id, $this->_internalActions);
192
    }
193
194
    public function createAction($id)
195
    {
196
        $config = $this->_internalActions[$id];
197
        return $config ? Yii::createObject($config, [$id, $this]) : parent::createAction($id);
198
    }
199
200
    /**
201
     * Prepares array for building url to action based on given action id and parameters.
202
     *
203
     * @param string $action action id
204
     * @param string|int|array $params ID of object to be action'ed or array of parameters
205
     * @return array array suitable for Url::to
206
     */
207
    public static function getActionUrl($action = 'index', $params = [])
208
    {
209
        $params = is_array($params) ? $params : ['id' => $params];
210
        return array_merge([implode('/', ['', static::moduleId(), static::controllerId(), $action])], $params);
211
    }
212
213
    /**
214
     * Prepares array for building url to search with given filters.
215
     */
216
    public static function getSearchUrl(array $params = [])
217
    {
218
        return static::getActionUrl('index', [static::searchFormName() => $params]);
219
    }
220
}
221