Conditions | 14 |
Paths | 9 |
Total Lines | 40 |
Code Lines | 25 |
Lines | 0 |
Ratio | 0 % |
Changes | 8 | ||
Bugs | 1 | Features | 2 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
119 | public function getMenus() |
||
120 | { |
||
121 | if ($this->_normalizeMenus === null) { |
||
122 | $mid = '/' . $this->getUniqueId() . '/'; |
||
123 | // resolve core menus |
||
124 | $this->_normalizeMenus = []; |
||
125 | |||
126 | $config = components\Configs::instance(); |
||
127 | $conditions = [ |
||
128 | 'user' => $config->db && $config->db->schema->getTableSchema($config->userTable), |
||
129 | 'assignment' => ($userClass = Yii::$app->getUser()->identityClass) && is_subclass_of($userClass, 'yii\db\BaseActiveRecord'), |
||
|
|||
130 | 'menu' => $config->db && $config->db->schema->getTableSchema($config->menuTable), |
||
131 | ]; |
||
132 | foreach ($this->_coreItems as $id => $lable) { |
||
133 | if (!isset($conditions[$id]) || $conditions[$id]) { |
||
134 | $this->_normalizeMenus[$id] = ['label' => Yii::t('rbac-admin', $lable), 'url' => [$mid . $id]]; |
||
135 | } |
||
136 | } |
||
137 | foreach (array_keys($this->controllerMap) as $id) { |
||
138 | $this->_normalizeMenus[$id] = ['label' => Yii::t('rbac-admin', Inflector::humanize($id)), 'url' => [$mid . $id]]; |
||
139 | } |
||
140 | |||
141 | // user configure menus |
||
142 | foreach ($this->_menus as $id => $value) { |
||
143 | if (empty($value)) { |
||
144 | unset($this->_normalizeMenus[$id]); |
||
145 | continue; |
||
146 | } |
||
147 | if (is_string($value)) { |
||
148 | $value = ['label' => $value]; |
||
149 | } |
||
150 | $this->_normalizeMenus[$id] = isset($this->_normalizeMenus[$id]) ? array_merge($this->_normalizeMenus[$id], $value) |
||
151 | : $value; |
||
152 | if (!isset($this->_normalizeMenus[$id]['url'])) { |
||
153 | $this->_normalizeMenus[$id]['url'] = [$mid . $id]; |
||
154 | } |
||
155 | } |
||
156 | } |
||
157 | return $this->_normalizeMenus; |
||
158 | } |
||
159 | |||
188 |
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:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: