1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Recurrence; |
4
|
|
|
|
5
|
|
|
use Recurrence\Model\Recurrence; |
6
|
|
|
use Recurrence\Provider\DatetimeProviderFactory; |
7
|
|
|
use Recurrence\Validator\RecurrenceValidator; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class DatetimeProvider |
11
|
|
|
* @package Recurrence |
12
|
|
|
*/ |
13
|
|
|
class DatetimeProvider |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @param Recurrence $recurrence |
17
|
|
|
* @return array<\DateTime> |
18
|
|
|
*/ |
19
|
|
|
public function provide(Recurrence $recurrence) |
20
|
|
|
{ |
21
|
1 |
|
RecurrenceValidator::validate($recurrence); |
22
|
|
|
|
23
|
1 |
|
$provider = DatetimeProviderFactory::create($recurrence); |
24
|
|
|
|
25
|
1 |
|
$datetimes = $provider->provide($recurrence); |
26
|
|
|
|
27
|
1 |
|
if (!$recurrence->hasConstraints()) { |
28
|
1 |
|
return $datetimes; |
29
|
|
|
} |
30
|
|
|
|
31
|
1 |
|
$filteredDatetimes = []; |
32
|
|
|
|
33
|
1 |
|
$previousDatetime = null; |
34
|
1 |
|
foreach ($datetimes as $key => $datetime) { |
35
|
1 |
|
$filteredDatetime = $this->applyConstraints($recurrence, $datetime); |
36
|
|
|
|
37
|
|
|
// Check that datetime do not pass recurrence end period |
38
|
1 |
|
if ($recurrence->hasPeriodEndAt() && $datetime > $recurrence->getPeriodEndAt()) { |
39
|
1 |
|
break; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
// Avoid duplicate datetime due to constraint updates |
43
|
1 |
|
if (empty($filteredDatetimes) || $previousDatetime != $filteredDatetime) { |
44
|
1 |
|
$filteredDatetimes[] = $filteredDatetime; |
45
|
1 |
|
$previousDatetime = $filteredDatetime; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
1 |
|
return $filteredDatetimes; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param Recurrence $recurrence |
54
|
|
|
* @param \Datetime $datetime |
55
|
|
|
* @return \Datetime |
56
|
|
|
*/ |
57
|
|
|
private function applyConstraints($recurrence, $datetime) |
58
|
|
|
{ |
59
|
|
|
// Apply each constraint on current datetime |
60
|
1 |
|
foreach ($recurrence->getConstraints() as $constraint) { |
61
|
1 |
|
$filteredDatetime = $constraint->apply($recurrence, $datetime); |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
return $filteredDatetime; |
|
|
|
|
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
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:
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
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: