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

AColorToAnsiCodeConverter   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 27
c 0
b 0
f 0
dl 0
loc 87
rs 10
wmc 13

7 Methods

Rating   Name   Duplication   Size   Complexity  
A assert() 0 7 2
A normalize() 0 13 2
A toInt() 0 3 1
A convert4() 0 9 2
A __construct() 0 4 1
A convert8() 0 9 2
A assertColor() 0 8 3
1
<?php
2
3
declare(strict_types=1);
4
5
6
namespace AlecRabbit\Spinner\Extras\Color\A;
7
8
use AlecRabbit\Spinner\Contract\Option\OptionStyleMode;
9
use AlecRabbit\Spinner\Exception\InvalidArgumentException;
10
use AlecRabbit\Spinner\Extras\Color\Ansi4Color;
11
use AlecRabbit\Spinner\Extras\Color\Ansi8Color;
12
use AlecRabbit\Spinner\Extras\Color\Mixin\Ansi8ColorTableTrait;
13
14
abstract class AColorToAnsiCodeConverter
15
{
16
    use Ansi8ColorTableTrait;
17
18
    public function __construct(
19
        protected OptionStyleMode $styleMode,
20
    ) {
21
        self::assert($this);
22
    }
23
24
    protected static function assert(self $obj): void
25
    {
26
        if ($obj->styleMode === OptionStyleMode::NONE) {
27
            throw new InvalidArgumentException(
28
                sprintf(
29
                    'Unsupported style mode "%s".',
30
                    $obj->styleMode->name,
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on AlecRabbit\Spinner\Contract\Option\OptionStyleMode.
Loading history...
31
                )
32
            );
33
        }
34
    }
35
36
    /**
37
     * @throws InvalidArgumentException
38
     */
39
    protected function normalize(string $color): string
40
    {
41
        $color = strtolower($color);
42
43
        $color = str_replace('#', '', $color);
44
45
        if (strlen($color) === 3) {
46
            $color = $color[0] . $color[0] . $color[1] . $color[1] . $color[2] . $color[2];
47
        }
48
49
        $this->assertColor($color);
50
51
        return '#' . $color;
0 ignored issues
show
Bug introduced by
Are you sure $color of type array can be used in concatenation? ( Ignorable by Annotation )

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

51
        return '#' . /** @scrutinizer ignore-type */ $color;
Loading history...
52
    }
53
54
    /**
55
     * @throws InvalidArgumentException
56
     */
57
    private function assertColor(array|string $color): void
58
    {
59
        if ($color === '') {
0 ignored issues
show
introduced by
The condition $color === '' is always false.
Loading history...
60
            throw new InvalidArgumentException('Empty color string.');
61
        }
62
63
        if (strlen($color) !== 6) {
0 ignored issues
show
Bug introduced by
$color of type array is incompatible with the type string expected by parameter $string of strlen(). ( Ignorable by Annotation )

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

63
        if (strlen(/** @scrutinizer ignore-type */ $color) !== 6) {
Loading history...
64
            throw new InvalidArgumentException(sprintf('Invalid color: "#%s".', $color));
0 ignored issues
show
Bug introduced by
$color of type array is incompatible with the type double|integer|string expected by parameter $values of sprintf(). ( Ignorable by Annotation )

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

64
            throw new InvalidArgumentException(sprintf('Invalid color: "#%s".', /** @scrutinizer ignore-type */ $color));
Loading history...
65
        }
66
    }
67
68
    /**
69
     * @throws InvalidArgumentException
70
     */
71
    protected function convert8(string $color): string
72
    {
73
        $index = Ansi8Color::getIndex($color);
74
75
        if ($index) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $index of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
76
            return '8;5;' . $index;
77
        }
78
79
        return $this->convertHexColorToAnsiColorCode($color);
80
    }
81
82
    abstract protected function convertHexColorToAnsiColorCode(string $hexColor): string;
83
84
    /**
85
     * @throws InvalidArgumentException
86
     */
87
    protected function convert4(string $color): string
88
    {
89
        $index = Ansi4Color::getIndex($color);
90
91
        if ($index !== null) {
92
            return (string)$index;
93
        }
94
95
        return $this->convertHexColorToAnsiColorCode($color);
96
    }
97
98
    protected function toInt(string $color): int
99
    {
100
        return (int)hexdec(str_replace('#', '', $color));
101
    }
102
}
103