Passed
Pull Request — master (#400)
by
unknown
02:08
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 mdm\admin\components\Configs;
6
use Yii;
7
use yii\helpers\Inflector;
8
9
/**
10
 * GUI manager for RBAC.
11
 *
12
 * Use [[\yii\base\Module::$controllerMap]] to change property of controller.
13
 * To change listed menu, use property [[$menus]].
14
 *
15
 * ```
16
 * 'layout' => 'left-menu', // default to null mean use application layout.
17
 * 'controllerMap' => [
18
 *     'assignment' => [
19
 *         'class' => 'mdm\admin\controllers\AssignmentController',
20
 *         'userClassName' => 'app\models\User',
21
 *         'idField' => 'id'
22
 *     ]
23
 * ],
24
 * 'menus' => [
25
 *     'assignment' => [
26
 *         'label' => 'Grand Access' // change label
27
 *     ],
28
 *     'route' => null, // disable menu
29
 * ],
30
 * ```
31
 *
32
 * @property string $mainLayout Main layout using for module. Default to layout of parent module.
33
 * Its used when `layout` set to 'left-menu', 'right-menu' or 'top-menu'.
34
 * @property array $menus List available menu of module.
35
 * It generated by module items .
36
 *
37
 * @author Misbahul D Munir <[email protected]>
38
 * @since 1.0
39
 */
40
class Module extends \yii\base\Module
41
{
42
    /**
43
     * @inheritdoc
44
     */
45
    public $defaultRoute = 'assignment';
46
    /**
47
     * @var array Nav bar items.
48
     */
49
    public $navbar;
50
    /**
51
     * @var string Main layout using for module. Default to layout of parent module.
52
     * Its used when `layout` set to 'left-menu', 'right-menu' or 'top-menu'.
53
     */
54
    public $mainLayout = '@mdm/admin/views/layouts/main.php';
55
    /**
56
     * @var array
57
     * @see [[menus]]
58
     */
59
    private $_menus = [];
60
    /**
61
     * @var array
62
     * @see [[menus]]
63
     */
64
    private $_coreItems = [
65
        'user/index' => 'Users',
66
        'assignment/index' => 'Assignments',
67
        'role/index' => 'Roles',
68
        'permission/index' => 'Permissions',
69
        'route/index' => 'Routes',
70
        'rule/index' => 'Rules',
71
        'menu/index' => 'Menus',
72
    ];
73
    /**
74
     * @var array
75
     * @see [[items]]
76
     */
77
    private $_normalizeMenus;
78
79
    /**
80
     * @var string Default url for breadcrumb
81
     */
82
    public $defaultUrl;
83
84
    /**
85
     * @var string Default url label for breadcrumb
86
     */
87
    public $defaultUrlLabel;
88
89
    /**
90
     * @inheritdoc
91
     */
92
    public function init()
93
    {
94
        parent::init();
95
        if (!isset(Yii::$app->i18n->translations['rbac-admin'])) {
96
            Yii::$app->i18n->translations['rbac-admin'] = [
97
                'class' => 'yii\i18n\PhpMessageSource',
98
                'sourceLanguage' => 'en',
99
                'basePath' => '@mdm/admin/messages',
100
            ];
101
        }
102
103
        //user did not define the Navbar?
104
        if ($this->navbar === null && Yii::$app instanceof \yii\web\Application) {
105
            $this->navbar = [
106
                ['label' => Yii::t('rbac-admin', 'Help'), 'url' => ['default/index']],
107
                ['label' => Yii::t('rbac-admin', 'Application'), 'url' => Yii::$app->homeUrl],
108
            ];
109
        }
110
        if (class_exists('yii\jui\JuiAsset')) {
111
            Yii::$container->set('mdm\admin\AutocompleteAsset', 'yii\jui\JuiAsset');
112
        }
113
    }
114
115
    /**
116
     * Get available menu.
117
     * @return array
118
     */
119
    public function getMenus()
120
    {
121
        if ($this->_normalizeMenus === null) {
122
            $mid = '/' . $this->getUniqueId() . '/';
123
            // resolve core menus
124
            $this->_normalizeMenus = [];
125
126
            $config = Configs::instance();
127
            $conditions = [
128
                'user/index' => $config->db && $config->db->schema->getTableSchema($config->userTable),
129
                'assignment/index' => ($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...
130
                'menu/index' => $config->db && $config->db->schema->getTableSchema($config->menuTable),
131
            ];
132
133
            foreach ($this->_coreItems as $id => $lable) {
134
                if (!isset($conditions[$id]) || $conditions[$id]) {
135
                    $this->_normalizeMenus[$id] = ['label' => Yii::t('rbac-admin', $lable), 'url' => [$mid . $id]];
136
                }
137
            }
138
139
            // user configure menus
140
            foreach ($this->_menus as $id => $value) {
141
                if (empty($value)) {
142
                    unset($this->_normalizeMenus[$id]);
143
                    continue;
144
                }
145
                if (is_string($value)) {
146
                    $value = ['label' => $value];
147
                }
148
                $this->_normalizeMenus[$id] = isset($this->_normalizeMenus[$id]) ? array_merge($this->_normalizeMenus[$id], $value)
149
                    : $value;
150
                if (!isset($this->_normalizeMenus[$id]['url'])) {
151
                    $this->_normalizeMenus[$id]['url'] = [$mid . $id];
152
                }
153
            }
154
        }
155
        return $this->_normalizeMenus;
156
    }
157
158
    /**
159
     * Set or add available menu.
160
     * @param array $menus
161
     */
162
    public function setMenus($menus)
163
    {
164
        $this->_menus = array_merge($this->_menus, $menus);
165
        $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...
166
    }
167
168
    /**
169
     * @inheritdoc
170
     */
171
    public function beforeAction($action)
172
    {
173
        if (parent::beforeAction($action)) {
174
            /* @var $action \yii\base\Action */
175
            $view = $action->controller->getView();
176
177
            $view->params['breadcrumbs'][] = [
178
                'label' => ($this->defaultUrlLabel ?: Yii::t('rbac-admin', 'Admin')),
179
                'url' => ['/' . ($this->defaultUrl ?: $this->uniqueId)],
180
            ];
181
            return true;
182
        }
183
        return false;
184
    }
185
}
186