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 |
||
18 | class Role extends Command |
||
19 | { |
||
20 | |||
21 | /** |
||
22 | * Role model instance |
||
23 | * @var \gplcart\core\models\UserRole $role |
||
24 | */ |
||
25 | protected $role; |
||
26 | |||
27 | /** |
||
28 | * @param UserRoleModel $role |
||
29 | */ |
||
30 | public function __construct(UserRoleModel $role) |
||
36 | |||
37 | /** |
||
38 | * Callback for "role-perm-add" command |
||
39 | */ |
||
40 | public function cmdPermAddRole() |
||
54 | |||
55 | /** |
||
56 | * Callback for "role-perm-delete" command |
||
57 | */ |
||
58 | public function cmdPermDeleteRole() |
||
74 | |||
75 | /** |
||
76 | * Callback for "role-get" command |
||
77 | */ |
||
78 | public function cmdGetRole() |
||
85 | |||
86 | /** |
||
87 | * Callback for "role-delete" command |
||
88 | */ |
||
89 | public function cmdDeleteRole() |
||
125 | |||
126 | /** |
||
127 | * Callback for "role-add" command |
||
128 | */ |
||
129 | public function cmdAddRole() |
||
139 | |||
140 | /** |
||
141 | * Callback for "role-update" command |
||
142 | */ |
||
143 | public function cmdUpdateRole() |
||
164 | |||
165 | /** |
||
166 | * Returns an array of user roles |
||
167 | * @return array |
||
168 | */ |
||
169 | protected function getListRole() |
||
189 | |||
190 | /** |
||
191 | * Output table format |
||
192 | * @param array $items |
||
193 | */ |
||
194 | protected function outputFormatTableRole(array $items) |
||
218 | |||
219 | /** |
||
220 | * Returns an array containing parsed submitted data, such as: |
||
221 | * role ID, the role permissions, submitted permissions |
||
222 | * @return array |
||
223 | */ |
||
224 | protected function getPermissionsRole() |
||
246 | |||
247 | /** |
||
248 | * Updates a user role |
||
249 | * @param string $role_id |
||
250 | */ |
||
251 | protected function updateRole($role_id) |
||
257 | |||
258 | /** |
||
259 | * Add a new user role at once |
||
260 | */ |
||
261 | protected function submitAddRole() |
||
268 | |||
269 | /** |
||
270 | * Add a new user role |
||
271 | */ |
||
272 | protected function addRole() |
||
282 | |||
283 | /** |
||
284 | * Add a new user role step by step |
||
285 | */ |
||
286 | protected function wizardAddRole() |
||
297 | |||
298 | } |
||
299 |