Completed
Pull Request — master (#18)
by Samuel
02:03
created

DatetimeProvider::applyConstraints()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
ccs 3
cts 3
cp 1
rs 9.6666
cc 2
eloc 4
nc 2
nop 2
crap 2
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;
0 ignored issues
show
Bug introduced by
The variable $filteredDatetime 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...
65
    }
66
}
67