Completed
Pull Request — master (#17)
by
unknown
02:55
created

CCalendar::nextMonthDate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Mos\Calendar;
4
5
use \DateTime;
6
7
/**
8
 * Class for calendar function
9
 */
10
class CCalendar
11
{
12
13
    /**
14
    * Properties
15
    *
16
    */
17
    private $today;
18
    private $firstDayInWeekOfMonth;
19
    private $lastDayInLastWeek;
20
    private $lastDayInMonth;
21
    private $thisMonth;
22
    private $prevMonth;
23
    private $nextMonth;
24
25
    /**
26
    * Constructor
27
    *
28
    * @param string date in month to display. Format yyyy-mm-dd
29
    *
30
    */
31
    public function __construct($displayDate = null)
32
    {
33
        setlocale(LC_TIME, "Swedish");
34
        define("CHARSET", "iso-8859-1");
35
36
        if (null == $displayDate) {
37
            $date = new DateTime();
38
        } else {
39
            $date = new DateTime($displayDate);
40
        }
41
        $this->today = new DateTime();
42
        // Remove time, only keep date when we compare later.
43
        $this->today->setTime(0, 0);
44
45
        // Find first week and day in month and first day in that week
46
        $year = $date->format('Y');
47
        $month = $date->format('m');
48
        $this->firstDayInMonth = new DateTime();
0 ignored issues
show
Bug introduced by
The property firstDayInMonth does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
49
        $this->firstDayInMonth->setTime(0, 0);
50
        $this->firstDayInMonth->setDate($year, $month, 1);
51
        $dayOfWeek = $this->firstDayInMonth->format('N');
52
        $subtractDays = $dayOfWeek - 1;
53
        $this->firstDayInWeekOfMonth = new DateTime($this->firstDayInMonth->format('Y-m-d'));
54
        $this->firstDayInWeekOfMonth->modify("-{$subtractDays} day");
55
56
        // Find last week and day in month and last day in that week
57
        $daysInMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year);
58
        $this->lastDayInMonth = new DateTime("$year-$month-$daysInMonth");
59
        $this->lastDayInMonth->setTime(0, 0);
60
        $dayOfWeek = $this->lastDayInMonth->format('N');
61
        $addDays = 7 - $dayOfWeek;
62
        $this->lastDayInLastWeek = new DateTime($this->lastDayInMonth->format('Y-m-d'));
63
        $this->lastDayInLastWeek->modify("+$addDays day");
64
65
        $this->thisMonth = new DateTime($this->firstDayInMonth->format('Y-m-d'));
66
        $this->prevMonth = new DateTime($this->firstDayInMonth->format('Y-m-d'));
67
        $this->prevMonth->modify('-1 month');
68
        $this->nextMonth = new DateTime($this->firstDayInMonth->format('Y-m-d'));
69
        $this->nextMonth->modify('+1 month');
70
71
72
    }
73
74
75
    public function today()
76
    {
77
        $today = new DateTime();
78
        $out = utf8_encode(strftime("%A %d %B %Y", strtotime($today->format('Y-m-d'))));
79
        return $out;
80
    }
81
82
    /**
83
    * Get current month with month and year
84
    */
85
    public function thisMonth()
86
    {
87
        return utf8_encode(strftime("%B %Y", strtotime($this->thisMonth->format('Y-m-d'))));
88
    }
89
90
    /**
91
    * Get previous month in text format
92
    */
93
    public function prevMonth()
94
    {
95
        return utf8_encode(strftime("%B", strtotime($this->prevMonth->format('Y-m-d'))));
96
    }
97
98
    /**
99
    * Get previous month in format yyyy-mm-dd
100
    */
101
    public function prevMonthDate()
102
    {
103
        return $this->prevMonth->format('Y-m-d');
104
    }
105
106
    /**
107
    * Get next month in text format
108
    */
109
    public function nextMonth()
110
    {
111
        return utf8_encode(strftime("%B", strtotime($this->nextMonth->format('Y-m-d'))));
112
    }
113
114
    /**
115
    * Get next month in format yyyy-mm-dd
116
    */
117
    public function nextMonthDate()
118
    {
119
        return $this->nextMonth->format('Y-m-d');
120
    }
121
122
    /**
123
    * Get all dates in month with additional days in first and last week.
124
    *
125
    */
126
    public function datesInMonth()
127
    {
128
        $date = $this->firstDayInWeekOfMonth;
129
        while ($date <= $this->lastDayInLastWeek) {
130
            $w = ltrim($date->format('W'), 0);
131
            for ($d=1; $d < 8; $d++) {
132
                // $dateText = utf8_encode(strftime("%A %e %B", strtotime($date->format('Y-m-d'))));
133
                // %e does not work on windows, use %d instead
134
                $dateText = utf8_encode(strftime("%A %#d %B", strtotime($date->format('Y-m-d'))));
0 ignored issues
show
Unused Code introduced by
$dateText is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
135
                $dateText = $date->format('d M');
136
                // $dateText = $date->format('D d M');
137
                $redDay = (7==$d) ? "red-day" : "";
138
                $classToday = ($date == $this->today) ? "today" : "";
139
                $classThisMonth = ($date < $this->firstDayInMonth || $date > $this->lastDayInMonth) ? "class-outside-month" : "class-inside-month";
140
                $date->modify("+1 day");
141
                $dates[] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$dates was never initialized. Although not strictly required by PHP, it is generally a good practice to add $dates = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
142
                    'week' => $w,
143
                    'date' => $dateText,
144
                    'class-red' => $redDay,
145
                    'class-today' => $classToday,
146
                    'class-in-month' => $classThisMonth,
147
                );
148
            }
149
        }
150
        return $dates;
0 ignored issues
show
Bug introduced by
The variable $dates does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
151
    }
152
}
153