Conditions | 51 |
Paths | > 20000 |
Total Lines | 271 |
Code Lines | 153 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 1 |
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 |
||
295 | private function draw_month( $month, $year, $id = 0 ) { |
||
296 | |||
297 | $calendar = $this->calendar; |
||
298 | if ( empty( $calendar ) ) { |
||
299 | $calendar = simcal_get_calendar( intval( $id ) ); |
||
300 | if ( ! $calendar ) { |
||
301 | return ''; |
||
302 | } |
||
303 | } |
||
304 | |||
305 | $events = $calendar->events; |
||
306 | |||
307 | $feed = simcal_get_feed( $calendar ); |
||
308 | $feed_timezone = get_post_meta( $feed->post_id, '_feed_timezone', true ); |
||
309 | |||
310 | // Variables to cycle days in current month and find today in calendar. |
||
311 | $now = $calendar->now; |
||
312 | $current = Carbon::create( $year, $month, 1, 0, 0, 0, $calendar->timezone ); |
||
313 | $current_min = $current->getTimestamp(); |
||
314 | $current_max = $current->endOfDay()->getTimestamp(); |
||
315 | |||
316 | // Calendar grid variables. |
||
317 | $week_starts = $calendar->week_starts; |
||
318 | $week_of_year = $current->weekOfYear; // Relative count of the week number of the year. |
||
319 | $month_starts = $current->dayOfWeek; // Day upon which the month starts. |
||
320 | $days_in_month = $current->daysInMonth; // Number of days in the given month. |
||
321 | |||
322 | // Set current month events timestamp boundaries. |
||
323 | $this->start = $current_min; |
||
324 | $this->end = $current->endOfMonth()->timestamp; |
||
325 | |||
326 | // Get daily events for this month. |
||
327 | if ( $events && is_array( $events ) ) { |
||
328 | |||
329 | // Filter events within the boundaries previously set above. |
||
330 | $timestamps = array_keys( $events ); |
||
331 | $lower_bound = array_filter( $timestamps, array( $this, 'filter_events_before' ) ); |
||
332 | $higher_bound = array_filter( $lower_bound, array( $this, 'filter_events_after' ) ); |
||
333 | $filtered = ( is_array( $events ) && is_array( $higher_bound) ) && ! empty( $events ) && ! empty( $higher_bound ) ? array_intersect_key( $events, array_combine( $higher_bound, $higher_bound ) ) : array(); |
||
334 | |||
335 | // Put resulting events in an associative array, with day of the month as key for easy retrieval in calendar days loop. |
||
336 | $day_events = array(); |
||
337 | foreach ( $filtered as $timestamp => $events_in_day ) { |
||
338 | foreach ( $events_in_day as $event ) { |
||
339 | if ( $event instanceof Event ){ |
||
340 | $day = intval( Carbon::createFromTimestamp( $timestamp, $event->timezone )->endOfDay()->day ); |
||
341 | $day_events[ $day ][] = $event; |
||
342 | } |
||
343 | } |
||
344 | } |
||
345 | |||
346 | ksort( $day_events, SORT_NUMERIC ); |
||
347 | } |
||
348 | |||
349 | ob_start(); |
||
350 | |||
351 | echo '<tbody class="simcal-month simcal-month-' . $month . '">' . "\n"; |
||
352 | echo "\t" . '<tr class="simcal-week simcal-week-' . $week_of_year . '">'; |
||
353 | |||
354 | $days_in_row = 0; |
||
355 | // Week may start on an arbitrary day (sun, 0 - sat, 6). |
||
356 | $week_day = $week_starts; |
||
357 | |||
358 | // This fixes a possible bug when a month starts by Sunday (0). |
||
359 | if ( 0 !== $week_starts ) { |
||
360 | $b = $month_starts === 0 ? 7 : $month_starts; |
||
361 | } else { |
||
362 | $b = $month_starts; |
||
363 | } |
||
364 | |||
365 | // Void days in first week. |
||
366 | for ( $a = $week_starts; $a < $b; $a++ ) : |
||
367 | |||
368 | $last_void_day_class = ( $a === ( $b - 1 ) ) ? 'simcal-day-void-last' : ''; |
||
369 | |||
370 | echo '<td class="simcal-day simcal-day-void ' . $last_void_day_class . '"></td>' . "\n"; |
||
371 | |||
372 | // Reset day of the week count (sun, 0 - sat, 6). |
||
373 | if ( $week_day === 6 ) { |
||
374 | $week_day = -1; |
||
375 | } |
||
376 | $week_day++; |
||
377 | |||
378 | $days_in_row++; |
||
379 | |||
380 | endfor; |
||
381 | |||
382 | // Actual days of the month. |
||
383 | for ( $day = 1; $day <= $days_in_month; $day++ ) : |
||
384 | |||
385 | $count = 0; |
||
386 | $calendar_classes = array(); |
||
387 | $day_classes = 'simcal-day-' . $day . ' simcal-weekday-' . $week_day; |
||
388 | |||
389 | $border_style = $bg_color = $color = ''; |
||
390 | |||
391 | // Is this the present, the past or the future, Doc? |
||
392 | if ( $current_min <= $now && $current_max >= $now ) { |
||
393 | $day_classes .= ' simcal-today simcal-present simcal-day'; |
||
394 | $the_color = new Color( $calendar->today_color ); |
||
395 | $bg_color = '#' . $the_color->getHex(); |
||
396 | $color = $the_color->isDark() ? '#ffffff' : '#000000'; |
||
397 | $border_style = ' style="border: 1px solid ' . $bg_color . ';"'; |
||
398 | } elseif ( $current_max < $now ) { |
||
399 | $day_classes .= ' simcal-past simcal-day'; |
||
400 | } elseif ( $current_min > $now ) { |
||
401 | $day_classes .= ' simcal-future simcal-day'; |
||
402 | } |
||
403 | |||
404 | // Print events for the current day in loop, if found any. |
||
405 | if ( isset( $day_events[ $day ] ) ) : |
||
406 | |||
407 | $bullet_colors = array(); |
||
408 | |||
409 | $list_events = '<ul class="simcal-events">'; |
||
410 | |||
411 | foreach ( $day_events[ $day ] as $event ) : |
||
412 | |||
413 | $event_classes = $event_visibility = ''; |
||
414 | |||
415 | if ( $event instanceof Event ) : |
||
416 | |||
417 | if ( $feed->type == 'grouped-calendars' ) { |
||
418 | date_default_timezone_set( $feed_timezone ); |
||
419 | } else { |
||
420 | date_default_timezone_set( $event->timezone ); |
||
421 | } |
||
422 | |||
423 | // Store the calendar id where the event belongs (useful in grouped calendar feeds) |
||
424 | $calendar_class = 'simcal-events-calendar-' . strval( $event->calendar ); |
||
425 | $calendar_classes[] = $calendar_class ; |
||
426 | |||
427 | $recurring = $event->recurrence ? 'simcal-event-recurring ' : ''; |
||
428 | $has_location = $event->venue ? 'simcal-event-has-location ' : ''; |
||
429 | |||
430 | $event_classes .= 'simcal-event ' . $recurring . $has_location . $calendar_class . ' simcal-tooltip'; |
||
431 | |||
432 | // Toggle some events visibility if more than optional limit. |
||
433 | if ( ( $calendar->events_limit > -1 ) && ( $count >= $calendar->events_limit ) ) : |
||
434 | $event_classes .= ' simcal-event-toggled'; |
||
435 | $event_visibility = ' style="display: none"'; |
||
436 | endif; |
||
437 | |||
438 | // Event title in list. |
||
439 | $title = ! empty( $event->title ) ? trim( $event->title ) : __( 'Event', 'google-calendar-events' ); |
||
440 | if ( $calendar->trim_titles >= 1 ) { |
||
441 | $title = strlen( $title ) > $calendar->trim_titles ? mb_substr( $title, 0, $calendar->trim_titles ) . '…' : $title; |
||
442 | } |
||
443 | |||
444 | // Event color. |
||
445 | $bullet = ''; |
||
446 | //$bullet_color = '#000'; |
||
447 | $event_color = $event->get_color(); |
||
448 | if ( ! empty( $event_color ) ) { |
||
449 | $bullet = '<span style="color: ' . $event_color . ';">■</span> '; |
||
450 | $bullet_colors[] = $event_color; |
||
451 | } else { |
||
452 | $bullet_colors[] = '#000'; |
||
453 | } |
||
454 | |||
455 | // Event contents. |
||
456 | $list_events .= "\t" . '<li class="' . $event_classes . '"' . $event_visibility . ' itemscope itemtype="http://schema.org/Event">' . "\n"; |
||
457 | $list_events .= "\t\t" . '<span class="simcal-event-title">' . $bullet . $title . '</span>' . "\n"; |
||
458 | $list_events .= "\t\t" . '<div class="simcal-event-details simcal-tooltip-content" style="display: none;">' . $calendar->get_event_html( $event ) . '</div>' . "\n"; |
||
459 | $list_events .= "\t" . '</li>' . "\n"; |
||
460 | |||
461 | $count ++; |
||
462 | |||
463 | endif; |
||
464 | |||
465 | endforeach; |
||
466 | |||
467 | if ( ( $current_min <= $now ) && ( $current_max >= $now ) ) { |
||
468 | $day_classes .= ' simcal-today-has-events'; |
||
469 | } |
||
470 | $day_classes .= ' simcal-day-has-events simcal-day-has-' . strval( $count ) . '-events'; |
||
471 | |||
472 | if ( $calendar_classes ) { |
||
473 | $day_classes .= ' ' . trim( implode( ' ', array_unique( $calendar_classes ) ) ); |
||
474 | } |
||
475 | |||
476 | $list_events .= '</ul>' . "\n"; |
||
477 | |||
478 | // Optional button to toggle hidden events in list. |
||
479 | if ( ( $calendar->events_limit > -1 ) && ( $count > $calendar->events_limit ) ) : |
||
480 | $list_events .= '<button class="simcal-events-toggle"><i class="simcal-icon-down simcal-icon-animate"></i></button>'; |
||
481 | endif; |
||
482 | |||
483 | else : |
||
484 | |||
485 | // Empty cell for day with no events. |
||
486 | $list_events = '<span class="simcal-no-events"></span>'; |
||
487 | |||
488 | endif; |
||
489 | |||
490 | // The actual days with numbers and events in each row cell. |
||
491 | echo '<td class="' . $day_classes . '" data-events-count="' . strval( $count ) . '">' . "\n"; |
||
492 | |||
493 | if ( $color ) { |
||
494 | $day_style = ' style="background-color: ' . $bg_color . '; color: ' . $color .'"'; |
||
495 | } elseif ( $count > 0 ) { |
||
496 | $the_color = new Color( $calendar->days_events_color ); |
||
497 | $color = ! $color ? ( $the_color->isDark() ? '#ffffff' : '#000000' ) : $color; |
||
498 | $bg_color = ! $bg_color ? '#' . $the_color->getHex() : $bg_color; |
||
499 | $day_style = ' style="background-color: ' . $bg_color . '; color: ' . $color .'"'; |
||
500 | } else { |
||
501 | $day_style = ''; |
||
502 | } |
||
503 | |||
504 | echo "\t" . '<div' . $border_style . '>' . "\n"; |
||
505 | echo "\t\t" . '<span class="simcal-day-label simcal-day-number"' . $day_style . '>' . $day . '</span>' . "\n"; |
||
506 | echo "\t\t" . $list_events . "\n"; |
||
507 | echo "\t\t"; |
||
508 | echo '<span class="simcal-events-dots" style="display: none;">'; |
||
509 | |||
510 | // Event bullets for calendar mobile mode. |
||
511 | for( $i = 0; $i < $count; $i++ ) { |
||
512 | echo '<b style="color: ' . $bullet_colors[ $i ] . ';"> • </b>'; |
||
513 | } |
||
514 | |||
515 | echo '</span>' . "\n"; |
||
516 | echo "\t" . '</div>' . "\n"; |
||
517 | echo '</td>' . "\n"; |
||
518 | |||
519 | // Reset day of the week count (sun, 0 - sat, 6). |
||
520 | if ( $week_day === 6 ) { |
||
521 | $week_day = - 1; |
||
522 | } |
||
523 | $week_day++; |
||
524 | |||
525 | // Reset count of days for this row (0-6). |
||
526 | if ( $days_in_row === 6 ) : |
||
527 | |||
528 | // Close the week row. |
||
529 | echo '</tr>'; |
||
530 | |||
531 | // Open a new week row. |
||
532 | if ( $day < $days_in_month ) { |
||
533 | echo '<tr class="simcal-week simcal-week-' . $week_of_year++ . '">' . "\n"; |
||
534 | } |
||
535 | |||
536 | $days_in_row = -1; |
||
537 | |||
538 | endif; |
||
539 | |||
540 | $days_in_row++; |
||
541 | |||
542 | $current_min = Carbon::createFromTimestamp( $current_min, $calendar->timezone )->addDay()->getTimestamp(); |
||
543 | $current_max = Carbon::createFromTimestamp( $current_max, $calendar->timezone )->addDay()->getTimestamp(); |
||
544 | |||
545 | endfor; |
||
546 | |||
547 | // Void days at the end of the month. |
||
548 | $remainder_days = ( 6 - $days_in_row ); |
||
549 | |||
550 | for ( $i = 0; $i <= $remainder_days; $i ++ ) { |
||
551 | |||
552 | $last_void_day_class = ( $i == $remainder_days ) ? 'simcal-day-void-last' : ''; |
||
553 | |||
554 | echo '<td class="simcal-day simcal-day-void ' . $last_void_day_class . '"></td>' . "\n"; |
||
555 | |||
556 | $week_day++; |
||
557 | } |
||
558 | |||
559 | echo "\t" . '</tr>' . "\n"; |
||
560 | echo '</tbody>' . "\n"; |
||
561 | |||
562 | date_default_timezone_set( $calendar->site_timezone ); |
||
563 | |||
564 | return ob_get_clean(); |
||
565 | } |
||
566 | |||
619 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..