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 | * Set the user's display name |
||
| 231 | * |
||
| 232 | * @param string $name The user's new display name |
||
| 233 | * |
||
| 234 | * @return boolean true if the user's display name was changed, false otherwise |
||
| 235 | */ |
||
| 236 | public function setDisplayName($name) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Set the user's given (first) name |
||
| 243 | * |
||
| 244 | * @param string $name The user's new given name |
||
| 245 | * |
||
| 246 | * @return boolean true if the user's given name was changed, false otherwise |
||
| 247 | */ |
||
| 248 | public function setGivenName($name) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Set the user's email address |
||
| 255 | * |
||
| 256 | * @param string $email The user's new email address |
||
| 257 | * |
||
| 258 | * @return boolean true if the user's email address was changed, false otherwise |
||
| 259 | * |
||
| 260 | * @SuppressWarnings("UnusedFormalParameter") |
||
| 261 | */ |
||
| 262 | public function setEmail($email) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Set the user's user ID or user name |
||
| 269 | * |
||
| 270 | * @param string $uid The user's new user ID |
||
| 271 | * |
||
| 272 | * @return boolean true if the user's ID was changed, false otherwise |
||
| 273 | * |
||
| 274 | * @SuppressWarnings("UnusedFormalParameter") |
||
| 275 | */ |
||
| 276 | public function setUid($uid) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Set the user's photo |
||
| 283 | * |
||
| 284 | * @param string $photo The user's new photo as a binary string |
||
| 285 | * |
||
| 286 | * @return boolean true if the user's photo was changed, false otherwise |
||
| 287 | * |
||
| 288 | * @SuppressWarnings("UnusedFormalParameter") |
||
| 289 | */ |
||
| 290 | public function setPhoto($photo) |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Set the user's phone number |
||
| 297 | * |
||
| 298 | * @param string $phone The user's new phonew number |
||
| 299 | * |
||
| 300 | * @return boolean true if the user's phone number was changed, false otherwise |
||
| 301 | * |
||
| 302 | * @SuppressWarnings("UnusedFormalParameter") |
||
| 303 | */ |
||
| 304 | public function setPhoneNumber($phone) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Set the user's organization |
||
| 311 | * |
||
| 312 | * @param string $org The user's new organization |
||
| 313 | * |
||
| 314 | * @return boolean true if the user's organization was changed, false otherwise |
||
| 315 | * |
||
| 316 | * @SuppressWarnings("UnusedFormalParameter") |
||
| 317 | */ |
||
| 318 | public function setOrganization($org) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Set the user's titles |
||
| 325 | * |
||
| 326 | * @param string $titles The user's new titles |
||
| 327 | * |
||
| 328 | * @return boolean true if the user's titles were changed, false otherwise |
||
| 329 | * |
||
| 330 | * @SuppressWarnings("UnusedFormalParameter") |
||
| 331 | */ |
||
| 332 | public function setTitles($titles) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Set the user's state |
||
| 339 | * |
||
| 340 | * @param string $state The user's new state |
||
| 341 | * |
||
| 342 | * @return boolean true if the user's state was changed, false otherwise |
||
| 343 | * |
||
| 344 | * @SuppressWarnings("UnusedFormalParameter") |
||
| 345 | */ |
||
| 346 | public function setState($state) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Set the user's city |
||
| 353 | * |
||
| 354 | * @param string $city The user's new city |
||
| 355 | * |
||
| 356 | * @return boolean true if the user's city was changed, false otherwise |
||
| 357 | * |
||
| 358 | * @SuppressWarnings("UnusedFormalParameter") |
||
| 359 | */ |
||
| 360 | public function setCity($city) |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Set the user's last name |
||
| 367 | * |
||
| 368 | * @param string $sn The user's new last name |
||
| 369 | * |
||
| 370 | * @return boolean true if the user's last name was changed, false otherwise |
||
| 371 | * |
||
| 372 | * @SuppressWarnings("UnusedFormalParameter") |
||
| 373 | */ |
||
| 374 | public function setLastName($sn) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Set the user's nick name |
||
| 381 | * |
||
| 382 | * @param string $displayName The user's new nick name |
||
| 383 | * |
||
| 384 | * @return boolean true if the user's nick name was changed, false otherwise |
||
| 385 | */ |
||
| 386 | public function setNickName($displayName) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Set the user's mailing address |
||
| 393 | * |
||
| 394 | * @param string $address The user's new mailing address |
||
| 395 | * |
||
| 396 | * @return boolean true if the user's mailing address was changed, false otherwise |
||
| 397 | * |
||
| 398 | * @SuppressWarnings("UnusedFormalParameter") |
||
| 399 | */ |
||
| 400 | public function setAddress($address) |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Set the user's postal or zip code |
||
| 407 | * |
||
| 408 | * @param string $postalcode The user's new postal code |
||
| 409 | * |
||
| 410 | * @return boolean true if the user's postal code was changed, false otherwise |
||
| 411 | * |
||
| 412 | * @SuppressWarnings("UnusedFormalParameter") |
||
| 413 | */ |
||
| 414 | public function setPostalCode($postalcode) |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Set the user's country |
||
| 421 | * |
||
| 422 | * @param string $country The user's new country |
||
| 423 | * |
||
| 424 | * @return boolean true if the user's country was changed, false otherwise |
||
| 425 | * |
||
| 426 | * @SuppressWarnings("UnusedFormalParameter") |
||
| 427 | */ |
||
| 428 | public function setCountry($country) |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Set the user's organizations |
||
| 435 | * |
||
| 436 | * @param string $ous The user's new organizations |
||
| 437 | * |
||
| 438 | * @return boolean true if the user's organizations was changed, false otherwise |
||
| 439 | * |
||
| 440 | * @SuppressWarnings("UnusedFormalParameter") |
||
| 441 | */ |
||
| 442 | public function setOrganizationUnits($ous) |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Allow write for the user |
||
| 449 | */ |
||
| 450 | protected function enableReadWrite() |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Update the user password if required |
||
| 463 | */ |
||
| 464 | private function editUserPassword($data) |
||
| 481 | |||
| 482 | private function editNames($data) |
||
| 505 | |||
| 506 | private function checkForUnsettableElements($data) |
||
| 525 | |||
| 526 | private function editAddressElements($data) |
||
| 554 | |||
| 555 | private function editOrganizationElements($data) |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Modify the user given the provided data object |
||
| 576 | * |
||
| 577 | * @param stdClass $data The user's new data |
||
| 578 | * |
||
| 579 | * @return boolean true if the user's data was changed, false otherwise |
||
| 580 | */ |
||
| 581 | public function editUser($data) |
||
| 602 | |||
| 603 | /** |
||
| 604 | * Obtain the user's password reset hash |
||
| 605 | * |
||
| 606 | * @return string|false A hash if available, false otherwise |
||
| 607 | */ |
||
| 608 | public function getPasswordResetHash() |
||
| 612 | |||
| 613 | /** |
||
| 614 | * Serialize the user data into a format usable by the json_encode method |
||
| 615 | * |
||
| 616 | * @return array A simple keyed array representing the user |
||
| 617 | */ |
||
| 618 | public function jsonSerialize() |
||
| 642 | |||
| 643 | /** |
||
| 644 | * Serialize the user data into a VCARD 2.1 format |
||
| 645 | * |
||
| 646 | * @return string The VCARD for the user |
||
| 647 | */ |
||
| 648 | public function getVcard() |
||
| 664 | } |
||
| 665 | /* vim: set tabstop=4 shiftwidth=4 expandtab: */ |
||
| 666 |
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@propertyannotation 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.