CalendarWeekView::getWeekEndDay()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
1
<?php
2
3
class CalendarWeekView extends CalendarAbstractTimeView
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
    
6
    // Attributes
7
8
    private $dayStart = 1;
9
    private $daysRemoved = array();
10
    
11
    // Abstract Functions Implemented
12
13
    public function init()
14
    {
15
        parent::init();
16
        $this->containerClass = 'weekView';
17
        $this->innerClass = 'week';
18
        $this->viewTitle = 'return \'Week Of \' . date(\'l jS F Y\', $date);';
19
    }
20
    
21 View Code Duplication
    public function prevLinkParams(Calendar $calendar)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
22
    {
23
        $date = $this->getWeekStartDay($calendar->getDay(), $calendar->getMonth(), $calendar->getYear());
24
        $dayValue = date('j', $date) - ($this->number * 7);
25
        $monthValue = date('n', $date);
26
        $yearValue = date('Y', $date);
27
        $date = mktime(0, 0, 0, $monthValue, $dayValue, $yearValue);
28
        return $this->getLinkParams($date);
29
    }
30
    
31 View Code Duplication
    public function nextLinkParams(Calendar $calendar)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
32
    {
33
        $date = $this->getWeekStartDay($calendar->getDay(), $calendar->getMonth(), $calendar->getYear());
34
        $dayValue = date('j', $date) + ($this->number * 7);
35
        $monthValue = date('n', $date);
36
        $yearValue = date('Y', $date);
37
        $date = mktime(0, 0, 0, $monthValue, $dayValue, $yearValue);
38
        return $this->getLinkParams($date);
39
    }
40
    
41
    public function title()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
42
    {
43
        return $this->number == 1 ? 'week' : "$this->number weeks";
44
    }
45
    
46
    public function Dates(Calendar $calendar)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
47
    {
48
        $year = $calendar->getYear();
49
        $month = $calendar->getMonth();
50
        $day = $calendar->getDay();
51
        
52
        if (count($this->daysRemoved) == 7) {
53
            return $datesGroups;
0 ignored issues
show
Bug introduced by
The variable $datesGroups seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
54
        }
55
        
56
        $lastDate = $this->getWeekStartDay($day, $month, $year);
57
        
58 View Code Duplication
        while (date('N', $lastDate) != $this->dayStart) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
            $lastDate = mktime(0, 0, 0, date('n', $lastDate), date('j', $lastDate) - 1, date('Y', $lastDate));
60
        }
61 View Code Duplication
        while (in_array(date('N', $lastDate), $this->daysRemoved)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
            $lastDate = mktime(0, 0, 0, date('n', $lastDate), date('j', $lastDate) + 1, date('Y', $lastDate));
63
        }
64
        
65
        for ($i = 0; $i < $this->number; $i++) {
66
            $datesGroup = array();
67
            for ($j = 0; $j < 7; $j++) {
68
                if (! in_array(date('N', $lastDate), $this->daysRemoved)) {
69
                    $datesGroup[] = $lastDate;
70
                }
71
                $lastDate = mktime(0, 0, 0, date('n', $lastDate), date('j', $lastDate) + 1, date('Y', $lastDate));
72
            }
73
            $datesGroups[] = $datesGroup;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$datesGroups was never initialized. Although not strictly required by PHP, it is generally a good practice to add $datesGroups = 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...
74
        }
75
        
76
        return $datesGroups;
0 ignored issues
show
Bug introduced by
The variable $datesGroups 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...
77
    }
78
    
79
    public function getCustomisedTitle($day, $month, $year)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
80
    {
81
        $date = $this->getWeekStartDay($day, $month, $year);
82
        $result = eval($this->viewTitle);
0 ignored issues
show
Coding Style introduced by
It is generally not recommended to use eval unless absolutely required.

On one hand, eval might be exploited by malicious users if they somehow manage to inject dynamic content. On the other hand, with the emergence of faster PHP runtimes like the HHVM, eval prevents some optimization that they perform.

Loading history...
83
        if ($this->number > 1) {
84
            $dayValue = date('j', $date) + (($this->number - 1) * 7);
85
            $monthValue = date('n', $date);
86
            $yearValue = date('Y', $date);
87
            $date = mktime(0, 0, 0, $monthValue, $dayValue, $yearValue);
0 ignored issues
show
Unused Code introduced by
$date 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...
88
            $result .= $this->viewTitleDelimiter . eval($this->viewTitle);
0 ignored issues
show
Coding Style introduced by
It is generally not recommended to use eval unless absolutely required.

On one hand, eval might be exploited by malicious users if they somehow manage to inject dynamic content. On the other hand, with the emergence of faster PHP runtimes like the HHVM, eval prevents some optimization that they perform.

Loading history...
89
        }
90
        return $result;
91
    }
92
    
93
    // Functions
94
95
    public function startByMonday()
96
    {
97
        $this->dayStart = 1;
98
    }
99
    public function startByTuesday()
100
    {
101
        $this->dayStart = 2;
102
    }
103
    public function startByWednesday()
104
    {
105
        $this->dayStart = 3;
106
    }
107
    public function startByThursday()
108
    {
109
        $this->dayStart = 4;
110
    }
111
    public function startByFriday()
112
    {
113
        $this->dayStart = 5;
114
    }
115
    public function startBySaturday()
116
    {
117
        $this->dayStart = 6;
118
    }
119
    public function startBySunday()
120
    {
121
        $this->dayStart = 7;
122
    }
123
    
124
    public function removeMonday()
125
    {
126
        $this->removeDay(1);
127
    }
128
    public function removeTuesday()
129
    {
130
        $this->removeDay(2);
131
    }
132
    public function removeWednesday()
133
    {
134
        $this->removeDay(3);
135
    }
136
    public function removeThursday()
137
    {
138
        $this->removeDay(4);
139
    }
140
    public function removeFriday()
141
    {
142
        $this->removeDay(5);
143
    }
144
    public function removeSaturday()
145
    {
146
        $this->removeDay(6);
147
    }
148
    public function removeSunday()
149
    {
150
        $this->removeDay(7);
151
    }
152
    
153
    // Private Functions
154
155
    private function getWeekStartDay($day, $month, $year)
156
    {
157
        $date = mktime(0, 0, 0, $month, $day, $year);
158
        
159 View Code Duplication
        while (date('N', $date) > 1) { // It means that the 1st day of this week is not Monday
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
160
            $date = mktime(0, 0, 0, date('n', $date), date('j', $date) - 1, date('Y', $date));
161
        }
162
        
163
        return $date;
164
    }
165
    
166
    private function getWeekEndDay($day, $month, $year)
167
    {
168
        $date = $this->getWeekStartDay($day, $month, $year);
169
        $date = mktime(0, 0, 0, date('n', $date), date('j', $date) + 6, date('Y', $date));
170
        return $date;
171
    }
172
    
173
    private function removeDay($day)
174
    {
175
        if (! in_array($day, $this->daysRemoved)) {
176
            $this->daysRemoved[] = $day;
177
        }
178
    }
179
}
180