| Conditions | 9 |
| Paths | 56 |
| Total Lines | 148 |
| Code Lines | 100 |
| 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 |
||
| 11 | public function safeUp() |
||
| 12 | { |
||
| 13 | $auth = Yii::$app->getAuthManager(); |
||
| 14 | |||
| 15 | $userDeleteRule = new UserDeleteRule(); |
||
| 16 | $roleDeleteRule = new RoleDeleteRule(); |
||
| 17 | $rules = [ |
||
| 18 | $userDeleteRule, $roleDeleteRule, |
||
| 19 | ]; |
||
| 20 | foreach ($rules as $rule) { |
||
| 21 | $auth->add($rule); |
||
| 22 | } |
||
| 23 | |||
| 24 | $permissions = [ |
||
| 25 | 'userIndex' => [ |
||
| 26 | 'description' => 'User List', |
||
| 27 | ], |
||
| 28 | 'userCreate' => [ |
||
| 29 | 'description' => 'Create User', |
||
| 30 | ], |
||
| 31 | 'userView' => [ |
||
| 32 | 'description' => 'View User', |
||
| 33 | ], |
||
| 34 | 'userUpdate' => [ |
||
| 35 | 'description' => 'Edit User', |
||
| 36 | 'children' => ['userView'], |
||
| 37 | ], |
||
| 38 | 'userDelete' => [ |
||
| 39 | 'description' => 'Delete User', |
||
| 40 | 'rule' => $userDeleteRule->name, |
||
| 41 | ], |
||
| 42 | 'userManagement' => [ |
||
| 43 | 'description' => 'User Management', |
||
| 44 | 'children' => ['userIndex', 'userCreate', 'userUpdate', 'userView', 'userDelete'], |
||
| 45 | ], |
||
| 46 | |||
| 47 | 'roleIndex' => [ |
||
| 48 | 'description' => 'Role List', |
||
| 49 | ], |
||
| 50 | 'roleCreate' => [ |
||
| 51 | 'description' => 'Create Role', |
||
| 52 | ], |
||
| 53 | 'roleView' => [ |
||
| 54 | 'description' => 'View Role', |
||
| 55 | ], |
||
| 56 | 'roleUpdate' => [ |
||
| 57 | 'description' => 'Edit Role', |
||
| 58 | 'children' => ['roleView'], |
||
| 59 | ], |
||
| 60 | 'roleDelete' => [ |
||
| 61 | 'description' => 'Delete Role', |
||
| 62 | 'rule' => $roleDeleteRule->name, |
||
| 63 | ], |
||
| 64 | 'roleManagement' => [ |
||
| 65 | 'description' => 'Role Management', |
||
| 66 | 'children' => ['roleIndex', 'roleCreate', 'roleUpdate', 'roleView', 'roleDelete'], |
||
| 67 | ], |
||
| 68 | |||
| 69 | 'articleIndex' => [ |
||
| 70 | 'description' => 'Article List', |
||
| 71 | ], |
||
| 72 | 'articleCreate' => [ |
||
| 73 | 'description' => 'Create Article', |
||
| 74 | ], |
||
| 75 | 'articleView' => [ |
||
| 76 | 'description' => 'View Article', |
||
| 77 | ], |
||
| 78 | 'articleUpdate' => [ |
||
| 79 | 'description' => 'Edit Article', |
||
| 80 | 'children' => ['articleView'], |
||
| 81 | ], |
||
| 82 | 'articleDelete' => [ |
||
| 83 | 'description' => 'Delete Article', |
||
| 84 | 'rule' => $roleDeleteRule->name, |
||
| 85 | ], |
||
| 86 | 'articleManagement' => [ |
||
| 87 | 'description' => 'Article Management', |
||
| 88 | 'children' => ['articleIndex', 'articleCreate', 'articleUpdate', 'articleView', 'articleDelete'], |
||
| 89 | ], |
||
| 90 | |||
| 91 | 'articleCategoryIndex' => [ |
||
| 92 | 'description' => 'Article Category List', |
||
| 93 | ], |
||
| 94 | 'articleCategoryCreate' => [ |
||
| 95 | 'description' => 'Create Article Category', |
||
| 96 | ], |
||
| 97 | 'articleCategoryView' => [ |
||
| 98 | 'description' => 'View Article Category', |
||
| 99 | ], |
||
| 100 | 'articleCategoryUpdate' => [ |
||
| 101 | 'description' => 'Edit Article Category', |
||
| 102 | 'children' => ['articleView'], |
||
| 103 | ], |
||
| 104 | 'articleCategoryDelete' => [ |
||
| 105 | 'description' => 'Delete Article Category', |
||
| 106 | ], |
||
| 107 | 'articleCategoryManagement' => [ |
||
| 108 | 'description' => 'Article Category Management', |
||
| 109 | 'children' => ['articleCategoryIndex', 'articleCategoryCreate', 'articleCategoryUpdate', 'articleCategoryView', 'articleCategoryDelete'], |
||
| 110 | ], |
||
| 111 | |||
| 112 | 'settingIndex' => [ |
||
| 113 | 'description' => 'Setting List', |
||
| 114 | ], |
||
| 115 | 'settingView' => [ |
||
| 116 | 'description' => 'View Setting', |
||
| 117 | ], |
||
| 118 | 'settingUpdate' => [ |
||
| 119 | 'description' => 'Edit Setting', |
||
| 120 | 'children' => ['settingView'], |
||
| 121 | ], |
||
| 122 | 'settingManagement' => [ |
||
| 123 | 'description' => 'Setting Management', |
||
| 124 | 'children' => ['settingIndex', 'settingView', 'settingUpdate'], |
||
| 125 | ], |
||
| 126 | ]; |
||
| 127 | |||
| 128 | foreach ($permissions as $name => $params) { |
||
| 129 | $permission = $auth->createPermission($name); |
||
| 130 | $permission->description = $params['description']; |
||
| 131 | if (isset($params['rule'])) { |
||
| 132 | $permission->ruleName = $params['rule']; |
||
| 133 | } |
||
| 134 | $auth->add($permission); |
||
| 135 | if (isset($params['children'])) { |
||
| 136 | foreach ($params['children'] as $child) { |
||
| 137 | $auth->addChild($permission, $auth->getPermission($child)); |
||
| 138 | } |
||
| 139 | } |
||
| 140 | } |
||
| 141 | |||
| 142 | $roles = [ |
||
| 143 | 'Administrator' => [ |
||
| 144 | 'description' => 'Administrator', |
||
| 145 | 'permissions' => ['userManagement', 'roleManagement', 'articleManagement', 'articleCategoryManagement', 'settingManagement'], |
||
| 146 | ], |
||
| 147 | 'User' => [ |
||
| 148 | 'description' => 'User', |
||
| 149 | 'permissions' => [], |
||
| 150 | ], |
||
| 151 | ]; |
||
| 152 | foreach ($roles as $name => $params) { |
||
| 153 | $role = $auth->createRole($name); |
||
| 154 | $role->description = $params['description']; |
||
| 155 | $auth->add($role); |
||
| 156 | if (isset($params['permissions'])) { |
||
| 157 | foreach ($params['permissions'] as $permissionName) { |
||
| 158 | $auth->addChild($role, $auth->getPermission($permissionName)); |
||
| 159 | } |
||
| 170 |