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 WSUser 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 WSUser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class WSUser extends WS |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * Enables or disables a user |
||
| 16 | * |
||
| 17 | * @param string User id field name |
||
| 18 | * @param string User id value |
||
| 19 | * @param int Set to 1 to enable and to 0 to disable |
||
| 20 | */ |
||
| 21 | protected function changeUserActiveState( |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Enables or disables multiple users |
||
| 42 | * |
||
| 43 | * @param array Users |
||
| 44 | * @param int Set to 1 to enable and to 0 to disable |
||
| 45 | * @return array Array of results |
||
| 46 | */ |
||
| 47 | protected function changeUsersActiveState($users, $state) |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Disables a user |
||
| 72 | * |
||
| 73 | * @param string API secret key |
||
| 74 | * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id |
||
| 75 | * @param string User id value |
||
| 76 | */ |
||
| 77 | public function DisableUser( |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Disables multiple users |
||
| 100 | * |
||
| 101 | * @param string API secret key |
||
| 102 | * @param array Array of users with elements of the form array('user_id_field_name' => 'name_of_field', 'user_id_value' => 'value') |
||
| 103 | * @return array Array with elements like array('user_id_value' => 'value', 'result' => array('code' => 0, 'message' => 'Operation was successful')). Note that if the result array contains a code different |
||
| 104 | * than 0, an error occured |
||
| 105 | */ |
||
| 106 | View Code Duplication | public function DisableUsers($secret_key, $users) |
|
| 116 | |||
| 117 | /** |
||
| 118 | * Enables a user |
||
| 119 | * |
||
| 120 | * @param string API secret key |
||
| 121 | * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id |
||
| 122 | * @param string User id value |
||
| 123 | */ |
||
| 124 | public function EnableUser($secret_key, $user_id_field_name, $user_id_value) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Enables multiple users |
||
| 143 | * |
||
| 144 | * @param string API secret key |
||
| 145 | * @param array Array of users with elements of the form array('user_id_field_name' => 'name_of_field', 'user_id_value' => 'value') |
||
| 146 | * @return array Array with elements like array('user_id_value' => 'value', 'result' => array('code' => 0, 'message' => 'Operation was successful')). Note that if the result array contains a code different |
||
| 147 | * than 0, an error occured |
||
| 148 | */ |
||
| 149 | View Code Duplication | public function EnableUsers($secret_key, $users) |
|
| 159 | |||
| 160 | /** |
||
| 161 | * Deletes a user (helper method) |
||
| 162 | * |
||
| 163 | * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id |
||
| 164 | * @param string User id value |
||
| 165 | * @return mixed True if user was successfully deleted, WSError otherwise |
||
| 166 | */ |
||
| 167 | protected function deleteUserHelper($user_id_field_name, $user_id_value) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Deletes a user |
||
| 186 | * |
||
| 187 | * @param string API secret key |
||
| 188 | * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id |
||
| 189 | * @param string User id value |
||
| 190 | */ |
||
| 191 | public function DeleteUser($secret_key, $user_id_field_name, $user_id_value) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Deletes multiple users |
||
| 209 | * |
||
| 210 | * @param string API secret key |
||
| 211 | * @param array Array of users with elements of the form array('user_id_field_name' => 'name_of_field', 'user_id_value' => 'value') |
||
| 212 | * @return array Array with elements like array('user_id_value' => 'value', 'result' => array('code' => 0, 'message' => 'Operation was successful')). Note that if the result array contains a code different |
||
| 213 | * than 0, an error occured |
||
| 214 | */ |
||
| 215 | View Code Duplication | public function DeleteUsers($secret_key, $users) |
|
| 241 | |||
| 242 | /** |
||
| 243 | * Creates a user (helper method) |
||
| 244 | * |
||
| 245 | * @param string User first name |
||
| 246 | * @param string User last name |
||
| 247 | * @param int User status |
||
| 248 | * @param string Login name |
||
| 249 | * @param string Password (encrypted or not) |
||
| 250 | * @param string Encrypt method. Leave blank if you are passing the password in clear text, set to the encrypt method used to encrypt the password otherwise. Remember |
||
| 251 | * to include the salt in the extra fields if you are encrypting the password |
||
| 252 | * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id |
||
| 253 | * @param string User id value. Leave blank if you are using the internal user_id |
||
| 254 | * @param int Visibility. |
||
| 255 | * @param string User email. |
||
| 256 | * @param string Language. |
||
| 257 | * @param string Phone. |
||
| 258 | * @param string Expiration date |
||
| 259 | * @param array Extra fields. An array with elements of the form ('field_name' => 'name_of_the_field', 'field_value' => 'value_of_the_field'). |
||
| 260 | * @return mixed New user id generated by the system, WSError otherwise |
||
| 261 | */ |
||
| 262 | protected function createUserHelper( |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Creates a user |
||
| 325 | * |
||
| 326 | * @param string API secret key |
||
| 327 | * @param string User first name |
||
| 328 | * @param string User last name |
||
| 329 | * @param int User status |
||
| 330 | * @param string Login name |
||
| 331 | * @param string Password (encrypted or not) |
||
| 332 | * @param string Encrypt method. Leave blank if you are passing the password in clear text, set to the encrypt method used to encrypt the password otherwise. Remember |
||
| 333 | * to include the salt in the extra fields if you are encrypting the password |
||
| 334 | * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id |
||
| 335 | * @param string User id value. Leave blank if you are using the internal user_id |
||
| 336 | * @param int Visibility. Set by default to 1 |
||
| 337 | * @param string User email. Set by default to an empty string |
||
| 338 | * @param string Language. Set by default to english |
||
| 339 | * @param string Phone. Set by default to an empty string |
||
| 340 | * @param string Expiration date. Set to null by default |
||
| 341 | * @param array Extra fields. An array with elements of the form ('field_name' => 'name_of_the_field', 'field_value' => 'value_of_the_field'). Set to an empty array by default |
||
| 342 | * @return int New user id generated by the system |
||
| 343 | */ |
||
| 344 | public function CreateUser( |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Creates multiple users |
||
| 392 | * |
||
| 393 | * @param string API secret key |
||
| 394 | * @param array Users array. Each member of this array must follow the structure imposed by the CreateUser method |
||
| 395 | * @return array Array with elements of the form array('user_id_value' => 'original value sent', 'user_id_generated' => 'value_generated', 'result' => array('code' => 0, 'message' => 'Operation was successful')) |
||
| 396 | */ |
||
| 397 | public function CreateUsers($secret_key, $users) |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Edits user info (helper method) |
||
| 443 | * |
||
| 444 | * @param string User id field name. Use "chamilo_user_id" in order to use internal system id |
||
| 445 | * @param string User id value |
||
| 446 | * @param string First name |
||
| 447 | * @param string Last name |
||
| 448 | * @param int User status |
||
| 449 | * @param string Login name |
||
| 450 | * @param string Password. Leave blank if you don't want to update it |
||
| 451 | * @param string Encrypt method |
||
| 452 | * @param string User email |
||
| 453 | * @param string Language. Set by default to english |
||
| 454 | * @param string Phone. Set by default to an empty string |
||
| 455 | * @param string Expiration date. Set to null by default |
||
| 456 | * @param array Extra fields. An array with elements of the form ('field_name' => 'name_of_the_field', 'field_value' => 'value_of_the_field'). Leave empty if you don't want to update |
||
| 457 | * @return mixed True if user was successfully updated, WSError otherwise |
||
| 458 | */ |
||
| 459 | protected function editUserHelper( |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Edits user info |
||
| 520 | * |
||
| 521 | * @param string API secret key |
||
| 522 | * @param string User id field name. Use "chamilo_user_id" in order to use internal system id |
||
| 523 | * @param string User id value |
||
| 524 | * @param string First name |
||
| 525 | * @param string Last name |
||
| 526 | * @param int User status |
||
| 527 | * @param string Login name |
||
| 528 | * @param string Password. Leave blank if you don't want to update it |
||
| 529 | * @param string Encrypt method |
||
| 530 | * @param string User email |
||
| 531 | * @param string Language. Set by default to english |
||
| 532 | * @param string Phone. Set by default to an empty string |
||
| 533 | * @param string Expiration date. Set to null by default |
||
| 534 | * @param array Extra fields. An array with elements of the form ('field_name' => 'name_of_the_field', 'field_value' => 'value_of_the_field'). Leave empty if you don't want to update |
||
| 535 | */ |
||
| 536 | public function EditUser( |
||
| 584 | |||
| 585 | /** |
||
| 586 | * Edits multiple users |
||
| 587 | * |
||
| 588 | * @param string API secret key |
||
| 589 | * @param array Users array. Each member of this array must follow the structure imposed by the EditUser method |
||
| 590 | * @return array Array with elements like array('user_id_value' => 'value', 'result' => array('code' => 0, 'message' => 'Operation was successful')). Note that if the result array contains a code different |
||
| 591 | * than 0, an error occured |
||
| 592 | */ |
||
| 593 | public function EditUsers($secret_key, $users) |
||
| 633 | } |
||
| 634 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: