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 Sensei_Analysis_Course_List_Table 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 Sensei_Analysis_Course_List_Table, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class Sensei_Analysis_Course_List_Table extends WooThemes_Sensei_List_Table { |
||
12 | public $user_id; |
||
13 | public $course_id; |
||
14 | public $total_lessons; |
||
15 | public $user_ids; |
||
16 | public $view = 'lesson'; |
||
17 | public $page_slug = 'sensei_analysis'; |
||
18 | |||
19 | /** |
||
20 | * Constructor |
||
21 | * @since 1.2.0 |
||
22 | */ |
||
23 | public function __construct ( $course_id = 0, $user_id = 0 ) { |
||
46 | |||
47 | /** |
||
48 | * Define the columns that are going to be used in the table |
||
49 | * @since 1.7.0 |
||
50 | * @return array $columns, the array of columns to use with the table |
||
51 | */ |
||
52 | function get_columns() { |
||
95 | |||
96 | /** |
||
97 | * Define the columns that are going to be used in the table |
||
98 | * @since 1.7.0 |
||
99 | * @return array $columns, the array of columns to use with the table |
||
100 | */ |
||
101 | function get_sortable_columns() { |
||
145 | |||
146 | /** |
||
147 | * Prepare the table with different parameters, pagination, columns and table elements |
||
148 | * @since 1.7.0 |
||
149 | * @return void |
||
150 | */ |
||
151 | public function prepare_items() { |
||
213 | |||
214 | /** |
||
215 | * Generate a csv report with different parameters, pagination, columns and table elements |
||
216 | * @since 1.7.0 |
||
217 | * @return data |
||
218 | */ |
||
219 | public function generate_report( $report ) { |
||
280 | |||
281 | /** |
||
282 | * Generates the overall array for a single item in the display |
||
283 | * |
||
284 | * @since 1.7.0 |
||
285 | * @param object $item The current item |
||
286 | */ |
||
287 | protected function get_row_data( $item ) { |
||
288 | |||
289 | switch( $this->view ) { |
||
290 | case 'user' : |
||
291 | $user_start_date = get_comment_meta( $item->comment_ID, 'start', true ); |
||
292 | $user_end_date = $item->comment_date; |
||
293 | |||
294 | View Code Duplication | if( 'complete' == $item->comment_approved ) { |
|
295 | |||
296 | $status = __( 'Completed', 'woothemes-sensei' ); |
||
297 | $status_class = 'graded'; |
||
298 | |||
299 | } else { |
||
300 | |||
301 | $status = __( 'In Progress', 'woothemes-sensei' ); |
||
302 | $status_class = 'in-progress'; |
||
303 | $user_end_date = ''; |
||
304 | |||
305 | } |
||
306 | $course_percent = get_comment_meta( $item->comment_ID, 'percent', true ); |
||
307 | |||
308 | // Output users data |
||
309 | $user_name = Sensei_Learner::get_full_name( $item->user_id ); |
||
310 | |||
311 | View Code Duplication | if ( !$this->csv_output ) { |
|
312 | |||
313 | $url = add_query_arg( array( 'page' => $this->page_slug, 'user_id' => $item->user_id, 'course_id' => $this->course_id ), admin_url( 'admin.php' ) ); |
||
314 | |||
315 | $user_name = '<strong><a class="row-title" href="' . esc_url( $url ) . '">' . $user_name . '</a></strong>'; |
||
316 | $status = sprintf( '<span class="%s">%s</span>', $status_class, $status ); |
||
317 | if ( is_numeric($course_percent) ) { |
||
318 | |||
319 | $course_percent .= '%'; |
||
320 | |||
321 | } |
||
322 | |||
323 | } // End If Statement |
||
324 | |||
325 | $column_data = apply_filters( 'sensei_analysis_course_column_data', array( 'title' => $user_name, |
||
326 | 'started' => $user_start_date, |
||
327 | 'completed' => $user_end_date, |
||
328 | 'user_status' => $status, |
||
329 | 'percent' => $course_percent, |
||
330 | ), $item, $this ); |
||
331 | break; |
||
332 | |||
333 | case 'lesson': |
||
334 | default: |
||
335 | // Displaying lessons for this Course for a specific User |
||
336 | if ( $this->user_id ) { |
||
337 | $status = __( 'Not started', 'woothemes-sensei' ); |
||
338 | $user_start_date = $user_end_date = $status_class = $grade = ''; |
||
339 | |||
340 | $lesson_args = array( |
||
341 | 'post_id' => $item->ID, |
||
342 | 'user_id' => $this->user_id, |
||
343 | 'type' => 'sensei_lesson_status', |
||
344 | 'status' => 'any', |
||
345 | ); |
||
346 | $lesson_status = Sensei_Utils::sensei_check_for_activity( apply_filters( 'sensei_analysis_course_user_lesson', $lesson_args, $item, $this->user_id ), true ); |
||
347 | |||
348 | if ( !empty($lesson_status) ) { |
||
349 | $user_start_date = get_comment_meta( $lesson_status->comment_ID, 'start', true ); |
||
350 | $user_end_date = $lesson_status->comment_date; |
||
351 | |||
352 | View Code Duplication | if( 'complete' == $lesson_status->comment_approved ) { |
|
353 | $status = __( 'Completed', 'woothemes-sensei' ); |
||
354 | $status_class = 'graded'; |
||
355 | |||
356 | $grade = __( 'No Grade', 'woothemes-sensei' ); |
||
357 | } |
||
358 | elseif( 'graded' == $lesson_status->comment_approved ) { |
||
359 | $status = __( 'Graded', 'woothemes-sensei' ); |
||
360 | $status_class = 'graded'; |
||
361 | |||
362 | $grade = get_comment_meta( $lesson_status->comment_ID, 'grade', true); |
||
363 | } |
||
364 | elseif( 'passed' == $lesson_status->comment_approved ) { |
||
365 | $status = __( 'Passed', 'woothemes-sensei' ); |
||
366 | $status_class = 'graded'; |
||
367 | |||
368 | $grade = get_comment_meta( $lesson_status->comment_ID, 'grade', true); |
||
369 | } |
||
370 | elseif( 'failed' == $lesson_status->comment_approved ) { |
||
371 | $status = __( 'Failed', 'woothemes-sensei' ); |
||
372 | $status_class = 'failed'; |
||
373 | |||
374 | $grade = get_comment_meta( $lesson_status->comment_ID, 'grade', true); |
||
375 | } |
||
376 | elseif( 'ungraded' == $lesson_status->comment_approved ) { |
||
377 | $status = __( 'Ungraded', 'woothemes-sensei' ); |
||
378 | $status_class = 'ungraded'; |
||
379 | |||
380 | } |
||
381 | elseif( 'in-progress' == $lesson_status->comment_approved ) { |
||
382 | $status = __( 'In Progress', 'woothemes-sensei' ); |
||
383 | $user_end_date = ''; |
||
384 | } |
||
385 | } // END lesson_status |
||
386 | |||
387 | // Output users data |
||
388 | if ( $this->csv_output ) { |
||
389 | $lesson_title = apply_filters( 'the_title', $item->post_title, $item->ID ); |
||
390 | } |
||
391 | else { |
||
392 | $url = add_query_arg( array( 'page' => $this->page_slug, 'lesson_id' => $item->ID ), admin_url( 'admin.php' ) ); |
||
393 | $lesson_title = '<strong><a class="row-title" href="' . esc_url( $url ) . '">' . apply_filters( 'the_title', $item->post_title, $item->ID ) . '</a></strong>'; |
||
394 | |||
395 | $status = sprintf( '<span class="%s">%s</span>', $status_class, $status ); |
||
396 | if ( is_numeric($grade) ) { |
||
397 | $grade .= '%'; |
||
398 | } |
||
399 | } // End If Statement |
||
400 | $column_data = apply_filters( 'sensei_analysis_course_column_data', array( 'title' => $lesson_title, |
||
401 | 'started' => $user_start_date, |
||
402 | 'completed' => $user_end_date, |
||
403 | 'user_status' => $status, |
||
404 | 'grade' => $grade, |
||
405 | ), $item, $this ); |
||
406 | } |
||
407 | // Display lessons for this Course regardless of users |
||
408 | else { |
||
409 | // Get Learners (i.e. those who have started) |
||
410 | $lesson_args = array( |
||
411 | 'post_id' => $item->ID, |
||
412 | 'type' => 'sensei_lesson_status', |
||
413 | 'status' => 'any', |
||
414 | ); |
||
415 | $lesson_students = Sensei_Utils::sensei_check_for_activity( apply_filters( 'sensei_analysis_lesson_learners', $lesson_args, $item ) ); |
||
416 | |||
417 | // Get Course Completions |
||
418 | $lesson_args = array( |
||
419 | 'post_id' => $item->ID, |
||
420 | 'type' => 'sensei_lesson_status', |
||
421 | 'status' => array( 'complete', 'graded', 'passed', 'failed' ), |
||
422 | 'count' => true, |
||
423 | ); |
||
424 | $lesson_completions = Sensei_Utils::sensei_check_for_activity( apply_filters( 'sensei_analysis_lesson_completions', $lesson_args, $item ) ); |
||
425 | |||
426 | $lesson_average_grade = __('n/a', 'woothemes-sensei'); |
||
427 | if ( false != get_post_meta($item->ID, '_quiz_has_questions', true) ) { |
||
428 | // Get Percent Complete |
||
429 | $grade_args = array( |
||
430 | 'post_id' => $item->ID, |
||
431 | 'type' => 'sensei_lesson_status', |
||
432 | 'status' => array( 'graded', 'passed', 'failed' ), |
||
433 | 'meta_key' => 'grade', |
||
434 | ); |
||
435 | add_filter( 'comments_clauses', array( 'WooThemes_Sensei_Utils', 'comment_total_sum_meta_value_filter' ) ); |
||
436 | $lesson_grades = Sensei_Utils::sensei_check_for_activity( apply_filters( 'sensei_analysis_lesson_grades', $grade_args, $item ), true ); |
||
437 | remove_filter( 'comments_clauses', array( 'WooThemes_Sensei_Utils', 'comment_total_sum_meta_value_filter' ) ); |
||
438 | |||
439 | $grade_count = !empty( $lesson_grades->total ) ? $lesson_grades->total : 1; |
||
440 | $grade_total = !empty( $lesson_grades->meta_sum ) ? doubleval( $lesson_grades->meta_sum ) : 0; |
||
441 | $lesson_average_grade = abs( round( doubleval( $grade_total / $grade_count ), 2 ) ); |
||
442 | } |
||
443 | // Output lesson data |
||
444 | View Code Duplication | if ( $this->csv_output ) { |
|
445 | $lesson_title = apply_filters( 'the_title', $item->post_title, $item->ID ); |
||
446 | } |
||
447 | else { |
||
448 | $url = add_query_arg( array( 'page' => $this->page_slug, 'lesson_id' => $item->ID ), admin_url( 'admin.php' ) ); |
||
449 | $lesson_title = '<strong><a class="row-title" href="' . esc_url( $url ) . '">' . apply_filters( 'the_title', $item->post_title, $item->ID ) . '</a></strong>'; |
||
450 | |||
451 | if ( is_numeric( $lesson_average_grade ) ) { |
||
452 | $lesson_average_grade .= '%'; |
||
453 | } |
||
454 | } // End If Statement |
||
455 | $column_data = apply_filters( 'sensei_analysis_course_column_data', array( 'title' => $lesson_title, |
||
456 | 'num_learners' => $lesson_students, |
||
457 | 'completions' => $lesson_completions, |
||
458 | 'average_grade' => $lesson_average_grade, |
||
459 | ), $item, $this ); |
||
460 | } // END if |
||
461 | break; |
||
462 | } // END switch |
||
463 | |||
464 | return $column_data; |
||
465 | } |
||
466 | |||
467 | /** |
||
468 | * Return array of course statuses |
||
469 | * @since 1.7.0 |
||
470 | * @return array statuses |
||
471 | */ |
||
472 | View Code Duplication | private function get_course_statuses( $args ) { |
|
516 | |||
517 | /** |
||
518 | * Return array of Courses' lessons |
||
519 | * @since 1.7.0 |
||
520 | * @return array statuses |
||
521 | */ |
||
522 | private function get_lessons( $args ) { |
||
551 | |||
552 | /** |
||
553 | * Sets output when no items are found |
||
554 | * Overloads the parent method |
||
555 | * @since 1.2.0 |
||
556 | * @return void |
||
557 | */ |
||
558 | View Code Duplication | public function no_items() { |
|
571 | |||
572 | /** |
||
573 | * Output for table heading |
||
574 | * @since 1.2.0 |
||
575 | * @return void |
||
576 | */ |
||
577 | public function data_table_header() { |
||
619 | |||
620 | /** |
||
621 | * Output for table footer |
||
622 | * @since 1.2.0 |
||
623 | * @return void |
||
624 | */ |
||
625 | public function data_table_footer() { |
||
641 | |||
642 | /** |
||
643 | * The text for the search button |
||
644 | * @since 1.7.0 |
||
645 | * @return string $text |
||
646 | */ |
||
647 | View Code Duplication | public function search_button( $text = '' ) { |
|
661 | |||
662 | } // End Class |
||
663 | |||
671 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.