Complex classes like UserController 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 UserController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class UserController extends Controller |
||
| 28 | { |
||
| 29 | public $userClass; |
||
| 30 | |||
| 31 | public $defaultAction = 'show'; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Check and get valid User. |
||
| 35 | * @return User |
||
| 36 | * @throws Exception throw if User is not an instance inherited from `\rhosocial\user\User`. |
||
| 37 | */ |
||
| 38 | protected function checkUserClass() |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Get user from database. |
||
| 52 | * @param User|string|integer $user User ID. |
||
| 53 | * @return User |
||
| 54 | */ |
||
| 55 | protected function getUser($user) |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Register new User. |
||
| 71 | * @param string $password Password. |
||
| 72 | * @param string $nickname If profile contains this property, this parameter is required. |
||
| 73 | * @param string $firstName If profile contains this property, this parameter is required. |
||
| 74 | * @param string $lastName If profile contains this propery, this parameter is required. |
||
| 75 | */ |
||
| 76 | public function actionRegister($password, $nickname = null, $firstName = null, $lastName = null) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Deregister user. |
||
| 99 | * @param User|string|integer $user The user to be deregistered. |
||
| 100 | */ |
||
| 101 | public function actionDeregister($user) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Show User Information. |
||
| 113 | * @param User|string|integer $user User ID. |
||
| 114 | * @param boolean $guid Show GUID? |
||
| 115 | * @param boolean $passHash Show PasswordH Hash? |
||
| 116 | * @param boolean $accessToken Show Access Token? |
||
| 117 | * @param boolean $authKey Show Authentication Key? |
||
| 118 | */ |
||
| 119 | public function actionShow($user, $guid = false, $passHash = false, $accessToken = false, $authKey = false) |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Show statistics. |
||
| 141 | * @param User|string|integer $user User ID. |
||
| 142 | */ |
||
| 143 | public function actionStat($user = null) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Assign a role to user or revoke a role. |
||
| 162 | * @param User|string|integer $user User ID. |
||
| 163 | * @param string $operation Only `assign` and `revoke` are acceptable. |
||
| 164 | * @param string $role Role name. |
||
| 165 | */ |
||
| 166 | public function actionRole($user, $operation, $role) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Assign a permission to user or revoke a permission. |
||
| 202 | * @param User|string|integer $user User ID. |
||
| 203 | * @param string $operation Only `assign` and `revoke` are acceptable. |
||
| 204 | * @param string $permission Permission name. |
||
| 205 | */ |
||
| 206 | public function actionPermission($user, $operation, $permission) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Validate password. |
||
| 242 | * @param User|string|integer $user User ID. |
||
| 243 | * @param password $password Password. |
||
| 244 | */ |
||
| 245 | public function actionValidatePassword($user, $password) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Change password directly. |
||
| 258 | * @param User|string|integer $user User ID. |
||
| 259 | * @param string $password Password. |
||
| 260 | */ |
||
| 261 | public function actionPassword($user, $password) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Confirm password in history. |
||
| 275 | * This command will list all matching passwords in reverse order. |
||
| 276 | * @param User|string|integer $user User ID. |
||
| 277 | * @param string $password Password. |
||
| 278 | */ |
||
| 279 | public function actionConfirmPasswordHistory($user, $password) |
||
| 296 | } |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: