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 User 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 User, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class User extends \SerializableObject |
||
22 | { |
||
23 | /** |
||
24 | * An array to cache the title to string mappings so that they don't need to be pulled from the database |
||
25 | * everytime |
||
26 | */ |
||
27 | public static $titlenames = null; |
||
28 | |||
29 | /** |
||
30 | * Is this user in the Group or a child of that group? |
||
31 | * |
||
32 | * @param string $name The name of the group to check if the user is in |
||
33 | * |
||
34 | * @return boolean True if the user is in the group, false otherwise |
||
35 | */ |
||
36 | public function isInGroupNamed($name) |
||
40 | |||
41 | public function __get($propName) |
||
45 | |||
46 | public function __set($propName, $value) |
||
49 | |||
50 | public function __isset($propName) |
||
54 | |||
55 | /** |
||
56 | * The list of titles for the user |
||
57 | * |
||
58 | * @return boolean|array The user's title(s) in user friendly strings |
||
59 | * |
||
60 | * @SuppressWarnings("StaticAccess") |
||
61 | */ |
||
62 | public function getTitleNames() |
||
92 | |||
93 | /** |
||
94 | * The groups the user is a part of |
||
95 | * |
||
96 | * @return boolean|array The user's Auth\Group structures |
||
97 | */ |
||
98 | public function getGroups() |
||
102 | |||
103 | /** |
||
104 | * Add a supplemental login type that the user can use to login |
||
105 | * |
||
106 | * @param string $provider The hostname for the provider |
||
107 | */ |
||
108 | public function addLoginProvider($provider) |
||
121 | |||
122 | /** |
||
123 | * Can the user login with this provider? |
||
124 | * |
||
125 | * @param string $provider The hostname for the provider |
||
126 | * |
||
127 | * @return boolean true if they can login with the provider, false otherwise |
||
128 | */ |
||
129 | public function canLoginWith($provider) |
||
146 | |||
147 | /** |
||
148 | * Set the user's password without verifying the current password |
||
149 | * |
||
150 | * @param string $password The new user password |
||
151 | * |
||
152 | * @return boolean true if the user's password was changed, false otherwise |
||
153 | */ |
||
154 | protected function setPass($password) |
||
158 | |||
159 | /** |
||
160 | * Has the user completely filled out their user profile? |
||
161 | * |
||
162 | * @return boolean true if the user's profile is complete, false otherwise |
||
163 | */ |
||
164 | public function isProfileComplete() |
||
174 | |||
175 | /** |
||
176 | * Validate that the user's password is the specified password |
||
177 | * |
||
178 | * @param string $password The user's current password |
||
179 | * |
||
180 | * @return boolean true if the user's password is correct, false otherwise |
||
181 | * |
||
182 | * @SuppressWarnings("UnusedFormalParameter") |
||
183 | */ |
||
184 | public function validate_password($password) |
||
188 | |||
189 | /** |
||
190 | * Validate that the user's reset hash is the sepcified hash |
||
191 | * |
||
192 | * @param string $hash The user's reset hash |
||
193 | * |
||
194 | * @return boolean true if the user's hash is correct, false otherwise |
||
195 | * |
||
196 | * @SuppressWarnings("UnusedFormalParameter") |
||
197 | */ |
||
198 | public function validate_reset_hash($hash) |
||
202 | |||
203 | /** |
||
204 | * Change the user's password, validating the old password or reset hash |
||
205 | * |
||
206 | * @param string $oldpass The user's original password or reset hash if $isHash is true |
||
207 | * @param string $newpass The user's new password |
||
208 | * @param boolean $isHash Is $old_pass a password or a hash |
||
209 | * |
||
210 | * @return boolean true if the user's password was changed, false otherwise |
||
211 | */ |
||
212 | public function change_pass($oldpass, $newpass, $isHash = false) |
||
228 | |||
229 | /** |
||
230 | * Allow write for the user |
||
231 | */ |
||
232 | protected function enableReadWrite() |
||
242 | |||
243 | /** |
||
244 | * Update the user password if required |
||
245 | */ |
||
246 | private function editUserPassword($data) |
||
263 | |||
264 | private function editNames($data) |
||
287 | |||
288 | private function checkForUnsettableElements($data) |
||
307 | |||
308 | private function editAddressElements($data) |
||
336 | |||
337 | private function editOrganizationElements($data) |
||
355 | |||
356 | /** |
||
357 | * Modify the user given the provided data object |
||
358 | * |
||
359 | * @param stdClass $data The user's new data |
||
360 | * |
||
361 | * @return boolean true if the user's data was changed, false otherwise |
||
362 | */ |
||
363 | public function editUser($data) |
||
384 | |||
385 | /** |
||
386 | * Obtain the user's password reset hash |
||
387 | * |
||
388 | * @return string|false A hash if available, false otherwise |
||
389 | */ |
||
390 | public function getPasswordResetHash() |
||
394 | |||
395 | /** |
||
396 | * Serialize the user data into a format usable by the json_encode method |
||
397 | * |
||
398 | * @return array A simple keyed array representing the user |
||
399 | */ |
||
400 | public function jsonSerialize() |
||
424 | |||
425 | /** |
||
426 | * Serialize the user data into a VCARD 2.1 format |
||
427 | * |
||
428 | * @return string The VCARD for the user |
||
429 | */ |
||
430 | public function getVcard() |
||
446 | } |
||
447 | /* vim: set tabstop=4 shiftwidth=4 expandtab: */ |
||
448 |
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.