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