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

CustomStylePattern::assertPattern()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 11
c 0
b 0
f 0
nc 5
nop 1
dl 0
loc 17
rs 9.6111
1
<?php
2
3
declare(strict_types=1);
4
5
6
namespace AlecRabbit\Spinner\Extras\Pattern;
7
8
use AlecRabbit\Spinner\Contract\Option\OptionStyleMode;
9
use AlecRabbit\Spinner\Core\Pattern\A\AStylePattern;
10
use AlecRabbit\Spinner\Exception\InvalidArgumentException;
11
use Traversable;
12
13
/** @psalm-suppress UnusedClass */
14
final class CustomStylePattern extends AStylePattern
15
{
16
    public function __construct(
17
        protected array $pattern,
18
        ?int $interval = null,
19
        bool $reversed = false,
20
    ) {
21
        self::assertPattern($pattern);
22
        parent::__construct(
23
            interval: $interval,
24
            reversed: $reversed,
25
        );
26
    }
27
28
    /**
29
     * @throws InvalidArgumentException
30
     */
31
    private static function assertPattern(array $pattern): void
32
    {
33
        if (empty($pattern)) {
34
            throw new InvalidArgumentException('Pattern is empty.');
35
        }
36
        if (!array_key_exists(OptionStyleMode::ANSI4->value, $pattern)) {
37
            throw new InvalidArgumentException('Pattern does not contain ANSI4 key.');
38
        }
39
        if (!array_key_exists(OptionStyleMode::ANSI8->value, $pattern)) {
40
            throw new InvalidArgumentException('Pattern does not contain ANSI8 key.');
41
        }
42
        if (!array_key_exists(OptionStyleMode::ANSI24->value, $pattern)) {
43
            throw new InvalidArgumentException('Pattern does not contain ANSI24 key.');
44
        }
45
        self::assertPatternSection($pattern[OptionStyleMode::ANSI4->value]);
46
        self::assertPatternSection($pattern[OptionStyleMode::ANSI8->value]);
47
        self::assertPatternSection($pattern[OptionStyleMode::ANSI24->value]);
48
//        match (true) {
49
//            empty($pattern) => throw new InvalidArgumentException('Pattern is empty.'),
50
//            !array_key_exists(OptionStyleMode::ANSI4->value, $pattern)
51
//            =>
52
//            throw new InvalidArgumentException('Pattern does not contain ANSI4 key.'),
53
//            !array_key_exists(OptionStyleMode::ANSI8->value, $pattern)
54
//            =>
55
//            throw new InvalidArgumentException('Pattern does not contain ANSI8 key.'),
56
//            !array_key_exists(OptionStyleMode::ANSI24->value, $pattern)
57
//            =>
58
//            throw new InvalidArgumentException('Pattern does not contain ANSI24 key.'),
59
//        };
60
    }
61
62
    private static function assertPatternSection(mixed $value): void
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed. ( Ignorable by Annotation )

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

62
    private static function assertPatternSection(/** @scrutinizer ignore-unused */ mixed $value): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
63
    {
64
    }
65
66
    public function getEntries(OptionStyleMode $styleMode = OptionStyleMode::ANSI8): Traversable
67
    {
68
        yield from $this->pattern[$styleMode->value];
69
    }
70
}
71