| Conditions | 25 |
| Paths | 51 |
| Total Lines | 75 |
| Code Lines | 48 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 65 | public static function getAssignedMenu($userId, $root = null, $callback = null, $refresh = false) |
||
| 66 | { |
||
| 67 | $config = Configs::instance(); |
||
| 68 | |||
| 69 | /* @var $manager \yii\rbac\BaseManager */ |
||
| 70 | $manager = Configs::authManager(); |
||
| 71 | $menus = Menu::find()->asArray()->indexBy('id')->all(); |
||
| 72 | $key = [__METHOD__, $userId, $manager->defaultRoles]; |
||
| 73 | $cache = $config->cache; |
||
| 74 | |||
| 75 | if ($refresh || $cache === null || ($assigned = $cache->get($key)) === false) { |
||
| 76 | $routes = $filter1 = $filter2 = []; |
||
| 77 | if ($userId !== null) { |
||
| 78 | foreach ($manager->getPermissionsByUser($userId) as $name => $value) { |
||
| 79 | if ($name[0] === '/') { |
||
| 80 | if (substr($name, -2) === '/*') { |
||
| 81 | $name = substr($name, 0, -1); |
||
| 82 | } |
||
| 83 | $routes[] = $name; |
||
| 84 | } |
||
| 85 | } |
||
| 86 | } |
||
| 87 | foreach ($manager->defaultRoles as $role) { |
||
| 88 | foreach ($manager->getPermissionsByRole($role) as $name => $value) { |
||
| 89 | if ($name[0] === '/') { |
||
| 90 | if (substr($name, -2) === '/*') { |
||
| 91 | $name = substr($name, 0, -1); |
||
| 92 | } |
||
| 93 | $routes[] = $name; |
||
| 94 | } |
||
| 95 | } |
||
| 96 | } |
||
| 97 | $routes = array_unique($routes); |
||
| 98 | sort($routes); |
||
| 99 | $prefix = '\\'; |
||
| 100 | foreach ($routes as $route) { |
||
| 101 | if (strpos($route, $prefix) !== 0) { |
||
| 102 | if (substr($route, -1) === '/') { |
||
| 103 | $prefix = $route; |
||
| 104 | $filter1[] = $route . '%'; |
||
| 105 | } else { |
||
| 106 | $filter2[] = $route; |
||
| 107 | } |
||
| 108 | } |
||
| 109 | } |
||
| 110 | $assigned = []; |
||
| 111 | $query = Menu::find()->select(['id'])->asArray(); |
||
| 112 | if (count($filter2)) { |
||
| 113 | $assigned = $query->where(['route' => $filter2])->column(); |
||
| 114 | } |
||
| 115 | if (count($filter1)) { |
||
| 116 | $query->where('route like :filter'); |
||
| 117 | foreach ($filter1 as $filter) { |
||
| 118 | $assigned = array_merge($assigned, $query->params([':filter' => $filter])->column()); |
||
| 119 | } |
||
| 120 | } |
||
| 121 | $assigned = static::requiredParent($assigned, $menus); |
||
| 122 | if ($cache !== null) { |
||
| 123 | $cache->set($key, $assigned, $config->cacheDuration, new TagDependency([ |
||
| 124 | 'tags' => Configs::CACHE_TAG |
||
| 125 | ])); |
||
| 126 | } |
||
| 127 | } |
||
| 128 | |||
| 129 | $key = [__METHOD__, $assigned, $root]; |
||
| 130 | if ($refresh || $callback !== null || $cache === null || (($result = $cache->get($key)) === false)) { |
||
| 131 | $result = static::normalizeMenu($assigned, $menus, $callback, $root); |
||
| 132 | if ($cache !== null && $callback === null) { |
||
| 133 | $cache->set($key, $result, $config->cacheDuration, new TagDependency([ |
||
| 134 | 'tags' => Configs::CACHE_TAG |
||
| 135 | ])); |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | return $result; |
||
| 140 | } |
||
| 223 |