| Conditions | 12 |
| Paths | 6 |
| Total Lines | 60 |
| Code Lines | 27 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 1 | Features | 2 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 111 | public function get_events() { |
||
| 112 | |||
| 113 | $ids = $this->calendars_ids; |
||
| 114 | $events = get_transient( '_simple-calendar_feed_id_' . strval( $this->post_id ) . '_' . $this->type ); |
||
| 115 | |||
| 116 | if ( empty( $events ) && ! empty( $ids ) && is_array( $ids ) ) { |
||
| 117 | |||
| 118 | $events = array(); |
||
| 119 | |||
| 120 | foreach ( $ids as $cal_id ) { |
||
| 121 | |||
| 122 | $calendar = simcal_get_calendar( intval( $cal_id ) ); |
||
| 123 | |||
| 124 | simcal_delete_feed_transients( $cal_id ); |
||
| 125 | |||
| 126 | if ( $calendar instanceof Calendar ) { |
||
| 127 | |||
| 128 | // Sometimes the calendars might have events at the same time from different calendars |
||
| 129 | // When merging the arrays together some of the events will be lost because the keys are the same and one will overwrite the other |
||
| 130 | // This snippet checks if the key already exists in the master events array and if it does it subtracts 1 from it to make the key unique and then unsets the original key. |
||
| 131 | foreach( $calendar->events as $k => $v ) { |
||
| 132 | if ( array_key_exists( $k, $events ) ) { |
||
| 133 | $calendar->events[ $k - 1 ] = $v; |
||
| 134 | unset( $k ); |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | $events = is_array( $calendar->events ) ? $events + $calendar->events : $events; |
||
| 139 | } |
||
| 140 | |||
| 141 | } |
||
| 142 | |||
| 143 | if ( ! empty( $events ) ) { |
||
| 144 | |||
| 145 | // Trim events to set the earliest one as specified in feed settings. |
||
| 146 | $earliest_event = intval( $this->time_min ); |
||
| 147 | if ( $earliest_event > 0 ) { |
||
| 148 | $events = $this->array_filter_key( $events, array( $this, 'filter_events_before' ) ); |
||
| 149 | } |
||
| 150 | |||
| 151 | // Trim events to set the latest one as specified in feed settings. |
||
| 152 | $latest_event = intval( $this->time_max ); |
||
| 153 | if ( $latest_event > 0 ) { |
||
| 154 | $events = $this->array_filter_key( $events, array( $this, 'filter_events_after' ) ); |
||
| 155 | } |
||
| 156 | |||
| 157 | set_transient( |
||
| 158 | '_simple-calendar_feed_id_' . strval( $this->post_id ) . '_' . $this->type, |
||
| 159 | $events, |
||
| 160 | absint( $this->cache ) |
||
| 161 | ); |
||
| 162 | } |
||
| 163 | |||
| 164 | } |
||
| 165 | |||
| 166 | // Sort events by start time before returning |
||
| 167 | uasort( $events, array( $this, 'sort_by_start_time' ) ); |
||
| 168 | |||
| 169 | return $events; |
||
| 170 | } |
||
| 171 | |||
| 241 |