Complex classes like Member 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 Member, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 46 | class Member extends BaseBlameableModel |
||
| 47 | { |
||
| 48 | public $createdByAttribute = 'organization_guid'; |
||
| 49 | public $updatedByAttribute = false; |
||
| 50 | public $hostClass = Organization::class; |
||
| 51 | |||
| 52 | public $memberAttribute = 'user_guid'; |
||
| 53 | public $memberUserClass = User::class; |
||
| 54 | public $contentAttribute = 'nickname'; |
||
| 55 | private $noInitMemberUser; |
||
| 56 | /** |
||
| 57 | * @return User |
||
| 58 | */ |
||
| 59 | 30 | protected function getNoInitMemberUser() |
|
| 60 | { |
||
| 61 | 30 | if (!$this->noInitMemberUser) { |
|
| 62 | 30 | $class = $this->memberUserClass; |
|
| 63 | 30 | $this->noInitMemberUser = $class::buildNoInitModel(); |
|
| 64 | 30 | } |
|
| 65 | 30 | return $this->noInitMemberUser; |
|
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @inheritdoc |
||
| 70 | */ |
||
| 71 | 30 | public function init() |
|
| 72 | { |
||
| 73 | 30 | if (!is_string($this->queryClass)) { |
|
| 74 | 30 | $this->queryClass = MemberQuery::class; |
|
| 75 | 30 | } |
|
| 76 | 30 | if ($this->skipInit) { |
|
| 77 | 30 | return; |
|
| 78 | } |
||
| 79 | 30 | parent::init(); |
|
| 80 | 30 | } |
|
| 81 | |||
| 82 | public $descriptionAttribute = 'description'; |
||
| 83 | |||
| 84 | 30 | public function getMemberUserRules() |
|
| 85 | { |
||
| 86 | return [ |
||
| 87 | 30 | [$this->memberAttribute, 'required'], |
|
| 88 | 30 | [$this->memberAttribute, 'string', 'max' => 36], |
|
| 89 | 30 | ]; |
|
| 90 | } |
||
| 91 | |||
| 92 | 30 | public function getMemberRoleRules() |
|
| 93 | { |
||
| 94 | return [ |
||
| 95 | 30 | ['role', 'string', 'max' => 255], |
|
| 96 | 30 | ]; |
|
| 97 | } |
||
| 98 | |||
| 99 | 30 | public function getMemberPositionRules() |
|
| 100 | { |
||
| 101 | return [ |
||
| 102 | 30 | ['position', 'default', 'value' => ''], |
|
| 103 | 30 | ['position', 'string', 'max' => 255], |
|
| 104 | 30 | ]; |
|
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Set member user. |
||
| 109 | * @param User|string|integer $user |
||
| 110 | */ |
||
| 111 | 30 | public function setMemberUser($user) |
|
| 112 | { |
||
| 113 | 30 | $class = $this->memberUserClass; |
|
| 114 | 30 | if (is_numeric($user)) { |
|
| 115 | $user = $class::find()->id($user)->one(); |
||
| 116 | } |
||
| 117 | 30 | if ($user instanceof $class) { |
|
| 118 | 30 | $user = $user->getGUID(); |
|
| 119 | 30 | } |
|
| 120 | 30 | $this->{$this->memberAttribute} = $user; |
|
| 121 | 30 | } |
|
| 122 | |||
| 123 | 30 | public function getMemberUser() |
|
| 127 | |||
| 128 | /** |
||
| 129 | * Get Organization Query. |
||
| 130 | * Alias of `getHost` method. |
||
| 131 | * @return OrganizationQuery |
||
| 132 | */ |
||
| 133 | 25 | public function getOrganization() |
|
| 137 | |||
| 138 | /** |
||
| 139 | * Set Organization. |
||
| 140 | * @param BaseOrganization $organization |
||
| 141 | * @return boolean |
||
| 142 | */ |
||
| 143 | 30 | public function setOrganization($organization) |
|
| 147 | |||
| 148 | /** |
||
| 149 | * Assign role. |
||
| 150 | * The setting role operation will not take effect immediately. You should |
||
| 151 | * wrap this method and the subsequent save operations together into a |
||
| 152 | * transaction, in order to ensure data cosistency. |
||
| 153 | * @param Role $role |
||
| 154 | * @return boolean |
||
| 155 | */ |
||
| 156 | 30 | public function assignRole($role) |
|
| 157 | { |
||
| 158 | 30 | $user = $this->memberUser; |
|
| 159 | 30 | if (!$user) { |
|
| 160 | throw new InvalidValueException('Invalid User'); |
||
| 161 | } |
||
| 162 | 30 | if ($role instanceof Item) { |
|
| 163 | 7 | $role = $role->name; |
|
| 164 | 7 | } |
|
| 165 | 30 | $assignment = Yii::$app->authManager->getAssignment($role, $user); |
|
| 166 | 30 | if (!$assignment) { |
|
| 167 | 30 | $assignment = Yii::$app->authManager->assign($role, $user->getGUID()); |
|
|
|
|||
| 168 | 30 | } |
|
| 169 | 30 | return $this->setRole($role); |
|
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Assign administrator. |
||
| 174 | * @return boolean |
||
| 175 | * @throws \Exception |
||
| 176 | * @throws IntegrityException |
||
| 177 | */ |
||
| 178 | 7 | public function assignAdministrator() |
|
| 179 | { |
||
| 180 | 7 | $host = $this->organization; |
|
| 181 | /* @var $host Organization */ |
||
| 182 | 7 | $role = null; |
|
| 183 | 7 | if ($host->type == Organization::TYPE_ORGANIZATION) { |
|
| 184 | 7 | $role = new OrganizationAdmin(); |
|
| 185 | 7 | } elseif ($host->type == Organization::TYPE_DEPARTMENT) { |
|
| 186 | 1 | $role = new DepartmentAdmin(); |
|
| 187 | 1 | } |
|
| 188 | 7 | $transaction = Yii::$app->db->beginTransaction(); |
|
| 189 | try { |
||
| 190 | 7 | $this->assignRole($role); |
|
| 191 | 7 | if (!$this->save()) { |
|
| 192 | throw new IntegrityException("Failed to assign administrator."); |
||
| 193 | } |
||
| 194 | 7 | $transaction->commit(); |
|
| 195 | 7 | } catch (\Exception $ex) { |
|
| 196 | $transaction->rollBack(); |
||
| 197 | Yii::error($ex->getMessage(), __METHOD__); |
||
| 198 | throw $ex; |
||
| 199 | } |
||
| 200 | 7 | return true; |
|
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Set role. |
||
| 205 | * @param Role $role |
||
| 206 | * @return boolean |
||
| 207 | */ |
||
| 208 | 30 | public function setRole($role = null) |
|
| 209 | { |
||
| 210 | 30 | if (empty($role)) { |
|
| 211 | 15 | $role = ''; |
|
| 212 | 15 | } |
|
| 213 | 30 | if ($role instanceof Item) { |
|
| 214 | $role = $role->name; |
||
| 215 | } |
||
| 216 | 30 | $this->role = $role; |
|
| 217 | 30 | return true; |
|
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Revoke role. |
||
| 222 | * @param Role $role |
||
| 223 | */ |
||
| 224 | 15 | public function revokeRole($role) |
|
| 225 | { |
||
| 226 | 15 | $user = $this->memberUser; |
|
| 227 | 15 | if (!$user) { |
|
| 228 | throw new InvalidValueException('Invalid User'); |
||
| 229 | } |
||
| 230 | 15 | if ($role instanceof Item) { |
|
| 231 | 1 | $role = $role->name; |
|
| 232 | 1 | } |
|
| 233 | 15 | $transaction = Yii::$app->db->beginTransaction(); |
|
| 234 | try { |
||
| 235 | 15 | $assignment = Yii::$app->authManager->getAssignment($role, $user); |
|
| 236 | 15 | if ($assignment) { |
|
| 237 | 15 | $count = (int)($user->getOfMembers()->role($role)->count()); |
|
| 238 | 15 | if ($count == 1) { |
|
| 239 | 13 | Yii::$app->authManager->revoke($role, $user); |
|
| 240 | 13 | } |
|
| 241 | 15 | } |
|
| 242 | 15 | $this->setRole(); |
|
| 243 | 15 | if (!$this->save()) { |
|
| 244 | throw new IntegrityException('Save failed.'); |
||
| 245 | } |
||
| 246 | 15 | $transaction->commit(); |
|
| 247 | 15 | } catch (\Exception $ex) { |
|
| 248 | $transaction->rollBack(); |
||
| 249 | Yii::error($ex->getMessage(), __METHOD__); |
||
| 250 | throw $ex; |
||
| 251 | } |
||
| 252 | 15 | return true; |
|
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Revoke administrator. |
||
| 257 | * @return boolean |
||
| 258 | * @throws IntegrityException |
||
| 259 | */ |
||
| 260 | 1 | public function revokeAdministrator() |
|
| 261 | { |
||
| 262 | 1 | $host = $this->organization; |
|
| 263 | /* @var $host Organization */ |
||
| 264 | 1 | $role = null; |
|
| 265 | 1 | if ($host->type == Organization::TYPE_ORGANIZATION) { |
|
| 266 | 1 | $role = new OrganizationAdmin(); |
|
| 267 | 1 | } elseif ($host->type == Organization::TYPE_DEPARTMENT) { |
|
| 268 | $role = new DepartmentAdmin(); |
||
| 269 | } |
||
| 270 | 1 | $transaction = Yii::$app->db->beginTransaction(); |
|
| 271 | try { |
||
| 272 | 1 | $this->revokeRole($role); |
|
| 273 | 1 | if (!$this->save()) { |
|
| 274 | throw new IntegrityException("Failed to revoke administrator."); |
||
| 275 | } |
||
| 276 | 1 | $transaction->commit(); |
|
| 277 | 1 | } catch (\Exception $ex) { |
|
| 278 | $transaction->rollBack(); |
||
| 279 | Yii::error($ex->getMessage(), __METHOD__); |
||
| 280 | throw $ex; |
||
| 281 | } |
||
| 282 | 1 | return true; |
|
| 283 | } |
||
| 284 | |||
| 285 | 30 | public function rules() |
|
| 289 | |||
| 290 | /** |
||
| 291 | * @inheritdoc |
||
| 292 | */ |
||
| 293 | 30 | public static function tableName() |
|
| 297 | |||
| 298 | /** |
||
| 299 | * @inheritdoc |
||
| 300 | */ |
||
| 301 | 1 | public function attributeLabels() |
|
| 302 | { |
||
| 303 | return [ |
||
| 304 | 1 | 'guid' => Yii::t('user', 'GUID'), |
|
| 305 | 1 | 'id' => Yii::t('user', 'ID'), |
|
| 306 | 1 | 'organization_guid' => Yii::t('organization', 'Organization GUID'), |
|
| 307 | 'user_guid' => Yii::t('organization', 'User GUID'), |
||
| 308 | 'nickname' => Yii::t('user', 'Nickname'), |
||
| 309 | 'role' => Yii::t('organization', 'Role'), |
||
| 310 | 'position' => Yii::t('organization', 'Member Position'), |
||
| 311 | 'description' => Yii::t('organization', 'Description'), |
||
| 312 | 'ip' => Yii::t('user', 'IP Address'), |
||
| 313 | 'ip_type' => Yii::t('user', 'IP Address Type'), |
||
| 314 | 'created_at' => Yii::t('user', 'Creation Time'), |
||
| 315 | 'updated_at' => Yii::t('user', 'Last Updated Time'), |
||
| 316 | ]; |
||
| 317 | } |
||
| 318 | |||
| 319 | 6 | public function isDepartmentAdministrator() |
|
| 323 | |||
| 324 | 14 | public function isDepartmentCreator() |
|
| 328 | |||
| 329 | 6 | public function isOrganizationAdministrator() |
|
| 333 | |||
| 334 | 14 | public function isOrganizationCreator() |
|
| 338 | |||
| 339 | /** |
||
| 340 | * Check whether current member is administrator. |
||
| 341 | * @return boolean |
||
| 342 | */ |
||
| 343 | 6 | public function isAdministrator() |
|
| 347 | |||
| 348 | /** |
||
| 349 | * Check whether current member is creator. |
||
| 350 | * @return boolean |
||
| 351 | */ |
||
| 352 | 14 | public function isCreator() |
|
| 356 | |||
| 357 | /** |
||
| 358 | * We think it a `member` if `role` property is empty. |
||
| 359 | * @return boolean |
||
| 360 | */ |
||
| 361 | 2 | public function isOnlyMember() |
|
| 365 | } |
||
| 366 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.