| Conditions | 18 |
| Paths | 241 |
| Total Lines | 74 |
| Code Lines | 37 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | 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 |
||
| 78 | protected function getUserPermissionsFields($user, $fields) |
||
| 79 | { |
||
| 80 | if (!config('twill.enabled.permissions-management')) { |
||
| 81 | return $fields; |
||
| 82 | } |
||
| 83 | |||
| 84 | $itemScopes = Permission::available(Permission::SCOPE_ITEM); |
||
| 85 | |||
| 86 | // looking for group permissions that belongs to the user |
||
| 87 | foreach ($user->publishedGroups as $group) { |
||
| 88 | |||
| 89 | // get each permissions that belongs to a module from this group |
||
| 90 | foreach ($group->permissions()->moduleItem()->get() as $permission) { |
||
| 91 | $model = $permission->permissionable()->first(); |
||
| 92 | |||
| 93 | if (!$model) { |
||
| 94 | continue; |
||
| 95 | } |
||
| 96 | |||
| 97 | $moduleName = getModuleNameByModel($model); |
||
| 98 | $index = $moduleName . '_' . $model->id . '_permission'; |
||
| 99 | |||
| 100 | if (isset($fields[$index])) { |
||
| 101 | $current = array_search($fields[$index], $itemScopes); |
||
| 102 | $group = array_search($permission->name, $itemScopes); |
||
| 103 | |||
| 104 | // check that group permission is greater that current permission level |
||
| 105 | if ($group > $current) { |
||
| 106 | $fields[$index] = $permission->name; |
||
| 107 | } |
||
| 108 | } else { |
||
| 109 | $fields[$index] = $permission->name; |
||
| 110 | } |
||
| 111 | } |
||
| 112 | } |
||
| 113 | |||
| 114 | // looking for global permissions, if the user has the 'manage-modules' permission |
||
| 115 | $isManageAllModules = $user->isSuperAdmin() || ($user->role->permissions()->global()->where('name', 'manage-modules')->first() != null); |
||
| 116 | |||
| 117 | // looking for role module permission |
||
| 118 | $globalPermissions = []; |
||
| 119 | if (!$isManageAllModules) { |
||
| 120 | foreach ($user->role->permissions()->module()->get() as $permission) { |
||
| 121 | if ($permission->permissionable_type) { |
||
| 122 | $permissionName = str_replace("-module", "-item", $permission->name); |
||
| 123 | $globalPermissions[getModuleNameByModel($permission->permissionable_type)] = $permissionName; |
||
| 124 | } |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | // merge all permissions |
||
| 129 | // go through all existing modules |
||
| 130 | foreach (Permission::permissionableParentModuleItems() as $moduleName => $moduleItems) { |
||
| 131 | if (isset($globalPermissions[$moduleName]) || $isManageAllModules) { |
||
| 132 | $permission = $isManageAllModules ? 'manage-item' : $globalPermissions[$moduleName]; |
||
| 133 | |||
| 134 | foreach ($moduleItems as $moduleItem) { |
||
| 135 | $index = $moduleName . '_' . $moduleItem->id . '_permission'; |
||
| 136 | if (!isset($fields[$index])) { |
||
| 137 | $fields[$index] = "\"{$permission}\""; |
||
| 138 | } else { |
||
| 139 | $current = array_search($fields[$index], $itemScopes); |
||
| 140 | $global = array_search($permission, $itemScopes); |
||
| 141 | |||
| 142 | // check permission level |
||
| 143 | if ($global > $current) { |
||
| 144 | $fields[$index] = "\"{$permission}\""; |
||
| 145 | } |
||
| 146 | } |
||
| 147 | } |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | return $fields; |
||
| 152 | } |
||
| 178 |
This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.