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() |
||
49 | |||
50 | /** |
||
51 | * Check if the user has a role or roles. |
||
52 | * |
||
53 | * @param int|string|array $role |
||
54 | * @param bool $all |
||
55 | * @return bool |
||
56 | */ |
||
57 | public function hasRole($role, $all = false) |
||
69 | |||
70 | /** |
||
71 | * Check if the user has at least one of the given roles. |
||
72 | * |
||
73 | * @param int|string|array $role |
||
74 | * @return bool |
||
75 | */ |
||
76 | public function hasOneRole($role) |
||
86 | |||
87 | /** |
||
88 | * Check if the user has all roles. |
||
89 | * |
||
90 | * @param int|string|array $role |
||
91 | * @return bool |
||
92 | */ |
||
93 | public function hasAllRoles($role) |
||
103 | |||
104 | /** |
||
105 | * Check if the user has role. |
||
106 | * |
||
107 | * @param int|string $role |
||
108 | * @return bool |
||
109 | */ |
||
110 | public function checkRole($role) |
||
116 | |||
117 | /** |
||
118 | * Attach role to a user. |
||
119 | * |
||
120 | * @param int|Role $role |
||
121 | * @return null|bool |
||
122 | */ |
||
123 | public function attachRole($role) |
||
127 | |||
128 | /** |
||
129 | * Detach role from a user. |
||
130 | * |
||
131 | * @param int|Role $role |
||
132 | * @return int |
||
133 | */ |
||
134 | public function detachRole($role) |
||
140 | |||
141 | /** |
||
142 | * Detach all roles from a user. |
||
143 | * |
||
144 | * @return int |
||
145 | */ |
||
146 | public function detachAllRoles() |
||
152 | |||
153 | /** |
||
154 | * Sync roles for a user. |
||
155 | * |
||
156 | * @param array|\Ultraware\Roles\Models\Role[]|\Illuminate\Database\Eloquent\Collection $roles |
||
157 | * @return array |
||
158 | */ |
||
159 | public function syncRoles($roles) |
||
165 | |||
166 | /** |
||
167 | * Get role level of a user. |
||
168 | * |
||
169 | * @return int |
||
170 | */ |
||
171 | public function level() |
||
175 | |||
176 | /** |
||
177 | * Get all permissions from roles. |
||
178 | * |
||
179 | * @return Builder |
||
180 | */ |
||
181 | public function rolePermissions() |
||
214 | |||
215 | /** |
||
216 | * User belongs to many permissions. |
||
217 | * |
||
218 | * @return BelongsToMany |
||
219 | */ |
||
220 | public function userPermissions() |
||
224 | |||
225 | /** |
||
226 | * Get all permissions as collection. |
||
227 | * |
||
228 | * @return Collection |
||
229 | */ |
||
230 | public function getPermissions() |
||
240 | |||
241 | /** |
||
242 | * Check if the user has a permission or permissions. |
||
243 | * |
||
244 | * @param int|string|array $permission |
||
245 | * @param bool $all |
||
246 | * @return bool |
||
247 | */ |
||
248 | public function hasPermission($permission, $all = false) |
||
260 | |||
261 | /** |
||
262 | * Check if the user has at least one of the given permissions. |
||
263 | * |
||
264 | * @param int|string|array $permission |
||
265 | * @return bool |
||
266 | */ |
||
267 | public function hasOnePermission($permission) |
||
277 | |||
278 | /** |
||
279 | * Check if the user has all permissions. |
||
280 | * |
||
281 | * @param int|string|array $permission |
||
282 | * @return bool |
||
283 | */ |
||
284 | public function hasAllPermissions($permission) |
||
294 | |||
295 | /** |
||
296 | * Check if the user has a permission. |
||
297 | * |
||
298 | * @param int|string $permission |
||
299 | * @return bool |
||
300 | */ |
||
301 | public function checkPermission($permission) |
||
307 | |||
308 | /** |
||
309 | * Check if the user is allowed to manipulate with entity. |
||
310 | * |
||
311 | * @param string $providedPermission |
||
312 | * @param Model $entity |
||
313 | * @param bool $owner |
||
314 | * @param string $ownerColumn |
||
315 | * @return bool |
||
316 | */ |
||
317 | public function allowed($providedPermission, Model $entity, $owner = true, $ownerColumn = 'user_id') |
||
329 | |||
330 | /** |
||
331 | * Check if the user is allowed to manipulate with provided entity. |
||
332 | * |
||
333 | * @param string $providedPermission |
||
334 | * @param Model $entity |
||
335 | * @return bool |
||
336 | */ |
||
337 | protected function isAllowed($providedPermission, Model $entity) |
||
349 | |||
350 | /** |
||
351 | * Attach permission to a user. |
||
352 | * |
||
353 | * @param int|Permission $permission |
||
354 | * @return null|bool |
||
355 | */ |
||
356 | public function attachPermission($permission) |
||
360 | |||
361 | /** |
||
362 | * Detach permission from a user. |
||
363 | * |
||
364 | * @param int|Permission $permission |
||
365 | * @return int |
||
366 | */ |
||
367 | public function detachPermission($permission) |
||
373 | |||
374 | /** |
||
375 | * Detach all permissions from a user. |
||
376 | * |
||
377 | * @return int |
||
378 | */ |
||
379 | public function detachAllPermissions() |
||
385 | |||
386 | /** |
||
387 | * Sync permissions for a user. |
||
388 | * |
||
389 | * @param array|\Ultraware\Roles\Models\Permission[]|\Illuminate\Database\Eloquent\Collection $permissions |
||
390 | * @return array |
||
391 | */ |
||
392 | public function syncPermissions($permissions) |
||
398 | |||
399 | /** |
||
400 | * Check if pretend option is enabled. |
||
401 | * |
||
402 | * @return bool |
||
403 | */ |
||
404 | private function isPretendEnabled() |
||
408 | |||
409 | /** |
||
410 | * Allows to pretend or simulate package behavior. |
||
411 | * |
||
412 | * @param string $option |
||
413 | * @return bool |
||
414 | */ |
||
415 | private function pretend($option) |
||
419 | |||
420 | /** |
||
421 | * Get an array from argument. |
||
422 | * |
||
423 | * @param int|string|array $argument |
||
424 | * @return array |
||
425 | */ |
||
426 | private function getArrayFrom($argument) |
||
430 | |||
431 | public function callMagic($method, $parameters) |
||
448 | |||
449 | public function __call($method, $parameters) |
||
453 | } |
||
454 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: