Passed
Push — master ( de3d61...be839c )
by Alec
13:42 queued 13s
created

TimerBuilder   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
c 0
b 0
f 0
dl 0
loc 36
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 6 1
A withStartTime() 0 5 1
A build() 0 7 1
A withTimeFunction() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
6
namespace AlecRabbit\Spinner\Core\Builder;
7
8
use AlecRabbit\Spinner\Contract\ITimer;
9
use AlecRabbit\Spinner\Core\Builder\Contract\ITimerBuilder;
10
use AlecRabbit\Spinner\Core\Timer;
11
use AlecRabbit\Spinner\Exception\LogicException;
12
use Closure;
13
14
final class TimerBuilder implements ITimerBuilder
15
{
16
    private ?float $startingTime = null;
17
    private ?Closure $timeFunction = null;
18
19
    public function build(): ITimer
20
    {
21
        $this->validate();
22
23
        return new Timer(
24
            timeFunction: $this->timeFunction,
0 ignored issues
show
Bug introduced by
It seems like $this->timeFunction can also be of type null; however, parameter $timeFunction of AlecRabbit\Spinner\Core\Timer::__construct() does only seem to accept Closure, 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

24
            /** @scrutinizer ignore-type */ timeFunction: $this->timeFunction,
Loading history...
25
            time: $this->startingTime,
0 ignored issues
show
Bug introduced by
It seems like $this->startingTime can also be of type null; however, parameter $time of AlecRabbit\Spinner\Core\Timer::__construct() does only seem to accept double, 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

25
            /** @scrutinizer ignore-type */ time: $this->startingTime,
Loading history...
26
        );
27
    }
28
29
    private function validate(): void
30
    {
31
        match (true) {
32
            null === $this->startingTime => throw new LogicException('Starting time is not set.'),
33
            $this->timeFunction === null => throw new LogicException('Time function is not set.'),
34
            default => null,
35
        };
36
    }
37
38
    public function withStartTime(float $time): ITimerBuilder
39
    {
40
        $clone = clone $this;
41
        $clone->startingTime = $time;
42
        return $clone;
43
    }
44
45
    public function withTimeFunction(Closure $timeFunction): ITimerBuilder
46
    {
47
        $clone = clone $this;
48
        $clone->timeFunction = $timeFunction;
49
        return $clone;
50
    }
51
}
52