These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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
|
|||
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
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
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 ? $L * (1.0 + $S) : $L + $S - $L * $S; |
|
121 | 1 | $temp1 = 2.0 * $L - $temp2; |
|
122 | |||
123 | 1 | $r = $this->toRGBHelper($H + 1 / 3, $temp1, $temp2); |
|
124 | 1 | $g = $this->toRGBHelper($H, $temp1, $temp2); |
|
125 | 1 | $b = $this->toRGBHelper($H - 1 / 3, $temp1, $temp2); |
|
126 | } |
||
127 | |||
128 | // $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...
|
|||
129 | 2 | $out = ['color', $r * 255, $g * 255, $b * 255]; |
|
130 | 2 | if (count($color) > 4) { |
|
131 | 1 | $out[] = $color[4]; |
|
132 | } // copy alpha |
||
133 | |||
134 | 2 | return $out; |
|
135 | } |
||
136 | |||
137 | /** |
||
138 | * @param int $v |
||
139 | * @param int $max |
||
140 | * @param int $min |
||
141 | * |
||
142 | * @return mixed |
||
143 | */ |
||
144 | 2 | public function clamp($v, $max = 1, $min = 0) |
|
145 | { |
||
146 | 2 | return min($max, max($min, $v)); |
|
147 | } |
||
148 | } |
||
149 |
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:
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
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: