| Conditions | 18 |
| Paths | 241 |
| Total Lines | 74 |
| Code Lines | 37 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 96 | protected function getUserPermissionsFields($user, $fields) |
||
| 97 | { |
||
| 98 | if (!config('twill.enabled.permissions-management')) { |
||
| 99 | return $fields; |
||
| 100 | } |
||
| 101 | |||
| 102 | $itemScopes = Permission::available(Permission::SCOPE_ITEM); |
||
| 103 | |||
| 104 | // looking for group permissions that belongs to the user |
||
| 105 | foreach ($user->publishedGroups as $group) { |
||
| 106 | |||
| 107 | // get each permissions that belongs to a module from this group |
||
| 108 | foreach ($group->permissions()->moduleItem()->get() as $permission) { |
||
| 109 | $model = $permission->permissionable()->first(); |
||
| 110 | |||
| 111 | if (!$model) { |
||
| 112 | continue; |
||
| 113 | } |
||
| 114 | |||
| 115 | $moduleName = getModuleNameByModel($model); |
||
| 116 | $index = $moduleName . '_' . $model->id . '_permission'; |
||
| 117 | |||
| 118 | if (isset($fields[$index])) { |
||
| 119 | $current = array_search($fields[$index], $itemScopes); |
||
| 120 | $group = array_search($permission->name, $itemScopes); |
||
| 121 | |||
| 122 | // check that group permission is greater that current permission level |
||
| 123 | if ($group > $current) { |
||
| 124 | $fields[$index] = $permission->name; |
||
| 125 | } |
||
| 126 | } else { |
||
| 127 | $fields[$index] = $permission->name; |
||
| 128 | } |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | // looking for global permissions, if the user has the 'manage-modules' permission |
||
| 133 | $isManageAllModules = $user->isSuperAdmin() || ($user->role->permissions()->global()->where('name', 'manage-modules')->first() != null); |
||
| 134 | |||
| 135 | // looking for role module permission |
||
| 136 | $globalPermissions = []; |
||
| 137 | if (!$isManageAllModules) { |
||
| 138 | foreach ($user->role->permissions()->module()->get() as $permission) { |
||
| 139 | if ($permission->permissionable_type) { |
||
| 140 | $permissionName = str_replace("-module", "-item", $permission->name); |
||
| 141 | $globalPermissions[getModuleNameByModel($permission->permissionable_type)] = $permissionName; |
||
| 142 | } |
||
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 146 | // merge all permissions |
||
| 147 | // go through all existing modules |
||
| 148 | foreach (Permission::permissionableParentModuleItems() as $moduleName => $moduleItems) { |
||
| 149 | if (isset($globalPermissions[$moduleName]) || $isManageAllModules) { |
||
| 150 | $permission = $isManageAllModules ? 'manage-item' : $globalPermissions[$moduleName]; |
||
| 151 | |||
| 152 | foreach ($moduleItems as $moduleItem) { |
||
| 153 | $index = $moduleName . '_' . $moduleItem->id . '_permission'; |
||
| 154 | if (!isset($fields[$index])) { |
||
| 155 | $fields[$index] = "{$permission}"; |
||
| 156 | } else { |
||
| 157 | $current = array_search($fields[$index], $itemScopes); |
||
| 158 | $global = array_search($permission, $itemScopes); |
||
| 159 | |||
| 160 | // check permission level |
||
| 161 | if ($global > $current) { |
||
| 162 | $fields[$index] = "{$permission}"; |
||
| 163 | } |
||
| 164 | } |
||
| 165 | } |
||
| 166 | } |
||
| 167 | } |
||
| 168 | |||
| 169 | return $fields; |
||
| 170 | } |
||
| 200 |
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.