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 iCalendarReader 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 iCalendarReader, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class iCalendarReader { |
||
| 8 | |||
| 9 | public $todo_count = 0; |
||
| 10 | public $event_count = 0; |
||
| 11 | public $cal = array(); |
||
| 12 | public $_lastKeyWord = ''; |
||
| 13 | public $timezone = null; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Class constructor |
||
| 17 | * |
||
| 18 | * @return void |
||
|
|
|||
| 19 | */ |
||
| 20 | public function __construct() {} |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Return an array of events |
||
| 24 | * |
||
| 25 | * @param string $url (default: '') |
||
| 26 | * @return array | false on failure |
||
| 27 | */ |
||
| 28 | public function get_events( $url = '', $count = 5 ) { |
||
| 29 | $count = (int) $count; |
||
| 30 | $transient_id = 'icalendar_vcal_' . md5( $url ) . '_' . $count; |
||
| 31 | |||
| 32 | $vcal = get_transient( $transient_id ); |
||
| 33 | |||
| 34 | if ( ! empty( $vcal ) ) { |
||
| 35 | if ( isset( $vcal['TIMEZONE'] ) ) |
||
| 36 | $this->timezone = $this->timezone_from_string( $vcal['TIMEZONE'] ); |
||
| 37 | |||
| 38 | if ( isset( $vcal['VEVENT'] ) ) { |
||
| 39 | $vevent = $vcal['VEVENT']; |
||
| 40 | |||
| 41 | if ( $count > 0 ) |
||
| 42 | $vevent = array_slice( $vevent, 0, $count ); |
||
| 43 | |||
| 44 | $this->cal['VEVENT'] = $vevent; |
||
| 45 | |||
| 46 | return $this->cal['VEVENT']; |
||
| 47 | } |
||
| 48 | } |
||
| 49 | |||
| 50 | if ( ! $this->parse( $url ) ) |
||
| 51 | return false; |
||
| 52 | |||
| 53 | $vcal = array(); |
||
| 54 | |||
| 55 | if ( $this->timezone ) { |
||
| 56 | $vcal['TIMEZONE'] = $this->timezone->getName(); |
||
| 57 | } else { |
||
| 58 | $this->timezone = $this->timezone_from_string( '' ); |
||
| 59 | } |
||
| 60 | |||
| 61 | if ( ! empty( $this->cal['VEVENT'] ) ) { |
||
| 62 | $vevent = $this->cal['VEVENT']; |
||
| 63 | |||
| 64 | // check for recurring events |
||
| 65 | // $vevent = $this->add_recurring_events( $vevent ); |
||
| 66 | |||
| 67 | // remove before caching - no sense in hanging onto the past |
||
| 68 | $vevent = $this->filter_past_and_recurring_events( $vevent ); |
||
| 69 | |||
| 70 | // order by soonest start date |
||
| 71 | $vevent = $this->sort_by_recent( $vevent ); |
||
| 72 | |||
| 73 | $vcal['VEVENT'] = $vevent; |
||
| 74 | } |
||
| 75 | |||
| 76 | set_transient( $transient_id, $vcal, HOUR_IN_SECONDS ); |
||
| 77 | |||
| 78 | if ( !isset( $vcal['VEVENT'] ) ) |
||
| 79 | return false; |
||
| 80 | |||
| 81 | if ( $count > 0 ) |
||
| 82 | return array_slice( $vcal['VEVENT'], 0, $count ); |
||
| 83 | |||
| 84 | return $vcal['VEVENT']; |
||
| 85 | } |
||
| 86 | |||
| 87 | protected function filter_past_and_recurring_events( $events ) { |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Parse events from an iCalendar feed |
||
| 429 | * |
||
| 430 | * @param string $url (default: '') |
||
| 431 | * @return array | false on failure |
||
| 432 | */ |
||
| 433 | public function parse( $url = '' ) { |
||
| 434 | $cache_group = 'icalendar_reader_parse'; |
||
| 435 | $disable_get_key = 'disable:' . md5( $url ); |
||
| 436 | |||
| 437 | // Check to see if previous attempts have failed |
||
| 438 | if ( false !== wp_cache_get( $disable_get_key, $cache_group ) ) |
||
| 439 | return false; |
||
| 440 | |||
| 441 | // rewrite webcal: URI schem to HTTP |
||
| 442 | $url = preg_replace('/^webcal/', 'http', $url ); |
||
| 443 | // try to fetch |
||
| 444 | $r = wp_remote_get( $url, array( 'timeout' => 3, 'sslverify' => false ) ); |
||
| 445 | if ( 200 !== wp_remote_retrieve_response_code( $r ) ) { |
||
| 446 | // We were unable to fetch any content, so don't try again for another 60 seconds |
||
| 447 | wp_cache_set( $disable_get_key, 1, $cache_group, 60 ); |
||
| 448 | return false; |
||
| 449 | } |
||
| 450 | |||
| 451 | $body = wp_remote_retrieve_body( $r ); |
||
| 452 | if ( empty( $body ) ) |
||
| 453 | return false; |
||
| 454 | |||
| 455 | $body = str_replace( "\r\n", "\n", $body ); |
||
| 456 | $lines = preg_split( "/\n(?=[A-Z])/", $body ); |
||
| 457 | |||
| 458 | if ( empty( $lines ) ) |
||
| 459 | return false; |
||
| 460 | |||
| 461 | if ( false === stristr( $lines[0], 'BEGIN:VCALENDAR' ) ) |
||
| 462 | return false; |
||
| 463 | |||
| 464 | foreach ( $lines as $line ) { |
||
| 465 | $add = $this->key_value_from_string( $line ); |
||
| 466 | if ( ! $add ) { |
||
| 467 | $this->add_component( $type, false, $line ); |
||
| 468 | continue; |
||
| 469 | } |
||
| 470 | list( $keyword, $value ) = $add; |
||
| 471 | |||
| 472 | switch ( $keyword ) { |
||
| 473 | case 'BEGIN': |
||
| 474 | case 'END': |
||
| 475 | switch ( $line ) { |
||
| 476 | case 'BEGIN:VTODO': |
||
| 477 | $this->todo_count++; |
||
| 478 | $type = 'VTODO'; |
||
| 479 | break; |
||
| 480 | case 'BEGIN:VEVENT': |
||
| 481 | $this->event_count++; |
||
| 482 | $type = 'VEVENT'; |
||
| 483 | break; |
||
| 484 | case 'BEGIN:VCALENDAR': |
||
| 485 | case 'BEGIN:DAYLIGHT': |
||
| 486 | case 'BEGIN:VTIMEZONE': |
||
| 487 | case 'BEGIN:STANDARD': |
||
| 488 | $type = $value; |
||
| 489 | break; |
||
| 490 | case 'END:VTODO': |
||
| 491 | case 'END:VEVENT': |
||
| 492 | case 'END:VCALENDAR': |
||
| 493 | case 'END:DAYLIGHT': |
||
| 494 | case 'END:VTIMEZONE': |
||
| 495 | case 'END:STANDARD': |
||
| 496 | $type = 'VCALENDAR'; |
||
| 497 | break; |
||
| 498 | } |
||
| 499 | break; |
||
| 500 | case 'TZID': |
||
| 501 | if ( 'VTIMEZONE' == $type && ! $this->timezone ) |
||
| 502 | $this->timezone = $this->timezone_from_string( $value ); |
||
| 503 | break; |
||
| 504 | case 'X-WR-TIMEZONE': |
||
| 505 | if ( ! $this->timezone ) |
||
| 506 | $this->timezone = $this->timezone_from_string( $value ); |
||
| 507 | break; |
||
| 508 | default: |
||
| 509 | $this->add_component( $type, $keyword, $value ); |
||
| 510 | break; |
||
| 511 | } |
||
| 512 | } |
||
| 513 | |||
| 514 | // Filter for RECURRENCE-IDs |
||
| 515 | $recurrences = array(); |
||
| 516 | if ( array_key_exists( 'VEVENT', $this->cal ) ) { |
||
| 517 | foreach ( $this->cal['VEVENT'] as $event ) { |
||
| 518 | if ( isset( $event['RECURRENCE-ID'] ) ) { |
||
| 519 | $recurrences[] = $event; |
||
| 520 | } |
||
| 521 | } |
||
| 522 | foreach ( $recurrences as $recurrence ) { |
||
| 523 | for ( $i = 0; $i < count( $this->cal['VEVENT'] ); $i++ ) { |
||
| 524 | if ( $this->cal['VEVENT'][ $i ]['UID'] == $recurrence['UID'] && ! isset( $this->cal['VEVENT'][ $i ]['RECURRENCE-ID'] ) ) { |
||
| 525 | $this->cal['VEVENT'][ $i ]['EXDATE'][] = $recurrence['RECURRENCE-ID']; |
||
| 526 | break; |
||
| 527 | } |
||
| 528 | } |
||
| 529 | } |
||
| 530 | } |
||
| 531 | |||
| 532 | return $this->cal; |
||
| 533 | } |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Parse key:value from a string |
||
| 537 | * |
||
| 538 | * @param string $text (default: '') |
||
| 539 | * @return array |
||
| 540 | */ |
||
| 541 | public function key_value_from_string( $text = '' ) { |
||
| 542 | preg_match( '/([^:]+)(;[^:]+)?[:]([\w\W]*)/', $text, $matches ); |
||
| 543 | |||
| 544 | if ( 0 == count( $matches ) ) |
||
| 545 | return false; |
||
| 546 | |||
| 547 | return array( $matches[1], $matches[3] ); |
||
| 548 | } |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Convert a timezone name into a timezone object. |
||
| 552 | * |
||
| 553 | * @param string $text Timezone name. Example: America/Chicago |
||
| 554 | * @return object|null A DateTimeZone object if the conversion was successful. |
||
| 555 | */ |
||
| 556 | private function timezone_from_string( $text ) { |
||
| 557 | try { |
||
| 558 | $timezone = new DateTimeZone( $text ); |
||
| 559 | } catch ( Exception $e ) { |
||
| 560 | $blog_timezone = get_option( 'timezone_string' ); |
||
| 561 | if ( ! $blog_timezone ) { |
||
| 562 | $blog_timezone = 'Etc/UTC'; |
||
| 563 | } |
||
| 564 | |||
| 565 | $timezone = new DateTimeZone( $blog_timezone ); |
||
| 566 | } |
||
| 567 | |||
| 568 | return $timezone; |
||
| 569 | } |
||
| 570 | |||
| 571 | /** |
||
| 572 | * Add a component to the calendar array |
||
| 573 | * |
||
| 574 | * @param string $component (default: '') |
||
| 575 | * @param string $keyword (default: '') |
||
| 576 | * @param string $value (default: '') |
||
| 577 | * @return void |
||
| 578 | */ |
||
| 579 | public function add_component( $component = '', $keyword = '', $value = '' ) { |
||
| 580 | if ( false == $keyword ) { |
||
| 581 | $keyword = $this->last_keyword; |
||
| 582 | switch ( $component ) { |
||
| 583 | case 'VEVENT': |
||
| 584 | $value = $this->cal[ $component ][ $this->event_count - 1 ][ $keyword ] . $value; |
||
| 585 | break; |
||
| 586 | case 'VTODO' : |
||
| 587 | $value = $this->cal[ $component ][ $this->todo_count - 1 ][ $keyword ] . $value; |
||
| 588 | break; |
||
| 589 | } |
||
| 590 | } |
||
| 591 | |||
| 592 | /* |
||
| 593 | * Some events have a specific timezone set in their start/end date, |
||
| 594 | * and it may or may not be different than the calendar timzeone. |
||
| 595 | * Valid formats include: |
||
| 596 | * DTSTART;TZID=Pacific Standard Time:20141219T180000 |
||
| 597 | * DTEND;TZID=Pacific Standard Time:20141219T200000 |
||
| 598 | * EXDATE:19960402T010000Z,19960403T010000Z,19960404T010000Z |
||
| 599 | * EXDATE;VALUE=DATE:2015050 |
||
| 600 | * EXDATE;TZID=America/New_York:20150424T170000 |
||
| 601 | * EXDATE;TZID=Pacific Standard Time:20120615T140000,20120629T140000,20120706T140000 |
||
| 602 | */ |
||
| 603 | |||
| 604 | // Always store EXDATE as an array |
||
| 605 | if ( stristr( $keyword, 'EXDATE' ) ) { |
||
| 606 | $value = explode( ',', $value ); |
||
| 607 | } |
||
| 608 | |||
| 609 | // Adjust DTSTART, DTEND, and EXDATE according to their TZID if set |
||
| 610 | if ( strpos( $keyword, ';' ) && ( stristr( $keyword, 'DTSTART' ) || stristr( $keyword, 'DTEND' ) || stristr( $keyword, 'EXDATE' ) || stristr( $keyword, 'RECURRENCE-ID' ) ) ) { |
||
| 611 | $keyword = explode( ';', $keyword ); |
||
| 612 | |||
| 613 | $tzid = false; |
||
| 614 | if ( 2 == count( $keyword ) ) { |
||
| 615 | $tparam = $keyword[1]; |
||
| 616 | |||
| 617 | if ( strpos( $tparam, "TZID" ) !== false ) { |
||
| 618 | $tzid = $this->timezone_from_string( str_replace( 'TZID=', '', $tparam ) ); |
||
| 619 | } |
||
| 620 | } |
||
| 621 | |||
| 622 | // Normalize all times to default UTC |
||
| 623 | if ( $tzid ) { |
||
| 624 | $adjusted_times = array(); |
||
| 625 | foreach ( (array) $value as $v ) { |
||
| 626 | try { |
||
| 627 | $adjusted_time = new DateTime( $v, $tzid ); |
||
| 628 | $adjusted_time->setTimeZone( new DateTimeZone( 'UTC' ) ); |
||
| 629 | $adjusted_times[] = $adjusted_time->format('Ymd\THis'); |
||
| 630 | } catch ( Exception $e ) { |
||
| 631 | // Invalid argument to DateTime |
||
| 632 | return; |
||
| 633 | } |
||
| 634 | } |
||
| 635 | $value = $adjusted_times; |
||
| 636 | } |
||
| 637 | |||
| 638 | // Format for adding to event |
||
| 639 | $keyword = $keyword[0]; |
||
| 640 | if ( 'EXDATE' != $keyword ) { |
||
| 641 | $value = implode( (array) $value ); |
||
| 642 | } |
||
| 643 | } |
||
| 644 | |||
| 645 | foreach ( (array) $value as $v ) { |
||
| 646 | switch ($component) { |
||
| 647 | View Code Duplication | case 'VTODO': |
|
| 648 | if ( 'EXDATE' == $keyword ) { |
||
| 649 | $this->cal[ $component ][ $this->todo_count - 1 ][ $keyword ][] = $v; |
||
| 650 | } else { |
||
| 651 | $this->cal[ $component ][ $this->todo_count - 1 ][ $keyword ] = $v; |
||
| 652 | } |
||
| 653 | break; |
||
| 654 | View Code Duplication | case 'VEVENT': |
|
| 655 | if ( 'EXDATE' == $keyword ) { |
||
| 656 | $this->cal[ $component ][ $this->event_count - 1 ][ $keyword ][] = $v; |
||
| 657 | } else { |
||
| 658 | $this->cal[ $component ][ $this->event_count - 1 ][ $keyword ] = $v; |
||
| 659 | } |
||
| 660 | break; |
||
| 661 | default: |
||
| 662 | $this->cal[ $component ][ $keyword ] = $v; |
||
| 663 | break; |
||
| 664 | } |
||
| 665 | } |
||
| 666 | $this->last_keyword = $keyword; |
||
| 667 | } |
||
| 668 | |||
| 669 | /** |
||
| 670 | * Escape strings with wp_kses, allow links |
||
| 671 | * |
||
| 672 | * @param string $string (default: '') |
||
| 673 | * @return string |
||
| 674 | */ |
||
| 675 | public function escape( $string = '' ) { |
||
| 697 | |||
| 698 | /** |
||
| 699 | * Render the events |
||
| 700 | * |
||
| 701 | * @param string $url (default: '') |
||
| 702 | * @param string $context (default: 'widget') or 'shortcode' |
||
| 703 | * @return mixed bool|string false on failure, rendered HTML string on success. |
||
| 704 | */ |
||
| 705 | public function render( $url = '', $args = array() ) { |
||
| 706 | |||
| 707 | $args = wp_parse_args( $args, array( |
||
| 708 | 'context' => 'widget', |
||
| 709 | 'number' => 5 |
||
| 710 | ) ); |
||
| 711 | |||
| 712 | $events = $this->get_events( $url, $args['number'] ); |
||
| 713 | |||
| 714 | if ( empty( $events ) ) |
||
| 715 | return false; |
||
| 716 | |||
| 717 | ob_start(); |
||
| 718 | |||
| 719 | if ( 'widget' == $args['context'] ) : ?> |
||
| 720 | <ul class="upcoming-events"> |
||
| 721 | <?php foreach ( $events as $event ) : ?> |
||
| 722 | <li> |
||
| 723 | <strong class="event-summary"><?php echo $this->escape( stripslashes( $event['SUMMARY'] ) ); ?></strong> |
||
| 724 | <span class="event-when"><?php echo $this->formatted_date( $event ); ?></span> |
||
| 725 | <?php if ( ! empty( $event['LOCATION'] ) ) : ?> |
||
| 726 | <span class="event-location"><?php echo $this->escape( stripslashes( $event['LOCATION'] ) ); ?></span> |
||
| 727 | <?php endif; ?> |
||
| 728 | <?php if ( ! empty( $event['DESCRIPTION'] ) ) : ?> |
||
| 729 | <span class="event-description"><?php echo wp_trim_words( $this->escape( stripcslashes( $event['DESCRIPTION'] ) ) ); ?></span> |
||
| 730 | <?php endif; ?> |
||
| 731 | </li> |
||
| 732 | <?php endforeach; ?> |
||
| 733 | </ul> |
||
| 734 | <?php endif; |
||
| 735 | |||
| 736 | if ( 'shortcode' == $args['context'] ) : ?> |
||
| 737 | <table class="upcoming-events"> |
||
| 738 | <thead> |
||
| 739 | <tr> |
||
| 740 | <th>Location</th> |
||
| 741 | <th>When</th> |
||
| 742 | <th>Summary</th> |
||
| 743 | <th>Description</th> |
||
| 744 | </tr> |
||
| 745 | </thead> |
||
| 746 | <tbody> |
||
| 747 | <?php foreach ( $events as $event ) : ?> |
||
| 748 | <tr> |
||
| 749 | <td><?php echo $this->escape( stripslashes( $event['LOCATION'] ) ); ?></td> |
||
| 750 | <td><?php echo $this->formatted_date( $event ); ?></td> |
||
| 751 | <td><?php echo $this->escape( stripslashes( $event['SUMMARY'] ) ); ?></td> |
||
| 752 | <td><?php echo wp_trim_words( $this->escape( stripcslashes( $event['DESCRIPTION'] ) ) ); ?></td> |
||
| 753 | </tr> |
||
| 754 | <?php endforeach; ?> |
||
| 755 | </tbody> |
||
| 756 | </table> |
||
| 757 | <?php endif; |
||
| 758 | |||
| 759 | $rendered = ob_get_clean(); |
||
| 760 | |||
| 761 | if ( empty( $rendered ) ) |
||
| 762 | return false; |
||
| 763 | |||
| 764 | return $rendered; |
||
| 765 | } |
||
| 766 | |||
| 767 | public function formatted_date( $event ) { |
||
| 768 | |||
| 769 | $date_format = get_option( 'date_format' ); |
||
| 770 | $time_format = get_option( 'time_format' ); |
||
| 771 | $start = strtotime( $event['DTSTART'] ); |
||
| 772 | $end = isset( $event['DTEND'] ) ? strtotime( $event['DTEND'] ) : false; |
||
| 773 | |||
| 774 | $all_day = ( 8 == strlen( $event['DTSTART'] ) ); |
||
| 775 | |||
| 776 | if ( !$all_day && $this->timezone ) { |
||
| 777 | try { |
||
| 778 | $start_time = new DateTime( $event['DTSTART'] ); |
||
| 779 | $timezone_offset = $this->timezone->getOffset( $start_time ); |
||
| 780 | $start += $timezone_offset; |
||
| 781 | |||
| 782 | if ( $end ) { |
||
| 783 | $end += $timezone_offset; |
||
| 784 | } |
||
| 785 | } catch ( Exception $e ) { |
||
| 786 | // Invalid argument to DateTime |
||
| 787 | } |
||
| 788 | } |
||
| 789 | $single_day = $end ? ( $end - $start ) <= DAY_IN_SECONDS : true; |
||
| 790 | |||
| 791 | /* Translators: Date and time */ |
||
| 792 | $date_with_time = __( '%1$s at %2$s' , 'jetpack' ); |
||
| 793 | /* Translators: Two dates with a separator */ |
||
| 794 | $two_dates = __( '%1$s – %2$s' , 'jetpack' ); |
||
| 795 | |||
| 796 | // we'll always have the start date. Maybe with time |
||
| 797 | View Code Duplication | if ( $all_day ) |
|
| 798 | $date = date_i18n( $date_format, $start ); |
||
| 799 | else |
||
| 800 | $date = sprintf( $date_with_time, date_i18n( $date_format, $start ), date_i18n( $time_format, $start ) ); |
||
| 801 | |||
| 802 | // single day, timed |
||
| 803 | if ( $single_day && ! $all_day && false !== $end ) |
||
| 804 | $date = sprintf( $two_dates, $date, date_i18n( $time_format, $end ) ); |
||
| 805 | |||
| 806 | // multi-day |
||
| 807 | if ( ! $single_day ) { |
||
| 808 | |||
| 809 | View Code Duplication | if ( $all_day ) { |
|
| 810 | // DTEND for multi-day events represents "until", not "including", so subtract one minute |
||
| 811 | $end_date = date_i18n( $date_format, $end - 60 ); |
||
| 812 | } else { |
||
| 813 | $end_date = sprintf( $date_with_time, date_i18n( $date_format, $end ), date_i18n( $time_format, $end ) ); |
||
| 814 | } |
||
| 815 | |||
| 816 | $date = sprintf( $two_dates, $date, $end_date ); |
||
| 817 | |||
| 818 | } |
||
| 819 | |||
| 820 | return $date; |
||
| 821 | } |
||
| 822 | |||
| 823 | protected function sort_by_recent( $list ) { |
||
| 840 | |||
| 841 | } |
||
| 842 | |||
| 843 | |||
| 844 | /** |
||
| 845 | * Wrapper function for iCalendarReader->get_events() |
||
| 846 | * |
||
| 847 | * @param string $url (default: '') |
||
| 867 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.