Complex classes like UserModel 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 UserModel, and based on these observations, apply Extract Interface, too.
1 | <?php namespace Anomaly\UsersModule\User; |
||
19 | class UserModel extends UsersUsersEntryModel implements UserInterface, \Illuminate\Contracts\Auth\Authenticatable |
||
20 | { |
||
21 | |||
22 | use Notifiable; |
||
23 | use Authenticatable; |
||
24 | |||
25 | /** |
||
26 | * The eager loaded relationships. |
||
27 | * |
||
28 | * @var array |
||
29 | */ |
||
30 | protected $with = [ |
||
31 | 'roles', |
||
32 | ]; |
||
33 | |||
34 | /** |
||
35 | * The guarded attributes. |
||
36 | * |
||
37 | * @var array |
||
38 | */ |
||
39 | protected $guarded = [ |
||
40 | 'password', |
||
41 | ]; |
||
42 | |||
43 | /** |
||
44 | * Get the email. |
||
45 | * |
||
46 | * @return string |
||
47 | */ |
||
48 | public function getEmail() |
||
52 | |||
53 | /** |
||
54 | * Get the username. |
||
55 | * |
||
56 | * @return string |
||
57 | */ |
||
58 | public function getUsername() |
||
62 | |||
63 | /** |
||
64 | * Get the display name. |
||
65 | * |
||
66 | * @return string |
||
67 | */ |
||
68 | public function getDisplayName() |
||
72 | |||
73 | /** |
||
74 | * Get the first name. |
||
75 | * |
||
76 | * @return string |
||
77 | */ |
||
78 | public function getFirstName() |
||
82 | |||
83 | /** |
||
84 | * Get the last name. |
||
85 | * |
||
86 | * @return string |
||
87 | */ |
||
88 | public function getLastName() |
||
92 | |||
93 | /** |
||
94 | * Get related roles. |
||
95 | * |
||
96 | * @return RoleCollection |
||
97 | */ |
||
98 | public function getRoles() |
||
102 | |||
103 | /** |
||
104 | * Return whether a user is in a role. |
||
105 | * |
||
106 | * @param $role |
||
107 | * @return bool |
||
108 | */ |
||
109 | public function hasRole($role) |
||
128 | |||
129 | /** |
||
130 | * Return whether a user is in |
||
131 | * any of the provided roles. |
||
132 | * |
||
133 | * @param $roles |
||
134 | * @return bool |
||
135 | */ |
||
136 | public function hasAnyRole($roles) |
||
154 | |||
155 | /** |
||
156 | * Return whether the user |
||
157 | * is an admin or not. |
||
158 | * |
||
159 | * @return bool |
||
160 | */ |
||
161 | public function isAdmin() |
||
172 | |||
173 | /** |
||
174 | * Get the permissions. |
||
175 | * |
||
176 | * @return array |
||
177 | */ |
||
178 | public function getPermissions() |
||
182 | |||
183 | /** |
||
184 | * Return whether a user or it's roles has a permission. |
||
185 | * |
||
186 | * @param $permission |
||
187 | * @param bool $checkRoles |
||
188 | * @return mixed |
||
189 | */ |
||
190 | public function hasPermission($permission, $checkRoles = true) |
||
212 | |||
213 | /** |
||
214 | * Return whether a user has any of provided permission. |
||
215 | * |
||
216 | * @param array $permissions |
||
217 | * @param bool $checkRoles |
||
218 | * @return bool |
||
219 | */ |
||
220 | public function hasAnyPermission(array $permissions, $checkRoles = true) |
||
230 | |||
231 | /** |
||
232 | * Hash the password whenever setting it. |
||
233 | * |
||
234 | * @param $password |
||
235 | */ |
||
236 | public function setPasswordAttribute($password) |
||
240 | |||
241 | /** |
||
242 | * Return whether the model is deletable or not. |
||
243 | * |
||
244 | * @return bool |
||
245 | */ |
||
246 | public function isDeletable() |
||
260 | |||
261 | /** |
||
262 | * Return the activated flag. |
||
263 | * |
||
264 | * @return bool |
||
265 | */ |
||
266 | public function isActivated() |
||
270 | |||
271 | /** |
||
272 | * Return the enabled flag. |
||
273 | * |
||
274 | * @return bool |
||
275 | */ |
||
276 | public function isEnabled() |
||
280 | |||
281 | /** |
||
282 | * Get the reset code. |
||
283 | * |
||
284 | * @return string |
||
285 | */ |
||
286 | public function getResetCode() |
||
290 | |||
291 | /** |
||
292 | * Get the activation code. |
||
293 | * |
||
294 | * @return string |
||
295 | */ |
||
296 | public function getActivationCode() |
||
300 | |||
301 | /** |
||
302 | * Return the full name. |
||
303 | * |
||
304 | * @return string |
||
305 | */ |
||
306 | public function name() |
||
310 | |||
311 | /** |
||
312 | * Attach a role to the user. |
||
313 | * |
||
314 | * @param RoleInterface $role |
||
315 | */ |
||
316 | public function attachRole(RoleInterface $role) |
||
320 | |||
321 | /** |
||
322 | * Route notifications for the Slack channel. |
||
323 | * |
||
324 | * @return string |
||
325 | */ |
||
326 | public function routeNotificationForSlack() |
||
330 | } |
||
331 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.