| Conditions | 36 |
| Paths | 338 |
| Total Lines | 144 |
| Code Lines | 95 |
| Lines | 15 |
| Ratio | 10.42 % |
| Changes | 0 | ||
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 |
||
| 225 | public function process_recurrences() |
||
| 226 | { |
||
| 227 | $array = $this->cal; |
||
| 228 | $events = $array['VEVENT']; |
||
| 229 | foreach ($array['VEVENT'] as $anEvent) { |
||
| 230 | if (isset($anEvent['RRULE']) && $anEvent['RRULE'] != '') { |
||
| 231 | // Recurring event, parse RRULE and add appropriate duplicate events |
||
| 232 | $rrules = array(); |
||
| 233 | $rrule_strings = explode(';',$anEvent['RRULE']); |
||
| 234 | foreach ($rrule_strings as $s) { |
||
| 235 | list($k,$v) = explode('=', $s); |
||
| 236 | $rrules[$k] = $v; |
||
| 237 | } |
||
| 238 | // Get Start timestamp |
||
| 239 | $start_timestamp = $this->iCalDateToUnixTimestamp($anEvent['DTSTART']); |
||
| 240 | $end_timestamp = $this->iCalDateToUnixTimestamp($anEvent['DTEND']); |
||
| 241 | $event_timestmap_offset = $end_timestamp - $start_timestamp; |
||
| 242 | // Get Interval |
||
| 243 | $interval = (isset($rrules['INTERVAL']) && $rrules['INTERVAL'] != '') ? $rrules['INTERVAL'] : 1; |
||
| 244 | // Get Until |
||
| 245 | $until = $this->iCalDateToUnixTimestamp($rrules['UNTIL']); |
||
| 246 | // Decide how often to add events and do so |
||
| 247 | switch ($rrules['FREQ']) { |
||
| 248 | case 'DAILY': |
||
| 249 | // Simply add a new event each interval of days until UNTIL is reached |
||
| 250 | $offset = "+$interval day"; |
||
| 251 | $recurring_timestamp = strtotime($offset, $start_timestamp); |
||
| 252 | while ($recurring_timestamp <= $until) { |
||
| 253 | // Add event |
||
| 254 | $anEvent['DTSTART'] = date('Ymd\THis',$recurring_timestamp); |
||
| 255 | $anEvent['DTEND'] = date('Ymd\THis',$recurring_timestamp+$event_timestmap_offset); |
||
| 256 | $events[] = $anEvent; |
||
| 257 | // Move forward |
||
| 258 | $recurring_timestamp = strtotime($offset,$recurring_timestamp); |
||
| 259 | } |
||
| 260 | break; |
||
| 261 | case 'WEEKLY': |
||
| 262 | // Create offset |
||
| 263 | $offset = "+$interval week"; |
||
| 264 | // Build list of days of week to add events |
||
| 265 | $weekdays = array('SU','MO','TU','WE','TH','FR','SA'); |
||
| 266 | $bydays = (isset($rrules['BYDAY']) && $rrules['BYDAY'] != '') ? explode(',', $rrules['BYDAY']) : array('SU','MO','TU','WE','TH','FR','SA'); |
||
| 267 | // Get timestamp of first day of start week |
||
| 268 | $week_recurring_timestamp = (date('w', $start_timestamp) == 0) ? $start_timestamp : strtotime('last Sunday '.date('H:i:s',$start_timestamp), $start_timestamp); |
||
| 269 | // Step through weeks |
||
| 270 | while ($week_recurring_timestamp <= $until) { |
||
| 271 | // Add events for bydays |
||
| 272 | $day_recurring_timestamp = $week_recurring_timestamp; |
||
| 273 | foreach ($weekdays as $day) { |
||
| 274 | // Check if day should be added |
||
| 275 | if (in_array($day, $bydays) && $day_recurring_timestamp > $start_timestamp && $day_recurring_timestamp <= $until) { |
||
| 276 | // Add event to day |
||
| 277 | $anEvent['DTSTART'] = date('Ymd\THis',$day_recurring_timestamp); |
||
| 278 | $anEvent['DTEND'] = date('Ymd\THis',$day_recurring_timestamp+$event_timestmap_offset); |
||
| 279 | $events[] = $anEvent; |
||
| 280 | } |
||
| 281 | // Move forward a day |
||
| 282 | $day_recurring_timestamp = strtotime('+1 day',$day_recurring_timestamp); |
||
| 283 | } |
||
| 284 | // Move forward $interaval weeks |
||
| 285 | $week_recurring_timestamp = strtotime($offset,$week_recurring_timestamp); |
||
| 286 | } |
||
| 287 | break; |
||
| 288 | case 'MONTHLY': |
||
| 289 | // Create offset |
||
| 290 | $offset = "+$interval month"; |
||
| 291 | $recurring_timestamp = strtotime($offset, $start_timestamp); |
||
| 292 | if (isset($rrules['BYMONTHDAY']) && $rrules['BYMONTHDAY'] != '') { |
||
| 293 | // Deal with BYMONTHDAY |
||
| 294 | while ($recurring_timestamp <= $until) { |
||
| 295 | // Add event |
||
| 296 | $anEvent['DTSTART'] = date('Ym'.sprintf('%02d',$rrules['BYMONTHDAY']).'\THis',$recurring_timestamp); |
||
| 297 | $anEvent['DTEND'] = date('Ymd\THis',$this->iCalDateToUnixTimestamp($anEvent['DTSTART'])+$event_timestmap_offset); |
||
| 298 | $events[] = $anEvent; |
||
| 299 | // Move forward |
||
| 300 | $recurring_timestamp = strtotime($offset,$recurring_timestamp); |
||
| 301 | } |
||
| 302 | } elseif (isset($rrules['BYDAY']) && $rrules['BYDAY'] != '') { |
||
| 303 | $start_time = date('His',$start_timestamp); |
||
| 304 | // Deal with BYDAY |
||
| 305 | $day_number = substr($rrules['BYDAY'], 0, 1); |
||
| 306 | $week_day = substr($rrules['BYDAY'], 1); |
||
| 307 | $day_cardinals = array(1 => 'first', 2 => 'second', 3 => 'third', 4 => 'fourth', 5 => 'fifth'); |
||
| 308 | $weekdays = array('SU' => 'sunday','MO' => 'monday','TU' => 'tuesday','WE' => 'wednesday','TH' => 'thursday','FR' => 'friday','SA' => 'saturday'); |
||
| 309 | while ($recurring_timestamp <= $until) { |
||
| 310 | $event_start_desc = "{$day_cardinals[$day_number]} {$weekdays[$week_day]} of ".date('F',$recurring_timestamp)." ".date('Y',$recurring_timestamp)." ".date('H:i:s',$recurring_timestamp); |
||
| 311 | $event_start_timestamp = strtotime($event_start_desc); |
||
| 312 | if ($event_start_timestamp > $start_timestamp && $event_start_timestamp < $until) { |
||
| 313 | $anEvent['DTSTART'] = date('Ymd\T',$event_start_timestamp).$start_time; |
||
| 314 | $anEvent['DTEND'] = date('Ymd\THis',$this->iCalDateToUnixTimestamp($anEvent['DTSTART'])+$event_timestmap_offset); |
||
| 315 | $events[] = $anEvent; |
||
| 316 | } |
||
| 317 | // Move forward |
||
| 318 | $recurring_timestamp = strtotime($offset,$recurring_timestamp); |
||
| 319 | } |
||
| 320 | } |
||
| 321 | break; |
||
| 322 | case 'YEARLY': |
||
| 323 | // Create offset |
||
| 324 | $offset = "+$interval year"; |
||
| 325 | $recurring_timestamp = strtotime($offset, $start_timestamp); |
||
| 326 | $month_names = array(1=>"January", 2=>"Februrary", 3=>"March", 4=>"April", 5=>"May", 6=>"June", 7=>"July", 8=>"August", 9=>"September", 10=>"October", 11=>"November", 12=>"December"); |
||
| 327 | // HACK: Exchange doesn't set a correct UNTIL for yearly events, so just go 2 years out |
||
| 328 | $until = strtotime('+2 year',$start_timestamp); |
||
| 329 | // Check if BYDAY rule exists |
||
| 330 | if (isset($rrules['BYDAY']) && $rrules['BYDAY'] != '') { |
||
| 331 | $start_time = date('His',$start_timestamp); |
||
| 332 | // Deal with BYDAY |
||
| 333 | $day_number = substr($rrules['BYDAY'], 0, 1); |
||
| 334 | $month_day = substr($rrules['BYDAY'], 1); |
||
| 335 | $day_cardinals = array(1 => 'first', 2 => 'second', 3 => 'third', 4 => 'fourth', 5 => 'fifth'); |
||
| 336 | $weekdays = array('SU' => 'sunday','MO' => 'monday','TU' => 'tuesday','WE' => 'wednesday','TH' => 'thursday','FR' => 'friday','SA' => 'saturday'); |
||
| 337 | while ($recurring_timestamp <= $until) { |
||
| 338 | $event_start_desc = "{$day_cardinals[$day_number]} {$weekdays[$month_day]} of {$month_names[$rrules['BYMONTH']]} ".date('Y',$recurring_timestamp)." ".date('H:i:s',$recurring_timestamp); |
||
| 339 | $event_start_timestamp = strtotime($event_start_desc); |
||
| 340 | if ($event_start_timestamp > $start_timestamp && $event_start_timestamp < $until) { |
||
| 341 | $anEvent['DTSTART'] = date('Ymd\T',$event_start_timestamp).$start_time; |
||
| 342 | $anEvent['DTEND'] = date('Ymd\THis',$this->iCalDateToUnixTimestamp($anEvent['DTSTART'])+$event_timestmap_offset); |
||
| 343 | $events[] = $anEvent; |
||
| 344 | } |
||
| 345 | // Move forward |
||
| 346 | $recurring_timestamp = strtotime($offset,$recurring_timestamp); |
||
| 347 | } |
||
| 348 | } else { |
||
| 349 | $day = date('d',$start_timestamp); |
||
| 350 | // Step throuhg years adding specific month dates |
||
| 351 | while ($recurring_timestamp <= $until) { |
||
| 352 | $event_start_desc = "$day {$month_names[$rrules['BYMONTH']]} ".date('Y',$recurring_timestamp)." ".date('H:i:s',$recurring_timestamp); |
||
| 353 | $event_start_timestamp = strtotime($event_start_desc); |
||
| 354 | if ($event_start_timestamp > $start_timestamp && $event_start_timestamp < $until) { |
||
| 355 | $anEvent['DTSTART'] = date('Ymd\T',$event_start_timestamp).$start_time; |
||
| 356 | $anEvent['DTEND'] = date('Ymd\THis',$this->iCalDateToUnixTimestamp($anEvent['DTSTART'])+$event_timestmap_offset); |
||
| 357 | $events[] = $anEvent; |
||
| 358 | } |
||
| 359 | // Move forward |
||
| 360 | $recurring_timestamp = strtotime($offset,$recurring_timestamp); |
||
| 361 | } |
||
| 362 | } |
||
| 363 | break; |
||
| 364 | } |
||
| 365 | } |
||
| 366 | } |
||
| 367 | $this->cal['VEVENT'] = $events; |
||
| 368 | } |
||
| 369 | |||
| 480 |