Passed
Push — master ( cfdd1e...2b821f )
by Marcus
02:49
created

Converter::clamp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

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
eloc 2
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
57
        $out = [
58 2
            'hsl',
59 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...
60 2
            $S * 100,
61 2
            $L * 100,
62
        ];
63
64 2
        if (count($color) > 4) {
65 1
            $out[] = $color[4];
66
        } // copy alpha
67
68 2
        return $out;
69
    }
70
71
    /**
72
     * @param double $comp
73
     * @param double $temp1
74
     * @param double $temp2
75
     *
76
     * @return double
77
     */
78 1
    protected function toRGBHelper($comp, $temp1, $temp2)
79
    {
80 1
        if ($comp < 0) {
81 1
            $comp += 1.0;
82 1
        } elseif ($comp > 1) {
83 1
            $comp -= 1.0;
84
        }
85
86 1 View Code Duplication
        if (6 * $comp < 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
87 1
            return $temp1 + ($temp2 - $temp1) * 6 * $comp;
88
        }
89 1
        if (2 * $comp < 1) {
90 1
            return $temp2;
91
        }
92 1 View Code Duplication
        if (3 * $comp < 2) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
93 1
            return $temp1 + ($temp2 - $temp1) * ((2 / 3) - $comp) * 6;
94
        }
95
96 1
        return $temp1;
97
    }
98
99
    /**
100
     * Converts a hsl array into a color value in rgb.
101
     * Expects H to be in range of 0 to 360, S and L in 0 to 100
102
     *
103
     * @param array $color
104
     *
105
     * @return array
106
     */
107 2
    public function toRGB(array $color)
108
    {
109 2
        if ($color[0] === 'color') {
110
            return $color;
111
        }
112
113 2
        $H = $color[1] / 360;
114 2
        $S = $color[2] / 100;
115 2
        $L = $color[3] / 100;
116
117 2
        if ($S === 0) {
118 2
            $r = $g = $b = $L;
119
        } else {
120 1
            $temp2 = $L < 0.5 ?
121 1
                $L * (1.0 + $S) :
122 1
                $L + $S - $L * $S;
123
124 1
            $temp1 = 2.0 * $L - $temp2;
125
126 1
            $r = $this->toRGBHelper($H + 1 / 3, $temp1, $temp2);
127 1
            $g = $this->toRGBHelper($H, $temp1, $temp2);
128 1
            $b = $this->toRGBHelper($H - 1 / 3, $temp1, $temp2);
129
        }
130
131
        // $out = array('color', round($r*255), round($g*255), round($b*255));
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
132 2
        $out = ['color', $r * 255, $g * 255, $b * 255];
133 2
        if (count($color) > 4) {
134 1
            $out[] = $color[4];
135
        } // copy alpha
136
137 2
        return $out;
138
    }
139
140
    /**
141
     * @param double $v
142
     * @param double $max
143
     * @param double $min
144
     *
145
     * @return mixed
146
     */
147 2
    public function clamp($v, $max = 1, $min = 0)
148
    {
149 2
        return min($max, max($min, $v));
150
    }
151
}
152