DeltaTimerBuilder   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

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

4 Methods

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

26
            /** @scrutinizer ignore-type */ nowTimer: $this->nowTimer,
Loading history...
27
            startTime: $this->startTime,
0 ignored issues
show
Bug introduced by
It seems like $this->startTime can also be of type null; however, parameter $startTime of AlecRabbit\Spinner\Core\DeltaTimer::__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

27
            /** @scrutinizer ignore-type */ startTime: $this->startTime,
Loading history...
28
        );
29
    }
30
31
    private function validate(): void
32
    {
33
        match (true) {
34
            $this->startTime === null => throw new LogicException('Start time is not set.'),
35
            $this->nowTimer === null => throw new LogicException('NowTimer is not set.'),
36
            default => null,
37
        };
38
    }
39
40
    public function withStartTime(float $time): IDeltaTimerBuilder
41
    {
42
        $clone = clone $this;
43
        $clone->startTime = $time;
44
        return $clone;
45
    }
46
47
    public function withNowTimer(INowTimer $now): IDeltaTimerBuilder
48
    {
49
        $clone = clone $this;
50
        $clone->nowTimer = $now;
51
        return $clone;
52
    }
53
}
54