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 UserTrait 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 UserTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | trait UserTrait |
||
18 | { |
||
19 | |||
20 | //in EntrustUserTrait.php |
||
21 | public function cachedRole() |
||
28 | |||
29 | View Code Duplication | public static function bootEntrustUserTrait() |
|
46 | |||
47 | /** |
||
48 | * Many-to-Many relations with Role. |
||
49 | * |
||
50 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
51 | */ |
||
52 | public function role() |
||
56 | |||
57 | /** |
||
58 | * Checks if the user has a role by its name. |
||
59 | * |
||
60 | * @param string|array $name Role name or array of role names. |
||
61 | * @param bool $requireAll All roles in the array are required. |
||
62 | * @return bool |
||
63 | */ |
||
64 | View Code Duplication | public function hasRole($name, $requireAll = false) |
|
87 | |||
88 | /** |
||
89 | * Check if user has a permission by its name. |
||
90 | * |
||
91 | * @param string|array $permission Permission string or array of permissions. |
||
92 | * @param bool $requireAll All permissions in the array are required. |
||
93 | * @return bool |
||
94 | */ |
||
95 | View Code Duplication | public function can($permission, $requireAll = false) |
|
123 | |||
124 | /** |
||
125 | * Checks role(s) and permission(s). |
||
126 | * |
||
127 | * @param string|array $roles Array of roles or comma separated string |
||
128 | * @param string|array $permissions Array of permissions or comma separated string. |
||
129 | * @param array $options validate_all (true|false) or return_type (boolean|array|both) |
||
130 | * @throws \InvalidArgumentException |
||
131 | * @return array|bool |
||
132 | */ |
||
133 | public function ability($roles, $permissions, $options = []) |
||
192 | |||
193 | /** |
||
194 | * Alias to eloquent many-to-many relation's attach() method. |
||
195 | * |
||
196 | * @param mixed $role |
||
197 | */ |
||
198 | View Code Duplication | public function attachRole($role) |
|
210 | |||
211 | /** |
||
212 | * Alias to eloquent many-to-many relation's detach() method. |
||
213 | * |
||
214 | * @param mixed $role |
||
215 | */ |
||
216 | View Code Duplication | public function detachRole($role) |
|
228 | |||
229 | /** |
||
230 | * Attach multiple roles to a user |
||
231 | * |
||
232 | * @param mixed $roles |
||
233 | */ |
||
234 | public function attachRoles($roles) |
||
240 | |||
241 | /** |
||
242 | * Detach multiple roles from a user |
||
243 | * |
||
244 | * @param mixed $roles |
||
245 | */ |
||
246 | public function detachRoles($roles = null) |
||
256 | } |
||
257 |
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.