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