Complex classes like Role often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Role, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class Role extends Base |
||
| 18 | { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Role model instance |
||
| 22 | * @var \gplcart\core\models\UserRole $role |
||
| 23 | */ |
||
| 24 | protected $role; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @param UserRoleModel $rule |
||
| 28 | */ |
||
| 29 | public function __construct(UserRoleModel $rule) |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Callback for "role-perm-add" command |
||
| 38 | */ |
||
| 39 | public function cmdPermAddRole() |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Callback for "role-perm-delete" command |
||
| 56 | */ |
||
| 57 | public function cmdPermDeleteRole() |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Callback for "role-get" command |
||
| 76 | */ |
||
| 77 | public function cmdGetRole() |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Callback for "role-delete" command |
||
| 87 | */ |
||
| 88 | public function cmdDeleteRole() |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Callback for "role-add" command |
||
| 127 | */ |
||
| 128 | public function cmdAddRole() |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Callback for "role-update" command |
||
| 141 | */ |
||
| 142 | public function cmdUpdateRole() |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Returns an array of user roles |
||
| 166 | * @return array |
||
| 167 | */ |
||
| 168 | protected function getListRole() |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Output table format |
||
| 191 | * @param array $items |
||
| 192 | */ |
||
| 193 | protected function outputFormatTableRole(array $items) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Returns an array containing parsed submitted data, such as: |
||
| 220 | * role ID, the role permissions, submitted permissions |
||
| 221 | * @return array |
||
| 222 | */ |
||
| 223 | protected function getPermissionsRole() |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Updates a user role |
||
| 248 | * @param string $role_id |
||
| 249 | */ |
||
| 250 | protected function updateRole($role_id) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Add a new user role at once |
||
| 259 | */ |
||
| 260 | protected function submitAddRole() |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Add a new user role |
||
| 270 | */ |
||
| 271 | protected function addRole() |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Add a new user role step by step |
||
| 284 | */ |
||
| 285 | protected function wizardAddRole() |
||
| 296 | |||
| 297 | } |
||
| 298 |