| Conditions | 14 |
| Paths | 24 |
| Total Lines | 49 |
| Code Lines | 30 |
| 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 |
||
| 59 | public function afterSaveHandleGroupPermissions($object, $fields) |
||
| 60 | { |
||
| 61 | // Assign global permissions |
||
| 62 | foreach (Permission::available(Permission::SCOPE_GLOBAL) as $permissionName) { |
||
| 63 | if (isset($fields[$permissionName]) && $fields[$permissionName] === true) { |
||
| 64 | $object->grantGlobalPermission($permissionName); |
||
| 65 | } else { |
||
| 66 | $object->revokeGlobalPermission($permissionName); |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | $subdomainsAccess = []; |
||
| 71 | |||
| 72 | // Assign item permissions + subdomain permission |
||
| 73 | foreach ($fields as $key => $permissionName) { |
||
| 74 | if (Str::startsWith($key, 'module_') && Str::endsWith($key, '_permissions')) { |
||
| 75 | $modulePermissions = Permission::available(Permission::SCOPE_MODULE); |
||
| 76 | $model = getModelByModuleName($moduleName = explode('_', $key)[1]); |
||
| 77 | |||
| 78 | $currentPermission = $object->permissions() |
||
| 79 | ->where('permissionable_type', $model) |
||
| 80 | ->whereIn('name', $modulePermissions) |
||
| 81 | ->first() |
||
| 82 | ; |
||
| 83 | |||
| 84 | if (!$currentPermission || $permissionName != $currentPermission->name) { |
||
| 85 | $object->revokeAllModulePermission($model); |
||
| 86 | if (in_array($permissionName, $modulePermissions)) { |
||
| 87 | $object->grantModulePermission($permissionName, $model); |
||
| 88 | } |
||
| 89 | } |
||
| 90 | } elseif (Str::endsWith($key, '_permission')) { |
||
| 91 | $item_name = explode('_', $key)[0]; |
||
| 92 | $item_id = explode('_', $key)[1]; |
||
| 93 | $item = getRepositoryByModuleName($item_name)->getById($item_id); |
||
| 94 | |||
| 95 | // Only permissionName existed, do update or create |
||
| 96 | if ($permissionName) { |
||
| 97 | $object->grantModuleItemPermission($permissionName, $item); |
||
| 98 | } else { |
||
| 99 | $object->revokeModuleItemAllPermissions($item); |
||
| 100 | } |
||
| 101 | } elseif (Str::startsWith($key, 'subdomain_access_') && $permissionName) { |
||
| 102 | array_push($subdomainsAccess, substr($key, strlen('subdomain_access_'))); |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | $object->subdomains_access = $subdomainsAccess; |
||
| 107 | $object->save(); |
||
| 108 | } |
||
| 110 |
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.