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