Complex classes like OrganizationController 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 OrganizationController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class OrganizationController extends Controller |
||
30 | { |
||
31 | public $userClass; |
||
32 | public $organizationClass; |
||
33 | public $defaultAction = 'show'; |
||
34 | |||
35 | /** |
||
36 | * Check user class. |
||
37 | * @return User |
||
38 | * @throws Exception throw if User is not an instance inherited from `\rhosocial\user\User`. |
||
39 | */ |
||
40 | protected function checkUserClass() |
||
51 | |||
52 | /** |
||
53 | * Get user from database. |
||
54 | * @param User|string|integer $user User ID. |
||
55 | * @throws Exception |
||
56 | * @return User |
||
57 | */ |
||
58 | protected function getUser($user) |
||
71 | |||
72 | /** |
||
73 | * Check organization class. |
||
74 | * @return Organization |
||
75 | * @throws Exception throw if Organization is not an instance inherited from `\rhosocial\organization\Organization`. |
||
76 | */ |
||
77 | protected function checkOrganizationClass() |
||
88 | |||
89 | /** |
||
90 | * Get organization. |
||
91 | * @param Organization|string|integer $organization |
||
92 | * @throws Exception |
||
93 | * @return Organization |
||
94 | */ |
||
95 | protected function getOrganization($organization) |
||
108 | |||
109 | /** |
||
110 | * Assign SetUpOrganization permission and set upper limit. |
||
111 | * If the permission has been assigned, it will only change the limit. |
||
112 | * @param User|string|integer $user |
||
113 | * @param string|integer $limit |
||
114 | * @return boolean |
||
115 | */ |
||
116 | public function actionAssignSetUpOrganization($user, $limit = 1) |
||
165 | |||
166 | /** |
||
167 | * Revoke SetUpOrganization permission. |
||
168 | * @param User|string|integer $user |
||
169 | * @return boolean |
||
170 | */ |
||
171 | public function actionRevokeSetUpOrganization($user) |
||
194 | |||
195 | /** |
||
196 | * Show Organization Information. |
||
197 | * @param Organization|string|integer $organization Organization's or department's ID. |
||
198 | * @return integer |
||
199 | */ |
||
200 | public function actionShow($organization) |
||
206 | |||
207 | /** |
||
208 | * Set up organization. |
||
209 | * @param User|string|integer $user Organization creator. |
||
210 | * @param string $name |
||
211 | * @throws Exception |
||
212 | * @return integer |
||
213 | */ |
||
214 | public function actionSetUpOrganization($user, $name) |
||
228 | |||
229 | /** |
||
230 | * Set up department. |
||
231 | * @param User|string|integer $user Department creator. |
||
232 | * @param string $name |
||
233 | * @param Organization|string|integer $parent |
||
234 | * @throws Exception |
||
235 | * @return integer |
||
236 | */ |
||
237 | public function actionSetUpDepartment($user, $name, $parent) |
||
252 | |||
253 | /** |
||
254 | * Revoke organization. |
||
255 | * @param Organization|string|integer $organization |
||
256 | * @throws Exception |
||
257 | * @return integer |
||
258 | */ |
||
259 | public function actionRevokeOrganization($organization) |
||
269 | |||
270 | /** |
||
271 | * Add administrator. |
||
272 | * @param Organization|string|integer $organization |
||
273 | * @param User|string|integer $user |
||
274 | * @throws Exception |
||
275 | * @return integer |
||
276 | */ |
||
277 | public function actionAddAdministrator($organization, $user) |
||
287 | |||
288 | /** |
||
289 | * Remove administrator |
||
290 | * @param Organization|string|integer $organization |
||
291 | * @param User|string|integer $user |
||
292 | * @param boolean $keepMember |
||
293 | * @throws Exception |
||
294 | * @return integer |
||
295 | */ |
||
296 | public function actionRemoveAdministrator($organization, $user, $keepMember = "yes") |
||
309 | |||
310 | /** |
||
311 | * Add member. |
||
312 | * @param Organization|string|intger $organization |
||
313 | * @param User|string|integer $user |
||
314 | * @throws Exception |
||
315 | * @return integer |
||
316 | */ |
||
317 | public function actionAddMember($organization, $user) |
||
327 | |||
328 | /** |
||
329 | * Remove member. |
||
330 | * @param Organization|string|intger $organization |
||
331 | * @param User|string|integer $user |
||
332 | * @throws Exception |
||
333 | * @return integer |
||
334 | */ |
||
335 | public function actionRemoveMember($organization, $user) |
||
346 | } |
||
347 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: