OptimizedProvider::provide()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
dl 0
loc 25
ccs 10
cts 10
cp 1
rs 9.8666
c 1
b 0
f 0
cc 4
nc 8
nop 1
crap 4
1
<?php
2
3
namespace Recurrence\Provider;
4
5
use Recurrence\Model\Recurrence;
6
7
class OptimizedProvider extends AbstractDatetimeProvider
8
{
9
    /**
10
     * @return array<\DateTime>
11
     */
12
    public function provide(Recurrence $recurrence): array
13
    {
14 1
        $interval = $recurrence->getFrequency()->convertToDateIntervalFormat();
15
16 1
        $periodEndAt = ($recurrence->hasPeriodEndAt()) ? $recurrence->getPeriodEndAt() : $this->estimatePeriodEndAt($recurrence);
17
18
        // Transform interval in Datetime interval expression
19 1
        if ($recurrence->getInterval() !== 1) {
20 1
            $interval = str_replace('1', $recurrence->getInterval(), $interval);
21
        }
22
23 1
        $dateInterval = new \DateInterval($interval);
24
25 1
        $recurrences = iterator_to_array(new \DatePeriod(
26 1
            $recurrence->getPeriodStartAt(),
27
            $dateInterval,
28
            $periodEndAt
1 ignored issue
show
Bug introduced by
It seems like $periodEndAt can also be of type null; however, parameter $end of DatePeriod::__construct() does only seem to accept DateTimeInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

28
            /** @scrutinizer ignore-type */ $periodEndAt
Loading history...
29
        ));
30
31
        // When having count option, return only amount of recurrences requested
32 1
        if ($recurrence->hasCount()) {
33 1
            return array_slice($recurrences, 0, $recurrence->getCount());
34
        }
35
36 1
        return $recurrences;
37
    }
38
}
39