| Conditions | 27 |
| Paths | 811 |
| Total Lines | 98 |
| Code Lines | 58 |
| Lines | 27 |
| Ratio | 27.55 % |
| 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 |
||
| 41 | public function handle() |
||
| 42 | { |
||
| 43 | $permissions = config('roleManager.permissions'); |
||
| 44 | $roles = config('roleManager.roles'); |
||
| 45 | foreach ($permissions as $permissionName => $permission) { |
||
| 46 | |||
| 47 | $permissionModel = new Permissions(); |
||
| 48 | if (empty($permissionName)) { |
||
| 49 | throw new \Exception('Permission Name Required'); |
||
| 50 | } |
||
| 51 | |||
| 52 | if (!$permissionModel->where('name', $permissionName)->first()) { |
||
|
|
|||
| 53 | |||
| 54 | |||
| 55 | $permissionModel->name = $permissionName; |
||
| 56 | |||
| 57 | if (isset($permission['description'])) { |
||
| 58 | $permissionModel->description = $permission['description']; |
||
| 59 | } |
||
| 60 | if (isset($permission['class'])) { |
||
| 61 | $permissionModel->class = $permission['class']; |
||
| 62 | } |
||
| 63 | if (isset($permission['method'])) { |
||
| 64 | $permissionModel->method = $permission['method']; |
||
| 65 | } |
||
| 66 | $permissionModel->save(); |
||
| 67 | } |
||
| 68 | |||
| 69 | } |
||
| 70 | foreach ($roles as $roleName => $role) { |
||
| 71 | $roleModel = new Roles(); |
||
| 72 | if (empty($roleName)) { |
||
| 73 | throw new \Exception('Role Name Required'); |
||
| 74 | } |
||
| 75 | if (!$roleModel->where('name', $roleName)->first()) { |
||
| 76 | $roleModel->name = $roleName; |
||
| 77 | if (isset($role['description'])) { |
||
| 78 | $roleModel->description = $role['description']; |
||
| 79 | } |
||
| 80 | $roleModel->save(); |
||
| 81 | } |
||
| 82 | } |
||
| 83 | |||
| 84 | $defaultRoles = config('roleManager.assignPermissionsToRole'); |
||
| 85 | foreach ($defaultRoles as $roleName => $permissionsList) { |
||
| 86 | |||
| 87 | $roleModel = Roles::where('name', $roleName) |
||
| 88 | ->with('permissions')->first(); |
||
| 89 | if ($roleModel) { |
||
| 90 | |||
| 91 | if ($permissionsList == '*') { |
||
| 92 | View Code Duplication | foreach ($permissions as $permissionName => $permission) { |
|
| 93 | $permissionModel = Permissions:: |
||
| 94 | where('name', $permissionName) |
||
| 95 | ->first(); |
||
| 96 | if ($permissionModel) { |
||
| 97 | $checker = $roleModel->permissions |
||
| 98 | ->where('name', $permissionName)->first(); |
||
| 99 | if (!$checker) { |
||
| 100 | $roleModel->permissions()->attach($permissionModel); |
||
| 101 | } |
||
| 102 | } |
||
| 103 | } |
||
| 104 | |||
| 105 | } elseif (is_array($permissionsList)) { |
||
| 106 | View Code Duplication | foreach ($permissions as $permissionName) { |
|
| 107 | |||
| 108 | $permissionModel = Permissions:: |
||
| 109 | where('name', $permissionName)->first(); |
||
| 110 | |||
| 111 | if ($permissionModel) { |
||
| 112 | |||
| 113 | $checker = $roleModel-> |
||
| 114 | permissions->where('name', $permissionName)->first(); |
||
| 115 | |||
| 116 | if (!$checker) { |
||
| 117 | $roleModel->permissions()->attach($permissionModel); |
||
| 118 | } |
||
| 119 | } |
||
| 120 | } |
||
| 121 | } |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | $userAssignments = config('roleManager.assignRoleToUser'); |
||
| 126 | if (!empty($userAssignments) AND is_array($userAssignments)) { |
||
| 127 | foreach ($userAssignments as $roleName => $userEmail) { |
||
| 128 | $roleModel = Roles::where('name', $roleName)->first(); |
||
| 129 | if ($user = User::where('email', $userEmail)->first() |
||
| 130 | AND $roleModel |
||
| 131 | AND !$roleModel->belongsToUser($user) |
||
| 132 | ) { |
||
| 133 | $roleModel->users()->attach($user); |
||
| 134 | } |
||
| 135 | } |
||
| 136 | } |
||
| 137 | echo "All permission Migrated Successfully"; |
||
| 138 | } |
||
| 139 | } |
||
| 140 |
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: