Color::newFromString()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 4
nop 1
dl 0
loc 16
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace EaselDrawing;
4
5
class Color
6
{
7
    private $red;
8
    private $green;
9
    private $blue;
10
11
    /**
12
     * Color constructor.
13
     * @param $red
14
     * @param $green
15
     * @param $blue
16
     */
17
    public function __construct(int $red, int $green, int $blue)
18
    {
19
        $this->red = max(0, min(255, $red));
20
        $this->green = max(0, min(255, $green));
21
        $this->blue = max(0, min(255, $blue));
22
    }
23
24
    public static function newFromArray(array $values)
25
    {
26
        $rgb = [0, 0, 0];
27
        $values = array_values($values);
28
        foreach (range(0, 2) as $index) {
29
            if (isset($values[$index])) {
30
                $rgb[$index] = $values[$index];
31
            }
32
        }
33
        return new Color($rgb[0], $rgb[1], $rgb[2]);
34
    }
35
36
    public static function newFromString(string $value)
37
    {
38
        $value = strtolower(substr($value, 0, 6));
39
        if (strlen($value) == 3) {
40
            $value = substr($value, 0, 1) . substr($value, 0, 1)
41
                . substr($value, 1, 1) . substr($value, 1, 1)
42
                . substr($value, 2, 1) . substr($value, 2, 1);
43
        }
44
        if (! preg_match('/^[[:xdigit:]]{6}$/', $value)) {
45
            throw new \InvalidArgumentException('A color must contain 3 or 6 hexadecimal characters');
46
        }
47
        $rgb = [];
48
        foreach (range(0, 2) as $index) {
49
            $rgb[$index] = substr($value, $index * 2, 2);
50
        }
51
        return new Color(hexdec($rgb[0]), hexdec($rgb[1]), hexdec($rgb[2]));
0 ignored issues
show
Bug introduced by
It seems like hexdec($rgb[2]) can also be of type double; however, parameter $blue of EaselDrawing\Color::__construct() does only seem to accept integer, 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

51
        return new Color(hexdec($rgb[0]), hexdec($rgb[1]), /** @scrutinizer ignore-type */ hexdec($rgb[2]));
Loading history...
Bug introduced by
It seems like hexdec($rgb[1]) can also be of type double; however, parameter $green of EaselDrawing\Color::__construct() does only seem to accept integer, 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

51
        return new Color(hexdec($rgb[0]), /** @scrutinizer ignore-type */ hexdec($rgb[1]), hexdec($rgb[2]));
Loading history...
Bug introduced by
It seems like hexdec($rgb[0]) can also be of type double; however, parameter $red of EaselDrawing\Color::__construct() does only seem to accept integer, 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

51
        return new Color(/** @scrutinizer ignore-type */ hexdec($rgb[0]), hexdec($rgb[1]), hexdec($rgb[2]));
Loading history...
52
    }
53
54
    public function getRed(): int
55
    {
56
        return $this->red;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->red returns the type mixed which includes types incompatible with the type-hinted return integer.
Loading history...
57
    }
58
59
    public function getGreen(): int
60
    {
61
        return $this->green;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->green returns the type mixed which includes types incompatible with the type-hinted return integer.
Loading history...
62
    }
63
64
    public function getBlue(): int
65
    {
66
        return $this->blue;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->blue returns the type mixed which includes types incompatible with the type-hinted return integer.
Loading history...
67
    }
68
69
    public function getRGB()
70
    {
71
        return [$this->red, $this->blue, $this->green];
72
    }
73
74
    public function getHex(): string
75
    {
76
        return $this->intToHex($this->red)
77
            . $this->intToHex($this->green)
78
            . $this->intToHex($this->blue);
79
    }
80
81
    private function intToHex($value): string
82
    {
83
        return sprintf('%02s', dechex($value));
84
    }
85
}
86