Complex classes like HasRoleAndPermission 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 HasRoleAndPermission, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | trait HasRoleAndPermission |
||
15 | { |
||
16 | /** |
||
17 | * Property for caching roles. |
||
18 | * |
||
19 | * @var Collection|null |
||
20 | */ |
||
21 | protected $roles; |
||
22 | |||
23 | /** |
||
24 | * Property for caching permissions. |
||
25 | * |
||
26 | * @var Collection|null |
||
27 | */ |
||
28 | protected $permissions; |
||
29 | |||
30 | /** |
||
31 | * User belongs to many roles. |
||
32 | * |
||
33 | * @return BelongsToMany |
||
34 | */ |
||
35 | public function roles() |
||
39 | |||
40 | /** |
||
41 | * Get all roles as collection. |
||
42 | * |
||
43 | * @return Collection |
||
44 | */ |
||
45 | public function getRoles() |
||
55 | |||
56 | /** |
||
57 | * Check if the user has a role or roles. |
||
58 | * |
||
59 | * @param int|string|array $role |
||
60 | * @param bool $all |
||
61 | * @return bool |
||
62 | */ |
||
63 | public function hasRole($role, $all = false) |
||
75 | |||
76 | /** |
||
77 | * Check if the user has at least one of the given roles. |
||
78 | * |
||
79 | * @param int|string|array $role |
||
80 | * @return bool |
||
81 | */ |
||
82 | public function hasOneRole($role) |
||
92 | |||
93 | /** |
||
94 | * Check if the user has all roles. |
||
95 | * |
||
96 | * @param int|string|array $role |
||
97 | * @return bool |
||
98 | */ |
||
99 | public function hasAllRoles($role) |
||
109 | |||
110 | /** |
||
111 | * Check if the user has role. |
||
112 | * |
||
113 | * @param int|string $role |
||
114 | * @return bool |
||
115 | */ |
||
116 | public function checkRole($role) |
||
122 | |||
123 | /** |
||
124 | * Attach role to a user. |
||
125 | * |
||
126 | * @param int|Role $role |
||
127 | * @return null|bool |
||
128 | */ |
||
129 | public function attachRole($role) |
||
133 | |||
134 | /** |
||
135 | * Detach role from a user. |
||
136 | * |
||
137 | * @param int|Role $role |
||
138 | * @return int |
||
139 | */ |
||
140 | public function detachRole($role) |
||
146 | |||
147 | /** |
||
148 | * Detach all roles from a user. |
||
149 | * |
||
150 | * @return int |
||
151 | */ |
||
152 | public function detachAllRoles() |
||
158 | |||
159 | /** |
||
160 | * Sync roles for a user. |
||
161 | * |
||
162 | * @param array|\Ultraware\Roles\Models\Role[]|\Illuminate\Database\Eloquent\Collection $roles |
||
163 | * @return array |
||
164 | */ |
||
165 | public function syncRoles($roles) |
||
171 | |||
172 | /** |
||
173 | * Get all permissions from roles. |
||
174 | * |
||
175 | * @return Builder |
||
176 | */ |
||
177 | public function rolePermissions() |
||
192 | |||
193 | /** |
||
194 | * User belongs to many permissions. |
||
195 | * |
||
196 | * @return BelongsToMany |
||
197 | */ |
||
198 | public function userPermissions() |
||
202 | |||
203 | /** |
||
204 | * Get all permissions as collection. |
||
205 | * |
||
206 | * @return Collection |
||
207 | */ |
||
208 | public function getPermissions() |
||
212 | |||
213 | /** |
||
214 | * Check if the user has a permission or permissions. |
||
215 | * |
||
216 | * @param int|string|array $permission |
||
217 | * @param bool $all |
||
218 | * @return bool |
||
219 | */ |
||
220 | public function hasPermission($permission, $all = false) |
||
232 | |||
233 | /** |
||
234 | * Check if the user has at least one of the given permissions. |
||
235 | * |
||
236 | * @param int|string|array $permission |
||
237 | * @return bool |
||
238 | */ |
||
239 | public function hasOnePermission($permission) |
||
249 | |||
250 | /** |
||
251 | * Check if the user has all permissions. |
||
252 | * |
||
253 | * @param int|string|array $permission |
||
254 | * @return bool |
||
255 | */ |
||
256 | public function hasAllPermissions($permission) |
||
266 | |||
267 | /** |
||
268 | * Check if the user has a permission. |
||
269 | * |
||
270 | * @param int|string $permission |
||
271 | * @return bool |
||
272 | */ |
||
273 | public function checkPermission($permission) |
||
279 | |||
280 | /** |
||
281 | * Check if the user is allowed to manipulate with entity. |
||
282 | * |
||
283 | * @param string $providedPermission |
||
284 | * @param Model $entity |
||
285 | * @param bool $owner |
||
286 | * @param string $ownerColumn |
||
287 | * @return bool |
||
288 | */ |
||
289 | public function allowed($providedPermission, Model $entity, $owner = true, $ownerColumn = 'user_id') |
||
301 | |||
302 | /** |
||
303 | * Check if the user is allowed to manipulate with provided entity. |
||
304 | * |
||
305 | * @param string $providedPermission |
||
306 | * @param Model $entity |
||
307 | * @return bool |
||
308 | */ |
||
309 | protected function isAllowed($providedPermission, Model $entity) |
||
321 | |||
322 | /** |
||
323 | * Attach permission to a user. |
||
324 | * |
||
325 | * @param int|Permission $permission |
||
326 | * @return null|bool |
||
327 | */ |
||
328 | public function attachPermission($permission) |
||
332 | |||
333 | /** |
||
334 | * Detach permission from a user. |
||
335 | * |
||
336 | * @param int|Permission $permission |
||
337 | * @return int |
||
338 | */ |
||
339 | public function detachPermission($permission) |
||
345 | |||
346 | /** |
||
347 | * Detach all permissions from a user. |
||
348 | * |
||
349 | * @return int |
||
350 | */ |
||
351 | public function detachAllPermissions() |
||
357 | |||
358 | /** |
||
359 | * Sync permissions for a user. |
||
360 | * |
||
361 | * @param array|\Ultraware\Roles\Models\Permission[]|\Illuminate\Database\Eloquent\Collection $permissions |
||
362 | * @return array |
||
363 | */ |
||
364 | public function syncPermissions($permissions) |
||
370 | |||
371 | /** |
||
372 | * Check if pretend option is enabled. |
||
373 | * |
||
374 | * @return bool |
||
375 | */ |
||
376 | private function isPretendEnabled() |
||
380 | |||
381 | /** |
||
382 | * Allows to pretend or simulate package behavior. |
||
383 | * |
||
384 | * @param string $option |
||
385 | * @return bool |
||
386 | */ |
||
387 | private function pretend($option) |
||
391 | |||
392 | /** |
||
393 | * Get an array from argument. |
||
394 | * |
||
395 | * @param int|string|array $argument |
||
396 | * @return array |
||
397 | */ |
||
398 | private function getArrayFrom($argument) |
||
402 | |||
403 | public function callMagic($method, $parameters) |
||
415 | |||
416 | public function __call($method, $parameters) |
||
420 | } |
||
421 |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: