Complex classes like User 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 User, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class User extends Controller |
||
19 | { |
||
20 | |||
21 | /** |
||
22 | * User model class instance |
||
23 | * @var \gplcart\modules\api\models\User $user_model |
||
24 | */ |
||
25 | protected $user_model; |
||
26 | |||
27 | /** |
||
28 | * Pager limit |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $data_limit; |
||
32 | |||
33 | /** |
||
34 | * The current updating user |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $data_user = array(); |
||
38 | |||
39 | /** |
||
40 | * @param UserModel $user |
||
41 | */ |
||
42 | public function __construct(UserModel $user) |
||
48 | |||
49 | /** |
||
50 | * Route callback |
||
51 | * Displays the API users overview page |
||
52 | */ |
||
53 | public function listUser() |
||
64 | |||
65 | /** |
||
66 | * Applies an action to the selected users |
||
67 | */ |
||
68 | protected function actionListUser() |
||
85 | |||
86 | /** |
||
87 | * Sets filter parameters |
||
88 | */ |
||
89 | protected function setFilterListUser() |
||
93 | |||
94 | /** |
||
95 | * Sets pager |
||
96 | * @return array |
||
97 | */ |
||
98 | protected function setPagerListUser() |
||
110 | |||
111 | /** |
||
112 | * Returns an array of API users |
||
113 | * @return array |
||
114 | */ |
||
115 | protected function getListUser() |
||
122 | |||
123 | /** |
||
124 | * Sets title on the credential overview page |
||
125 | */ |
||
126 | protected function setTitleListUser() |
||
130 | |||
131 | /** |
||
132 | * Sets breadcrumbs on the user overview page |
||
133 | */ |
||
134 | protected function setBreadcrumbListUser() |
||
143 | |||
144 | /** |
||
145 | * Render and output the user overview page |
||
146 | */ |
||
147 | protected function outputListUser() |
||
151 | |||
152 | /** |
||
153 | * Page callback |
||
154 | * Displays the edit user page |
||
155 | * @param null|int $api_user_id |
||
156 | */ |
||
157 | public function editUser($api_user_id = null) |
||
169 | |||
170 | /** |
||
171 | * Prepare template variables |
||
172 | */ |
||
173 | protected function setDataEditUser() |
||
181 | |||
182 | /** |
||
183 | * Sets an API user |
||
184 | * @param $api_user_id |
||
185 | */ |
||
186 | protected function setUser($api_user_id) |
||
195 | |||
196 | /** |
||
197 | * Sets titles on the edit user page |
||
198 | */ |
||
199 | protected function setTitleEditUser() |
||
209 | |||
210 | /** |
||
211 | * Sets breadcrumbs on the user edit page |
||
212 | */ |
||
213 | protected function setBreadcrumbEditUser() |
||
229 | |||
230 | /** |
||
231 | * Handles a submitted user |
||
232 | */ |
||
233 | protected function submitEditUser() |
||
245 | |||
246 | /** |
||
247 | * Validates a submitted user data |
||
248 | */ |
||
249 | protected function validateEditUser() |
||
285 | |||
286 | /** |
||
287 | * Updates a submitted user |
||
288 | */ |
||
289 | protected function updateUser() |
||
299 | |||
300 | /** |
||
301 | * Adds a new user |
||
302 | */ |
||
303 | protected function addUser() |
||
313 | |||
314 | /** |
||
315 | * Delete a submitted user |
||
316 | */ |
||
317 | protected function deleteUser() |
||
327 | |||
328 | /** |
||
329 | * Render and output the user edit page |
||
330 | */ |
||
331 | protected function outputEditUser() |
||
335 | |||
336 | } |
||
337 |