Passed
Push — master ( 678aad...f40294 )
by
unknown
02:35 queued 10s
created

Module.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace mdm\admin;
4
5
use Yii;
6
use yii\helpers\Inflector;
7
8
/**
9
 * GUI manager for RBAC.
10
 *
11
 * Use [[\yii\base\Module::$controllerMap]] to change property of controller.
12
 * To change listed menu, use property [[$menus]].
13
 *
14
 * ```
15
 * 'layout' => 'left-menu', // default to null mean use application layout.
16
 * 'controllerMap' => [
17
 *     'assignment' => [
18
 *         'class' => 'mdm\admin\controllers\AssignmentController',
19
 *         'userClassName' => 'app\models\User',
20
 *         'idField' => 'id'
21
 *     ]
22
 * ],
23
 * 'menus' => [
24
 *     'assignment' => [
25
 *         'label' => 'Grand Access' // change label
26
 *     ],
27
 *     'route' => null, // disable menu
28
 * ],
29
 * ```
30
 *
31
 * @property string $mainLayout Main layout using for module. Default to layout of parent module.
32
 * Its used when `layout` set to 'left-menu', 'right-menu' or 'top-menu'.
33
 * @property array $menus List available menu of module.
34
 * It generated by module items .
35
 *
36
 * @author Misbahul D Munir <[email protected]>
37
 * @since 1.0
38
 */
39
class Module extends \yii\base\Module
40
{
41
    /**
42
     * @inheritdoc
43
     */
44
    public $defaultRoute = 'assignment';
45
    /**
46
     * @var array Nav bar items.
47
     */
48
    public $navbar;
49
    /**
50
     * @var string Main layout using for module. Default to layout of parent module.
51
     * Its used when `layout` set to 'left-menu', 'right-menu' or 'top-menu'.
52
     */
53
    public $mainLayout = '@mdm/admin/views/layouts/main.php';
54
    /**
55
     * @var array
56
     * @see [[menus]]
57
     */
58
    private $_menus = [];
59
    /**
60
     * @var array
61
     * @see [[menus]]
62
     */
63
    private $_coreItems = [
64
        'user' => 'Users',
65
        'assignment' => 'Assignments',
66
        'role' => 'Roles',
67
        'permission' => 'Permissions',
68
        'route' => 'Routes',
69
        'rule' => 'Rules',
70
        'menu' => 'Menus',
71
    ];
72
    /**
73
     * @var array
74
     * @see [[items]]
75
     */
76
    private $_normalizeMenus;
77
78
    /**
79
     * @var string Default url for breadcrumb
80
     */
81
    public $defaultUrl;
82
83
    /**
84
     * @var string Default url label for breadcrumb
85
     */
86
    public $defaultUrlLabel;
87
88
    /**
89
     * @inheritdoc
90
     */
91
    public function init()
92
    {
93
        parent::init();
94
        if (!isset(Yii::$app->i18n->translations['rbac-admin'])) {
95
            Yii::$app->i18n->translations['rbac-admin'] = [
96
                'class' => 'yii\i18n\PhpMessageSource',
97
                'sourceLanguage' => 'en',
98
                'basePath' => '@mdm/admin/messages',
99
            ];
100
        }
101
102
        //user did not define the Navbar?
103
        if ($this->navbar === null && Yii::$app instanceof \yii\web\Application) {
104
            $this->navbar = [
105
                ['label' => Yii::t('rbac-admin', 'Help'), 'url' => ['default/index']],
106
                ['label' => Yii::t('rbac-admin', 'Application'), 'url' => Yii::$app->homeUrl],
107
            ];
108
        }
109
        if (class_exists('yii\jui\JuiAsset')) {
110
            Yii::$container->set('mdm\admin\AutocompleteAsset', 'yii\jui\JuiAsset');
111
        }
112
    }
113
114
    /**
115
     * Get available menu.
116
     * @return array
117
     */
118
    public function getMenus()
119
    {
120
        if ($this->_normalizeMenus === null) {
121
            $mid = '/' . $this->getUniqueId() . '/';
122
            // resolve core menus
123
            $this->_normalizeMenus = [];
124
125
            $config = components\Configs::instance();
126
            $conditions = [
127
                'user' => $config->db && $config->db->schema->getTableSchema($config->userTable),
128
                'assignment' => ($userClass = Yii::$app->getUser()->identityClass) && is_subclass_of($userClass, 'yii\db\BaseActiveRecord'),
0 ignored issues
show
The method getUser does only exist in yii\web\Application, but not in yii\console\Application.

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...
129
                'menu' => $config->db && $config->db->schema->getTableSchema($config->menuTable),
130
            ];
131
            foreach ($this->_coreItems as $id => $lable) {
132
                if (!isset($conditions[$id]) || $conditions[$id]) {
133
                    $this->_normalizeMenus[$id] = ['label' => Yii::t('rbac-admin', $lable), 'url' => [$mid . $id]];
134
                }
135
            }
136
            foreach (array_keys($this->controllerMap) as $id) {
137
                $this->_normalizeMenus[$id] = ['label' => Yii::t('rbac-admin', Inflector::humanize($id)), 'url' => [$mid . $id]];
138
            }
139
140
            // user configure menus
141
            foreach ($this->_menus as $id => $value) {
142
                if (empty($value)) {
143
                    unset($this->_normalizeMenus[$id]);
144
                    continue;
145
                }
146
                if (is_string($value)) {
147
                    $value = ['label' => $value];
148
                }
149
                $this->_normalizeMenus[$id] = isset($this->_normalizeMenus[$id]) ? array_merge($this->_normalizeMenus[$id], $value)
150
                : $value;
151
                if (!isset($this->_normalizeMenus[$id]['url'])) {
152
                    $this->_normalizeMenus[$id]['url'] = [$mid . $id];
153
                }
154
            }
155
        }
156
        return $this->_normalizeMenus;
157
    }
158
159
    /**
160
     * Set or add available menu.
161
     * @param array $menus
162
     */
163
    public function setMenus($menus)
164
    {
165
        $this->_menus = array_merge($this->_menus, $menus);
166
        $this->_normalizeMenus = null;
0 ignored issues
show
Documentation Bug introduced by
It seems like null of type null is incompatible with the declared type array of property $_normalizeMenus.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
167
    }
168
169
    /**
170
     * @inheritdoc
171
     */
172
    public function beforeAction($action)
173
    {
174
        if (parent::beforeAction($action)) {
175
            /* @var $action \yii\base\Action */
176
            $view = $action->controller->getView();
177
178
            $view->params['breadcrumbs'][] = [
179
                'label' => ($this->defaultUrlLabel ?: Yii::t('rbac-admin', 'Admin')),
180
                'url' => ['/' . ($this->defaultUrl ?: $this->uniqueId)],
181
            ];
182
            return true;
183
        }
184
        return false;
185
    }
186
}
187