Converter::clamp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
namespace LesserPhp\Color;
4
5
/**
6
 * lesserphp
7
 * https://www.maswaba.de/lesserphp
8
 *
9
 * LESS CSS compiler, adapted from http://lesscss.org
10
 *
11
 * Copyright 2013, Leaf Corcoran <[email protected]>
12
 * Copyright 2016, Marcus Schwarz <[email protected]>
13
 * Licensed under MIT or GPLv3, see LICENSE
14
 * @package LesserPhp
15
 */
16
class Converter
17
{
18
19
    /**
20
     * @param array $color
21
     *
22
     * @return array
23
     */
24 2
    public function toHSL($color)
25
    {
26 2
        if ($color[0] === 'hsl') {
27
            return $color;
28
        }
29
30 2
        $r = $color[1] / 255;
31 2
        $g = $color[2] / 255;
32 2
        $b = $color[3] / 255;
33
34 2
        $min = min($r, $g, $b);
35 2
        $max = max($r, $g, $b);
36
37 2
        $L = ($min + $max) / 2;
38 2
        if ($min == $max) {
39 2
            $S = $H = 0;
40
        } else {
41 1
            if ($L < 0.5) {
42 1
                $S = ($max - $min) / ($max + $min);
43
            } else {
44 1
                $S = ($max - $min) / (2.0 - $max - $min);
45
            }
46
47 1
            if ($r == $max) {
48 1
                $H = ($g - $b) / ($max - $min);
49 1
            } elseif ($g == $max) {
50 1
                $H = 2.0 + ($b - $r) / ($max - $min);
51 1
            } elseif ($b == $max) {
52 1
                $H = 4.0 + ($r - $g) / ($max - $min);
53
            }
54
        }
55
56
        $out = [
57 2
            'hsl',
58 2
            ($H < 0 ? $H + 6 : $H) * 60,
0 ignored issues
show
Bug introduced by
The variable $H does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
59 2
            $S * 100,
60 2
            $L * 100,
61
        ];
62
63 2
        if (count($color) > 4) {
64 1
            $out[] = $color[4];
65
        } // copy alpha
66
67 2
        return $out;
68
    }
69
70
    /**
71
     * @param double $comp
72
     * @param double $temp1
73
     * @param double $temp2
74
     *
75
     * @return double
76
     */
77 1
    protected function toRGBHelper($comp, $temp1, $temp2)
78
    {
79 1
        if ($comp < 0) {
80 1
            $comp += 1.0;
81 1
        } elseif ($comp > 1) {
82 1
            $comp -= 1.0;
83
        }
84
85 1
        if (6 * $comp < 1) {
86 1
            return $temp1 + ($temp2 - $temp1) * 6 * $comp;
87
        }
88 1
        if (2 * $comp < 1) {
89 1
            return $temp2;
90
        }
91 1
        if (3 * $comp < 2) {
92 1
            return $temp1 + ($temp2 - $temp1) * ((2 / 3) - $comp) * 6;
93
        }
94
95 1
        return $temp1;
96
    }
97
98
    /**
99
     * Converts a hsl array into a color value in rgb.
100
     * Expects H to be in range of 0 to 360, S and L in 0 to 100
101
     *
102
     * @param array $color
103
     *
104
     * @return array
105
     */
106 2
    public function toRGB(array $color)
107
    {
108 2
        if ($color[0] === 'color') {
109
            return $color;
110
        }
111
112 2
        $H = $color[1] / 360;
113 2
        $S = $color[2] / 100;
114 2
        $L = $color[3] / 100;
115
116 2
        if ($S === 0) {
117 2
            $r = $g = $b = $L;
118
        } else {
119 1
            $temp2 = $L < 0.5 ? $L * (1.0 + $S) : $L + $S - $L * $S;
120 1
            $temp1 = 2.0 * $L - $temp2;
121
122 1
            $r = $this->toRGBHelper($H + 1 / 3, $temp1, $temp2);
123 1
            $g = $this->toRGBHelper($H, $temp1, $temp2);
124 1
            $b = $this->toRGBHelper($H - 1 / 3, $temp1, $temp2);
125
        }
126
127
        // $out = array('color', round($r*255), round($g*255), round($b*255));
128 2
        $out = ['color', $r * 255, $g * 255, $b * 255];
129 2
        if (count($color) > 4) {
130 1
            $out[] = $color[4];
131
        } // copy alpha
132
133 2
        return $out;
134
    }
135
136
    /**
137
     * @param int $v
138
     * @param int $max
139
     * @param int $min
140
     *
141
     * @return int
142
     */
143 2
    public function clamp($v, $max = 1, $min = 0)
144
    {
145 2
        return min($max, max($min, $v));
146
    }
147
}
148