Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like UserAndLevel 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 UserAndLevel, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | trait UserAndLevel |
||
13 | { |
||
14 | /** |
||
15 | * USER & LEVEL |
||
16 | * Check if a model has a given level |
||
17 | * |
||
18 | * @param mixed level |
||
19 | * @return bool |
||
20 | */ |
||
21 | 1 | public function isLevel($level) |
|
22 | { |
||
23 | 1 | $level = $this->getALevel($level); |
|
|
|||
24 | |||
25 | 1 | return is_a($this, config('laravel-acl.level'), true) ? $this->name == $level->name : $this->level->name == $level->name; |
|
26 | } |
||
27 | |||
28 | /** |
||
29 | * USER & LEVEL |
||
30 | * Check if a user has or has inherited |
||
31 | * a given level |
||
32 | * |
||
33 | * @param $level |
||
34 | * @return bool |
||
35 | */ |
||
36 | 1 | public function hasLevel($level) |
|
37 | { |
||
38 | 1 | $level = $this->getLevel($level); |
|
39 | |||
40 | 1 | return is_a($this, config('laravel-acl.level'), true) ? $this->rank <= $level->rank : $this->level->rank <= $level->rank; |
|
41 | } |
||
42 | |||
43 | /** |
||
44 | * USER & LEVEL |
||
45 | * Add a single permission to a model |
||
46 | * |
||
47 | * @param $permission |
||
48 | */ |
||
49 | 16 | public function addPermission($permission) |
|
53 | |||
54 | /** |
||
55 | * USER & LEVEL |
||
56 | * Add an array of permissions to a model |
||
57 | * |
||
58 | * @param $permissions |
||
59 | */ |
||
60 | 9 | public function addPermissions(array $permissions) |
|
64 | |||
65 | /** |
||
66 | * USER & LEVEL |
||
67 | * Remove a permission from a model |
||
68 | * |
||
69 | * @param $permission |
||
70 | */ |
||
71 | 2 | public function removePermission($permission) |
|
75 | |||
76 | /** |
||
77 | * USER & LEVEL |
||
78 | * Remove an array of permissions objects or permission names (can be mixed) |
||
79 | * |
||
80 | * @param $permissions |
||
81 | */ |
||
82 | 1 | public function removePermissions(array $permissions) |
|
86 | |||
87 | /** |
||
88 | * USER & LEVEL |
||
89 | * Sync permissions on a model |
||
90 | * |
||
91 | * @param $permissions |
||
92 | */ |
||
93 | 2 | public function syncPermissions(array $permissions) |
|
97 | |||
98 | /** |
||
99 | * USER & LEVEL |
||
100 | * Clear permissions from a model |
||
101 | */ |
||
102 | 3 | public function clearPermissions() |
|
112 | |||
113 | /** |
||
114 | * USER & LEVEL |
||
115 | * First try the cache to return the collection, |
||
116 | * then fetch it from the database. |
||
117 | * |
||
118 | * See @cacheGetPermissions() |
||
119 | * |
||
120 | * @return \Illuminate\Support\Collection |
||
121 | */ |
||
122 | 2 | View Code Duplication | public function getPermissions() |
132 | |||
133 | /** |
||
134 | * USER & LEVEL |
||
135 | * First try the cache to return the collection, |
||
136 | * then fetch it from the database. |
||
137 | * |
||
138 | * See @cacheGetAllPermissions() |
||
139 | * |
||
140 | * @return \Illuminate\Support\Collection |
||
141 | */ |
||
142 | 7 | View Code Duplication | public function getAllPermissions() |
152 | |||
153 | /** |
||
154 | * USER & LEVEL |
||
155 | * First try the cache to return the collection, |
||
156 | * then fetch it from the database. |
||
157 | * |
||
158 | * See @cacheGetInheritedPermissions() |
||
159 | * |
||
160 | * @return \Illuminate\Support\Collection |
||
161 | */ |
||
162 | 3 | View Code Duplication | public function getInheritedPermissions() |
172 | |||
173 | /** |
||
174 | * USER & LEVEL |
||
175 | * First try the cache to return the collection, |
||
176 | * then fetch it from the database. |
||
177 | * |
||
178 | * See @cacheGetAvailablePermissions() |
||
179 | * |
||
180 | * @return \Illuminate\Support\Collection |
||
181 | */ |
||
182 | 2 | View Code Duplication | public function getAvailablePermissions() |
192 | |||
193 | /** |
||
194 | * USER & LEVEL |
||
195 | * First try the cache to return the collection, |
||
196 | * then fetch it from the database. |
||
197 | * |
||
198 | * See @cacheHasPermissionTo() |
||
199 | * |
||
200 | * @param $permission |
||
201 | * @return bool |
||
202 | */ |
||
203 | 3 | public function hasPermissionTo($permission) |
|
213 | |||
214 | /** |
||
215 | * USER & LEVEL |
||
216 | * Returns if a level is lower ranking than another level, |
||
217 | * or if a user is lower ranking than another user. |
||
218 | * |
||
219 | * @param $other |
||
220 | * @return bool |
||
221 | */ |
||
222 | 3 | public function isLowerThan($other) |
|
226 | |||
227 | /** |
||
228 | * USER & LEVEL |
||
229 | * Returns if a level is higher ranking than another level, |
||
230 | * or if a user is higher ranking than another user. |
||
231 | * |
||
232 | * @param $other |
||
233 | * @return bool |
||
234 | */ |
||
235 | 3 | public function isHigherThan($other) |
|
239 | |||
240 | /** |
||
241 | * USER & LEVEL |
||
242 | * Returns if a equal is lower ranking than another level, |
||
243 | * or if a user is equal ranking than another user. |
||
244 | * |
||
245 | * @param $other |
||
246 | * @return bool |
||
247 | */ |
||
248 | 1 | public function isEqualTo($other) |
|
252 | |||
253 | |||
254 | /* ------------------------------------------------------------------------------------------------ |
||
255 | | Other Functions |
||
256 | | ------------------------------------------------------------------------------------------------ |
||
257 | */ |
||
258 | /** |
||
259 | * USER & LEVEL |
||
260 | * Return a collection of permissions |
||
261 | * assigned to a user |
||
262 | * |
||
263 | * @return \Illuminate\Support\Collection |
||
264 | */ |
||
265 | 2 | protected function cacheGetPermissions() |
|
269 | |||
270 | /** |
||
271 | * USER & LEVEL |
||
272 | * Return a collection of all permissions |
||
273 | * associated to a user by direct or |
||
274 | * inheritance |
||
275 | * |
||
276 | * @return \Illuminate\Support\Collection |
||
277 | */ |
||
278 | 7 | protected function cacheGetAllPermissions() |
|
300 | |||
301 | /** |
||
302 | * USER & LEVEL |
||
303 | * Return a collection of permissions that |
||
304 | * are inherited from a higher level |
||
305 | * |
||
306 | * @return \Illuminate\Support\Collection |
||
307 | */ |
||
308 | 3 | protected function cacheGetInheritedPermissions() |
|
323 | |||
324 | /** |
||
325 | * USER & LEVEL |
||
326 | * Return a collection of permissions still able to be assigned |
||
327 | * ie, not already inherited or explicitly assigned |
||
328 | * |
||
329 | * @return \Illuminate\Support\Collection |
||
330 | */ |
||
331 | 2 | protected function cacheGetAvailablePermissions() |
|
337 | |||
338 | /** |
||
339 | * USER & LEVEL |
||
340 | * Check if a model has permission to do something |
||
341 | * |
||
342 | * @param $permission |
||
343 | * @return bool |
||
344 | */ |
||
345 | 3 | protected function cacheHasPermissionTo($permission) |
|
358 | |||
359 | /** |
||
360 | * USER & LEVEL |
||
361 | * Modify a given set of permissions, given the action to be taken |
||
362 | * |
||
363 | * @param $permissions |
||
364 | * @param string $action |
||
365 | */ |
||
366 | 24 | protected function modifyPermissions($permissions, $action) |
|
389 | |||
390 | /** |
||
391 | * USER & LEVEL |
||
392 | * Helper function to build the array |
||
393 | * for modifyPermissions |
||
394 | * |
||
395 | * @param $permissions |
||
396 | * @param $action |
||
397 | * @return array |
||
398 | * @throws PermissionNotFoundException |
||
399 | */ |
||
400 | 24 | protected function buildPermissionsArray($permissions, $action) |
|
418 | |||
419 | /** |
||
420 | * USER & LEVEL |
||
421 | * Helper function to handle adding or negating permissions |
||
422 | * |
||
423 | * @param $permissionObjects |
||
424 | */ |
||
425 | 23 | protected function addOrNegate($permissionObjects) |
|
435 | |||
436 | /** |
||
437 | * USER & LEVEL |
||
438 | * Helper function to handle removing permissions |
||
439 | * |
||
440 | * @param $permissionObjects |
||
441 | */ |
||
442 | 3 | View Code Duplication | protected function remove($permissionObjects) |
454 | |||
455 | /** |
||
456 | * USER & LEVEL |
||
457 | * Help function to handle syncing permissions |
||
458 | * |
||
459 | * @param $permissionObjects |
||
460 | */ |
||
461 | 2 | View Code Duplication | protected function sync($permissionObjects) |
476 | |||
477 | /** |
||
478 | * USER & LEVEL |
||
479 | * Helper function to get the permission whether it is the permission ID |
||
480 | * or the permission name, or the permission object itself. |
||
481 | * |
||
482 | * @param $permission |
||
483 | * @return \z1haze\Acl\Models\Permission |
||
484 | * @throws PermissionNotFoundException |
||
485 | */ |
||
486 | 24 | View Code Duplication | protected function getPermission($permission) |
502 | } |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.