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

DatetimeProvider   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 0
cbo 3
dl 0
loc 54
ccs 18
cts 18
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
C provide() 0 32 7
A applyConstraints() 0 9 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