| Conditions | 14 |
| Paths | 9 |
| Total Lines | 40 |
| Code Lines | 25 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 1 | Features | 0 |
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 |
||
| 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'), |
||
|
|
|||
| 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 | |||
| 187 |
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: