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