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 = aclGetALevel($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 | 2 | public function hasLevel($level) |
|
37 | { |
||
38 | 2 | $level = aclGetALevel($level); |
|
39 | |||
40 | 2 | 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 | public function getPermissions() |
|
126 | |||
127 | /** |
||
128 | * USER & LEVEL |
||
129 | * First try the cache to return the collection, |
||
130 | * then fetch it from the database. |
||
131 | * |
||
132 | * See @cacheGetAllPermissions() |
||
133 | * |
||
134 | * @return \Illuminate\Support\Collection |
||
135 | */ |
||
136 | 7 | public function getAllPermissions() |
|
140 | |||
141 | /** |
||
142 | * USER & LEVEL |
||
143 | * First try the cache to return the collection, |
||
144 | * then fetch it from the database. |
||
145 | * |
||
146 | * See @cacheGetInheritedPermissions() |
||
147 | * |
||
148 | * @return \Illuminate\Support\Collection |
||
149 | */ |
||
150 | 3 | public function getInheritedPermissions() |
|
154 | |||
155 | /** |
||
156 | * USER & LEVEL |
||
157 | * First try the cache to return the collection, |
||
158 | * then fetch it from the database. |
||
159 | * |
||
160 | * See @cacheGetAvailablePermissions() |
||
161 | * |
||
162 | * @return \Illuminate\Support\Collection |
||
163 | */ |
||
164 | 2 | public function getAvailablePermissions() |
|
168 | |||
169 | /** |
||
170 | * USER & LEVEL |
||
171 | * First try the cache to return the collection, |
||
172 | * then fetch it from the database. |
||
173 | * |
||
174 | * See @cacheHasPermissionTo() |
||
175 | * |
||
176 | * @param $permission |
||
177 | * @return bool |
||
178 | */ |
||
179 | 3 | View Code Duplication | public function hasPermissionTo($permission) |
189 | |||
190 | /** |
||
191 | * USER & LEVEL |
||
192 | * Returns if a level is lower ranking than another level, |
||
193 | * or if a user is lower ranking than another user. |
||
194 | * |
||
195 | * @param $other |
||
196 | * @return bool |
||
197 | */ |
||
198 | 3 | public function isLowerThan($other) |
|
202 | |||
203 | /** |
||
204 | * USER & LEVEL |
||
205 | * Returns if a level is higher ranking than another level, |
||
206 | * or if a user is higher ranking than another user. |
||
207 | * |
||
208 | * @param $other |
||
209 | * @return bool |
||
210 | */ |
||
211 | 3 | public function isHigherThan($other) |
|
215 | |||
216 | /** |
||
217 | * USER & LEVEL |
||
218 | * Returns if a equal is lower ranking than another level, |
||
219 | * or if a user is equal ranking than another user. |
||
220 | * |
||
221 | * @param $other |
||
222 | * @return bool |
||
223 | */ |
||
224 | 1 | public function isEqualTo($other) |
|
228 | |||
229 | |||
230 | /* ------------------------------------------------------------------------------------------------ |
||
231 | | Other Functions |
||
232 | | ------------------------------------------------------------------------------------------------ |
||
233 | */ |
||
234 | /** |
||
235 | * USER & LEVEL |
||
236 | * Pull the specific type requested from the cache |
||
237 | * |
||
238 | * @param $type |
||
239 | * @return \Illuminate\Support\Collection |
||
240 | */ |
||
241 | 12 | View Code Duplication | protected function cachePull($type) |
251 | /** |
||
252 | * USER & LEVEL |
||
253 | * Return a collection of permissions |
||
254 | * assigned to a user |
||
255 | * |
||
256 | * @return \Illuminate\Support\Collection |
||
257 | */ |
||
258 | 2 | protected function cacheGetPermissions() |
|
262 | |||
263 | /** |
||
264 | * USER & LEVEL |
||
265 | * Return a collection of all permissions |
||
266 | * associated to a user by direct or |
||
267 | * inheritance |
||
268 | * |
||
269 | * @return \Illuminate\Support\Collection |
||
270 | */ |
||
271 | 7 | protected function cacheGetAllPermissions() |
|
285 | |||
286 | /** |
||
287 | * USER & LEVEL |
||
288 | * Return a collection of permissions that |
||
289 | * are inherited from a higher level |
||
290 | * |
||
291 | * @return \Illuminate\Support\Collection |
||
292 | */ |
||
293 | 3 | protected function cacheGetInheritedPermissions() |
|
308 | |||
309 | /** |
||
310 | * USER & LEVEL |
||
311 | * Return a collection of permissions still able to be assigned |
||
312 | * ie, not already inherited or explicitly assigned |
||
313 | * |
||
314 | * @return \Illuminate\Support\Collection |
||
315 | */ |
||
316 | 2 | protected function cacheGetAvailablePermissions() |
|
322 | |||
323 | /** |
||
324 | * USER & LEVEL |
||
325 | * Check if a model has permission to do something |
||
326 | * |
||
327 | * @param $permission |
||
328 | * @return bool |
||
329 | */ |
||
330 | 3 | protected function cacheHasPermissionTo($permission) |
|
344 | |||
345 | /** |
||
346 | * USER & LEVEL |
||
347 | * Modify a given set of permissions, given the action to be taken |
||
348 | * |
||
349 | * @param $permissions |
||
350 | * @param string $action |
||
351 | */ |
||
352 | 24 | protected function modifyPermissions($permissions, $action) |
|
375 | |||
376 | /** |
||
377 | * USER & LEVEL |
||
378 | * Helper function to build the array |
||
379 | * for modifyPermissions |
||
380 | * |
||
381 | * @param $permissions |
||
382 | * @param $action |
||
383 | * @return array |
||
384 | * @throws PermissionNotFoundException |
||
385 | */ |
||
386 | 24 | protected function buildPermissionsArray($permissions, $action) |
|
404 | |||
405 | /** |
||
406 | * USER & LEVEL |
||
407 | * Helper function to handle adding or negating permissions |
||
408 | * |
||
409 | * @param $permissionObjects |
||
410 | */ |
||
411 | 23 | protected function addOrNegate($permissionObjects) |
|
421 | |||
422 | /** |
||
423 | * USER & LEVEL |
||
424 | * Helper function to handle removing permissions |
||
425 | * |
||
426 | * @param $permissionObjects |
||
427 | */ |
||
428 | 3 | View Code Duplication | protected function remove($permissionObjects) |
440 | |||
441 | /** |
||
442 | * USER & LEVEL |
||
443 | * Help function to handle syncing permissions |
||
444 | * |
||
445 | * @param $permissionObjects |
||
446 | */ |
||
447 | 2 | View Code Duplication | protected function sync($permissionObjects) |
462 | |||
463 | /** |
||
464 | * USER & LEVEL |
||
465 | * Helper function to get the permission whether it is the permission ID |
||
466 | * or the permission name, or the permission object itself. |
||
467 | * |
||
468 | * @param $permission |
||
469 | * @return \z1haze\Acl\Models\Permission |
||
470 | * @throws PermissionNotFoundException |
||
471 | */ |
||
472 | 24 | protected function getPermission($permission) |
|
488 | } |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: