CalendarAbstractTimeView::setTimeTitle()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
abstract class CalendarAbstractTimeView extends CalendarAbstractView
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 $timePeriod;
9
    private $subPeriodsLength;
10
    private $subPeriodsRemoved = array();
11
    
12
    protected $timeTitle;
13
    protected $dayTitleClass;
14
    protected $dayTitle;
15
    protected $timeClass;
16
    protected $time;
17
    protected $dayClass;
18
        
19
    // Abstract Functions Implemented
20
21
    public function init()
22
    {
23
        $this->timePeriod = new CalendarTimePeriod(new CalendarTime(), new CalendarTime(23, 59, 59));
24
        $this->subPeriodsLength = new CalendarTime(1);
25
        
26
        $this->dayTitleClass = 'return strtolower(date(\'l\', $date));';
27
        $this->dayTitle = 'return date(\'l jS F Y\', $date);';
28
        $this->timeClass = 'return \'hour\' . date(\'H\', $subPeriodStartDateTime) . \' minute\' . date(\'i\', $subPeriodStartDateTime) . \' second\' . date(\'s\', $subPeriodStartDateTime);';
29
        $this->time = 'return date(\'H:i\', $subPeriodStartDateTime);';
30
        $this->dayClass = 'return strtolower(date(\'l\', $subPeriodStart));';
31
    }
32
    
33
    public function needsMonth()
34
    {
35
        return true;
36
    }
37
    public function needsDay()
38
    {
39
        return true;
40
    }
41
    
42
    public function Calendars(Calendar $calendar)
43
    {
44
        $datesGroups = $this->Dates($calendar);
45
        
46
        foreach ($datesGroups as $datesGroup) {
47
            $calendars[] = $this->Calendar($datesGroup, $calendar);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$calendars was never initialized. Although not strictly required by PHP, it is generally a good practice to add $calendars = 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...
48
        }
49
        
50
        return new ArrayList($calendars);
0 ignored issues
show
Bug introduced by
The variable $calendars 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...
51
    }
52
    
53
    public function viewLinkParamsAndTitle(Calendar $calendar)
54
    {
55
        $day = $calendar->getDay();
56
        if (! $day) {
57
            $day = 1;
58
        }
59
        $month = $calendar->getMonth();
60
        if (! $month) {
61
            $month = 1;
62
        }
63
        $year = $calendar->getYear();
64
        $date = mktime(0, 0, 0, $month, $day, $year);
65
        $params = $this->getLinkParams($date);
66
        $title = $this->getCustomisedTitle($day, $month, $year);
67
        return array($params, $title);
68
    }
69
    
70
    public function getLinkParams($date)
71
    {
72
        return array(
73
            'year' => date('Y', $date),
74
            'month' => date('n', $date),
75
            'day' => date('j', $date)
76
        );
77
    }
78
    
