Passed
Push — master ( 45b9b9...c85d21 )
by Laurens
01:35
created

Rgb::isValid()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 2
nc 2
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LauLamanApps\ApplePassbook\Style\Color;
6
7
use LauLamanApps\ApplePassbook\Style\Color;
8
use LauLamanApps\ApplePassbook\Style\Color\Exception\OutOfRangeException;
9
10
class Rgb implements Color
11
{
12
    private int $red = 0;
13
    private int $green = 0;
14
    private int $blue = 0;
15
16
    public function __construct(int $red = null, int $green = null, int $blue = null)
17
    {
18
        if ($red) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $red 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...
19
            $this->setRed($red);
20
        }
21
22
        if ($green) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $green 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...
23
            $this->setGreen($green);
24
        }
25
26
        if ($blue) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $blue 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...
27 13
            $this->setBlue($blue);
28
        }
29 13
    }
30 13
31 13
    public function setRed(int $red): void
32 13
    {
33
        $this->isValid($red);
34 13
        $this->red = $red;
35
    }
36 13
37 13
    public function setGreen(int $green): void
38 13
    {
39
        $this->isValid($green);
40 13
        $this->green = $green;
41
    }
42 13
43 13
    public function setBlue(int $blue): void
44 13
    {
45
        $this->isValid($blue);
46 13
        $this->blue = $blue;
47
    }
48 13
49 13
    public function toString(): string
50 13
    {
51
        return sprintf('rgb(%s, %s, %s)', $this->red, $this->green, $this->blue);
52 7
    }
53
54 7
    private function isValid(int $value): void
55
    {
56
        if ($value < 0 || $value > 255 ) {
57 13
            throw new OutOfRangeException('Value out of range (0-255).');
58
        }
59 13
    }
60
}
61