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 WSCMCourse 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 WSCMCourse, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class WSCMCourse extends WSCM |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * Deletes a course (helper method) |
||
| 18 | * |
||
| 19 | * @param string Course id field name |
||
| 20 | * @param string Course id value |
||
| 21 | * @return mixed True if the course was successfully deleted, WSError otherwise |
||
| 22 | */ |
||
| 23 | View Code Duplication | protected function deleteCourseHelper( |
|
| 42 | |||
| 43 | /** |
||
| 44 | * Deletes a course |
||
| 45 | * |
||
| 46 | * @param string API secret key |
||
| 47 | * @param string Course id field name |
||
| 48 | * @param string Course id value |
||
| 49 | */ |
||
| 50 | public function DeleteCourse( |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Deletes multiple courses |
||
| 71 | * |
||
| 72 | * @param string API secret key |
||
| 73 | * @param array Array of courses with elements of the form array('course_id_field_name' => 'name_of_field', 'course_id_value' => 'value') |
||
| 74 | * @return array Array with elements like array('course_id_value' => 'value', 'result' => array('code' => 0, 'message' => 'Operation was successful')). Note that if the result array contains a code different |
||
| 75 | * than 0, an error occured |
||
| 76 | */ |
||
| 77 | View Code Duplication | public function DeleteCourses($secret_key, $courses) |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Creates a course (helper method) |
||
| 102 | * |
||
| 103 | * @param string Title |
||
| 104 | * @param string Category code |
||
| 105 | * @param string Wanted code. If it's not defined, it will be generated automatically |
||
| 106 | * @param string Tutor name |
||
| 107 | * @param string Course admin user id field name |
||
| 108 | * @param string Course admin user id value |
||
| 109 | * @param string Course language |
||
| 110 | * @param string Course id field name |
||
| 111 | * @param string Course id value |
||
| 112 | * @param array Course extra fields |
||
| 113 | * @return mixed Generated id if creation was successful, WSError otherwise |
||
| 114 | */ |
||
| 115 | View Code Duplication | protected function createCourseHelper( |
|
| 155 | |||
| 156 | /** |
||
| 157 | * Creates a course |
||
| 158 | * |
||
| 159 | * @param string API secret key |
||
| 160 | * @param string Title |
||
| 161 | * @param string Category code |
||
| 162 | * @param string Wanted code. If it's not defined, it will be generated automatically |
||
| 163 | * @param string Tutor name |
||
| 164 | * @param string Course admin user id field name |
||
| 165 | * @param string Course admin user id value |
||
| 166 | * @param string Course language |
||
| 167 | * @param string Course id field name |
||
| 168 | * @param string Course id value |
||
| 169 | * @param array Course extra fields |
||
| 170 | * @return int Course id generated |
||
| 171 | */ |
||
| 172 | public function CreateCourse( |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Create multiple courses |
||
| 201 | * |
||
| 202 | * @param string API secret key |
||
| 203 | * @param array Courses to be created, with elements following the structure presented in CreateCourse |
||
| 204 | * @return array Array with elements of the form array('course_id_value' => 'original value sent', 'course_id_generated' => 'value_generated', 'result' => array('code' => 0, 'message' => 'Operation was successful')) |
||
| 205 | */ |
||
| 206 | View Code Duplication | public function CreateCourses($secret_key, $courses) |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Edits a course (helper method) |
||
| 237 | * |
||
| 238 | * @param string Course id field name |
||
| 239 | * @param string Course id value |
||
| 240 | * @param string Title |
||
| 241 | * @param string Category code |
||
| 242 | * @param string Department name |
||
| 243 | * @param string Department url |
||
| 244 | * @param string Course language |
||
| 245 | * @param int Visibility |
||
| 246 | * @param int Subscribe (0 = denied, 1 = allowed) |
||
| 247 | * @param int Unsubscribe (0 = denied, 1 = allowed) |
||
| 248 | * @param string Visual code |
||
| 249 | * @param array Course extra fields |
||
| 250 | * @return mixed True in case of success, WSError otherwise |
||
| 251 | */ |
||
| 252 | protected function editCourseHelper( |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Edits a course |
||
| 317 | * |
||
| 318 | * @param string API secret key |
||
| 319 | * @param string Course id field name |
||
| 320 | * @param string Course id value |
||
| 321 | * @param string Title |
||
| 322 | * @param string Category code |
||
| 323 | * @param string Department name |
||
| 324 | * @param string Department url |
||
| 325 | * @param string Course language |
||
| 326 | * @param int Visibility |
||
| 327 | * @param int Subscribe (0 = denied, 1 = allowed) |
||
| 328 | * @param int Unsubscribe (0 = denied, 1 = allowed) |
||
| 329 | * @param string Visual code |
||
| 330 | * @param array Course extra fields |
||
| 331 | */ |
||
| 332 | public function EditCourse( |
||
| 357 | |||
| 358 | /** |
||
| 359 | * List courses |
||
| 360 | * |
||
| 361 | * @param string API secret key |
||
| 362 | * @param string Course id field name. Use "chamilo_course_id" to use internal id |
||
| 363 | * @return array An array with elements of the form ('id' => 'Course internal id', 'code' => 'Course code', 'title' => 'Course title', 'language' => 'Course language', 'visibility' => 'Course visibility', |
||
| 364 | * 'category_name' => 'Name of the category of the course', 'number_students' => 'Number of students in the course', 'external_course_id' => 'External course id') |
||
| 365 | */ |
||
| 366 | public function ListCourses($secret_key, $course_id_field_name) |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Subscribe or unsubscribe user to a course (helper method) |
||
| 408 | * |
||
| 409 | * @param string Course id field name. Use "chamilo_course_id" to use internal id |
||
| 410 | * @param string Course id value. |
||
| 411 | * @param string User id field name. Use "chamilo_user_id" to use internal id |
||
| 412 | * @param string User id value |
||
| 413 | * @param int Set to 1 to subscribe, 0 to unsubscribe |
||
| 414 | * @param int Status (STUDENT or TEACHER) Used for subscription only |
||
| 415 | * @return mixed True if subscription or unsubscription was successful, false otherwise |
||
| 416 | */ |
||
| 417 | View Code Duplication | protected function changeUserSubscription($course_id_field_name, $course_id_value, $user_id_field_name, $user_id_value, $state, $status = STUDENT) { |
|
| 442 | |||
| 443 | /** |
||
| 444 | * Subscribe user to a course |
||
| 445 | * |
||
| 446 | * @param string API secret key |
||
| 447 | * @param string Course id field name. Use "chamilo_course_id" to use internal id |
||
| 448 | * @param string Course id value. |
||
| 449 | * @param string User id field name. Use "chamilo_user_id" to use internal id |
||
| 450 | * @param string User id value |
||
| 451 | * @param int Status (1 = Teacher, 5 = Student) |
||
| 452 | */ |
||
| 453 | public function SubscribeUserToCourse($secret_key, $course_id_field_name, $course_id_value, $user_id_field_name, $user_id_value, $status) { |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Unsusbscribe user from course |
||
| 467 | * |
||
| 468 | * @param string API secret key |
||
| 469 | * @param string Course id field name. Use "chamilo_course_id" to use internal id |
||
| 470 | * @param string Course id value. |
||
| 471 | * @param string User id field name. Use "chamilo_user_id" to use internal id |
||
| 472 | * @param string User id value |
||
| 473 | */ |
||
| 474 | public function UnsubscribeUserFromCourse($secret_key, $course_id_field_name, $course_id_value, $user_id_field_name, $user_id_value) { |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Returns the descriptions of a course, along with their id |
||
| 488 | * |
||
| 489 | * @param string API secret key |
||
| 490 | * @param string Course id field name |
||
| 491 | * @param string Course id value |
||
| 492 | * @return array Returns an array with elements of the form ('course_desc_id' => 1, 'course_desc_title' => 'Title', 'course_desc_content' => 'Content') |
||
| 493 | */ |
||
| 494 | View Code Duplication | public function GetCourseDescriptions($secret_key, $course_id_field_name, $course_id_value) { |
|
| 515 | |||
| 516 | |||
| 517 | /** |
||
| 518 | * Edit course description |
||
| 519 | * |
||
| 520 | * @param string API secret key |
||
| 521 | * @param string Course id field name |
||
| 522 | * @param string Course id value |
||
| 523 | * @param int Category id from course description |
||
| 524 | * @param string Description title |
||
| 525 | * @param string Course description content |
||
| 526 | */ |
||
| 527 | View Code Duplication | public function EditCourseDescription($secret_key, $course_id_field_name, $course_id_value, $course_desc_id, $course_desc_title, $course_desc_content) { |
|
| 581 | |||
| 582 | public function get_message_data($username, $password) |
||
| 591 | |||
| 592 | public function nada($username, $password) |
||
| 598 | |||
| 599 | |||
| 600 | |||
| 601 | } |
||
| 602 | |||
| 603 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.