79
    public function DateTitle(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...
80
    {
81
        return $this->getCustomisedTitle($calendar->getDay(), $calendar->getMonth(), $calendar->getYear());
82
    }
83
    
84
    // Functions
85
86
    public function setTimePeriod(CalendarTimePeriod $timePeriod)
87
    {
88
        $this->timePeriod = $timePeriod;
89
    }
90
    public function setSubPeriodsLength(CalendarTime $subPeriodsLength)
91
    {
92
        $this->subPeriodsLength = $subPeriodsLength;
93
    }
94
    
95
    public function removeSubPeriods($subPeriods)
96
    {
97
        if (! is_array($subPeriods)) {
98
            $subPeriods = array($subPeriods);
99
        }
100
        foreach ($subPeriods as $subPeriod) {
101
            if (is_a($subPeriod, 'CalendarTimePeriod')) {
102
                $this->subPeriodsRemoved[] = $subPeriod;
103
            } else {
104
                user_error('CalendarAbstractTimeView::removeSubPeriods() : you cannot remove a period which class is not \'CalendarTimePeriod\'', E_USER_ERROR);
105
            }
106
        }
107
    }
108
    
109
    public function getSubPeriods()
110
    {
111
        $startSubPeriodTime = $this->timePeriod->getStartTime();
112
        $endTime = $this->timePeriod->getEndTime();
113
        
114
        while ($startSubPeriodTime < $endTime) {
115
            $endSubPeriodTime = $this->getEndSubPeriodTime($startSubPeriodTime);
116
            $subPeriods[] = new CalendarTimePeriod($startSubPeriodTime, $endSubPeriodTime);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$subPeriods was never initialized. Although not strictly required by PHP, it is generally a good practice to add $subPeriods = 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...
117
            $startSubPeriodTime = $endSubPeriodTime;
118
        }
119
        
120
        return $subPeriods;
0 ignored issues
show
Bug introduced by
The variable $subPeriods 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...
121
    }
122
    
123
    public function setTimeTitle($timeTitle)
124
    {
125
        $this->timeTitle = $timeTitle;
126
    }
127
    public function setDayTitleClass($dayTitleClass)
128
    {
129
        $this->dayTitleClass = $dayTitleClass;
130
    }
131
    public function setDayTitle($dayTitle)
132
    {
133
        $this->dayTitle = $dayTitle;
134
    }
135
    public function setTimeClass($timeClass)
136
    {
137
        $this->timeClass = $timeClass;
138
    }
139
    public function setTime($time)
140
    {
141
        $this->time = $time;
142
    }
143
    public function setDayClass($dayClass)
144
    {
145
        $this->dayClass = $dayClass;
146
    }
147
    
148
    // Private Functions
149
150
    private function getEndSubPeriodTime(CalendarTime $startSubPeriodTime)
151
    {
152
        $endSubPeriodTime = $startSubPeriodTime->add($this->subPeriodsLength);
153
        
154
        if ($endSubPeriodTime < $startSubPeriodTime) {
155
            $endSubPeriodTime = clone $this->timePeriod->getEndTime();
156
        }
157
        
158
        return $endSubPeriodTime;
159
    }
160
    
161
    // Abstract Functions
162
163
    abstract public function Dates(Calendar $calendar);
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
164
    
165
    abstract public function getCustomisedTitle($day, $month, $year);
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
166
    
167
    // Template Functions
168
169
    private function Calendar($dates, Calendar $currentCalendar)
170
    {
171
        
172
        // 1) Single Values
173
174
        $calendar['InnerClass'] = $this->innerClass;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$calendar was never initialized. Although not strictly required by PHP, it is generally a good practice to add $calendar = 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...
175
        $calendar['TimeTitle'] = $this->timeTitle;
176
        
177
        // 2) Days Values
178
179 View Code Duplication
        foreach ($dates as $date) {
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...
180
            $dayTitleClass = eval($this->dayTitleClass);
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...
181
            $dayTitle = eval($this->dayTitle);
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...
182
            $days[] = new ArrayData(array('DayTitleClass' => $dayTitleClass, 'DayTitle' => $dayTitle));
0 ignored issues
show
Coding Style Comprehensibility introduced by
$days was never initialized. Although not strictly required by PHP, it is generally a good practice to add $days = 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...
183
        }
184
        
185
        $calendar['Days'] = new ArrayList($days);
0 ignored issues
show
Bug introduced by
The variable $days 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...
186
        
187
        // 3) Periods Values
188
189
        $subPeriods = $this->getSubPeriods();
190
        
191
        $todayNow = mktime();
192
        
193
        foreach ($subPeriods as $subPeriod) {
194
            $period = array();
195
            
196
            $subPeriodStartTime = $subPeriod->getStartTime();
197
            $subPeriodEndTime = $subPeriod->getEndTime();
198
            
199
            $subPeriodStartTimeHours = $subPeriodStartTime->getHours();
200
            $subPeriodEndTimeHours = $subPeriodEndTime->getHours();
201
            
202
            $subPeriodStartTimeMinutes = $subPeriodStartTime->getMinutes();
203
            $subPeriodEndTimeMinutes = $subPeriodEndTime->getMinutes();
204
            
205
            $subPeriodStartTimeSeconds = $subPeriodStartTime->getSeconds();
206
            $subPeriodEndTimeSeconds = $subPeriodEndTime->getSeconds();
207
            
208
            $subPeriodStartDateTime = mktime($subPeriodStartTimeHours, $subPeriodStartTimeMinutes, $subPeriodStartTimeSeconds, 0, 0, 0);
0 ignored issues
show
Unused Code introduced by
$subPeriodStartDateTime 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...
209
            $subPeriodEndDateTime = mktime($subPeriodEndTimeHours, $subPeriodEndTimeMinutes, $subPeriodEndTimeSeconds, 0, 0, 0);
0 ignored issues
show
Unused Code introduced by
$subPeriodEndDateTime 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...
210
            
211
            // 1) Single Values
212
213
            $period['TimeClass'] = eval($this->timeClass);
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...
214
            $period['Time'] = eval($this->time);
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...
215
            
216
            // 2) Days Values
217
218
            $days = array();
219
            
220
            foreach ($dates as $date) {
221
                $day = date('j', $date);
222
                $month = date('n', $date);
223
                $year = date('Y', $date);
224
                
225
                $subPeriodStart = mktime($subPeriodStartTimeHours, $subPeriodStartTimeMinutes, $subPeriodStartTimeSeconds, $month, $day, $year);
226
                $subPeriodEnd = mktime($subPeriodEndTimeHours, $subPeriodEndTimeMinutes, $subPeriodEndTimeSeconds, $month, $day, $year);
227
            
228
                $subPeriodParams = array();
229
                    
230
                $subPeriodParams['IsTodayNow'] = $subPeriodStart <= $todayNow && $todayNow <= $subPeriodEnd;
231
                $subPeriodParams['IsToday'] = $day == date('j') && $month == date('n') && $year == date('Y');
232
                $subPeriodParams['IsPast'] = $subPeriodStart < $todayNow;
233
                $subPeriodParams['DayClass'] = eval($this->dayClass);
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...
234
                
235
                $this->extend('updateSubPeriodParams', $subPeriodStart, $subPeriodEnd, $subPeriodParams, $currentCalendar);
236
                
237
                $days[] = new ArrayData($subPeriodParams);
238
            }
239
            
240
            $period['Days'] = new ArrayList($days);
241
            
242
            $periods[] = new ArrayData($period);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$periods was never initialized. Although not strictly required by PHP, it is generally a good practice to add $periods = 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...
243
        }
244
                
245
        $calendar['Periods'] = new ArrayList($periods);
0 ignored issues
show
Bug introduced by
The variable $periods 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...
246
        
247
        return new ArrayData($calendar);
248
    }
249
}
250