| Conditions | 16 |
| Paths | 108 |
| Total Lines | 43 |
| Code Lines | 26 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 1 |
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 |
||
| 44 | function get_permission ($group, $label, $user = false) { |
||
| 45 | $user = (int)$user ?: $this->id; |
||
| 46 | if ($user == User::ROOT_ID) { |
||
| 47 | return true; |
||
| 48 | } |
||
| 49 | if (!$user) { |
||
| 50 | return false; |
||
| 51 | } |
||
| 52 | if (!isset($this->permissions[$user])) { |
||
| 53 | $this->permissions[$user] = $this->cache->get( |
||
| 54 | "permissions/$user", |
||
| 55 | function () use ($user) { |
||
| 56 | $permissions = []; |
||
| 57 | if ($user != User::GUEST_ID) { |
||
| 58 | $Group = System_Group::instance(); |
||
| 59 | foreach ($this->get_groups($user) ?: [] as $group_id) { |
||
| 60 | $permissions = $Group->get_permissions($group_id) ?: [] + $permissions; |
||
| 61 | } |
||
| 62 | } |
||
| 63 | $permissions = $this->get_permissions($user) ?: [] + $permissions; |
||
| 64 | return $permissions; |
||
| 65 | } |
||
| 66 | ); |
||
| 67 | } |
||
| 68 | $all_permission = Cache::instance()->{'permissions/all'} ?: System_Permission::instance()->get_all(); |
||
| 69 | if (isset($all_permission[$group][$label])) { |
||
| 70 | $permission = $all_permission[$group][$label]; |
||
| 71 | if (isset($this->permissions[$user][$permission])) { |
||
| 72 | return (bool)$this->permissions[$user][$permission]; |
||
| 73 | } |
||
| 74 | } |
||
| 75 | $group_label_exploded = explode('/', "$group/$label"); |
||
| 76 | /** |
||
| 77 | * Default permissions values: |
||
| 78 | * |
||
| 79 | * - only administrators have access to `admin/*` URLs by default |
||
| 80 | * - only administrators have access to `api/{module}/admin/*` URLs by default |
||
| 81 | * - all other URLs are available to everyone by default |
||
| 82 | */ |
||
| 83 | return $this->admin() |
||
| 84 | ? true |
||
| 85 | : $group_label_exploded[0] !== 'admin' && ($group_label_exploded[0] !== 'api' || @$group_label_exploded[2] !== 'admin'); |
||
| 86 | } |
||
| 87 | /** |
||
| 165 |