SequenceStateBuilder::build()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AlecRabbit\Spinner\Core\Builder;
6
7
use AlecRabbit\Spinner\Core\Builder\Contract\ISequenceStateBuilder;
8
use AlecRabbit\Spinner\Core\Contract\ISequenceState;
9
use AlecRabbit\Spinner\Core\SequenceState;
0 ignored issues
show
Bug introduced by
The type AlecRabbit\Spinner\Core\SequenceState was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use AlecRabbit\Spinner\Exception\LogicException;
11
12
/**
13
 * @psalm-suppress PossiblyNullArgument
14
 */
15
final class SequenceStateBuilder implements ISequenceStateBuilder
16
{
17
    private ?string $sequence = null;
18
    private ?int $width = null;
19
    private ?int $previousWidth = null;
20
21
    public function withSequence(string $sequence): ISequenceStateBuilder
22
    {
23
        $clone = clone $this;
24
        $clone->sequence = $sequence;
25
        return $clone;
26
    }
27
28
    public function withWidth(int $width): ISequenceStateBuilder
29
    {
30
        $clone = clone $this;
31
        $clone->width = $width;
32
        return $clone;
33
    }
34
35
    public function withPreviousWidth(int $previousWidth): ISequenceStateBuilder
36
    {
37
        $clone = clone $this;
38
        $clone->previousWidth = $previousWidth;
39
        return $clone;
40
    }
41
42
    public function build(): ISequenceState
43
    {
44
        $this->validate();
45
46
        return new SequenceState(
47
            sequence: $this->sequence,
48
            width: $this->width,
49
            previousWidth: $this->previousWidth,
50
        );
51
    }
52
53
    /**
54
     * @throws LogicException
55
     */
56
    private function validate(): void
57
    {
58
        match (true) {
59
            $this->sequence === null => throw new LogicException('Sequence is not set.'),
60
            $this->width === null => throw new LogicException('Width is not set.'),
61
            $this->previousWidth === null => throw new LogicException('Previous width is not set.'),
62
            default => null,
63
        };
64
    }
65
}
66