Test Setup Failed
Push — test ( 4027b3...9fddab )
by Jonathan
03:08
created

ColorRepresentation::hslToRgb()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 18
nc 6
nop 3
dl 0
loc 27
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace Kint\Object\Representation;
4
5
use InvalidArgumentException;
6
use LogicException;
7
8
class ColorRepresentation extends Representation
0 ignored issues
show
Coding Style introduced by
The property $color_map is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
The property $implicit_label is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
9
{
10
    const COLOR_NAME = 1;
11
    const COLOR_HEX_3 = 2;
12
    const COLOR_HEX_6 = 3;
13
    const COLOR_RGB = 4;
14
    const COLOR_RGBA = 5;
15
    const COLOR_HSL = 6;
16
    const COLOR_HSLA = 7;
17
    const COLOR_HEX_4 = 8;
18
    const COLOR_HEX_8 = 9;
19
20
    public static $color_map = array(
0 ignored issues
show
Coding Style introduced by
$color_map does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
21
        'aliceblue' => 'f0f8ff',
22
        'antiquewhite' => 'faebd7',
23
        'aqua' => '00ffff',
24
        'aquamarine' => '7fffd4',
25
        'azure' => 'f0ffff',
26
        'beige' => 'f5f5dc',
27
        'bisque' => 'ffe4c4',
28
        'black' => '000000',
29
        'blanchedalmond' => 'ffebcd',
30
        'blue' => '0000ff',
31
        'blueviolet' => '8a2be2',
32
        'brown' => 'a52a2a',
33
        'burlywood' => 'deb887',
34
        'cadetblue' => '5f9ea0',
35
        'chartreuse' => '7fff00',
36
        'chocolate' => 'd2691e',
37
        'coral' => 'ff7f50',
38
        'cornflowerblue' => '6495ed',
39
        'cornsilk' => 'fff8dc',
40
        'crimson' => 'dc143c',
41
        'cyan' => '00ffff',
42
        'darkblue' => '00008b',
43
        'darkcyan' => '008b8b',
44
        'darkgoldenrod' => 'b8860b',
45
        'darkgray' => 'a9a9a9',
46
        'darkgreen' => '006400',
47
        'darkgrey' => 'a9a9a9',
48
        'darkkhaki' => 'bdb76b',
49
        'darkmagenta' => '8b008b',
50
        'darkolivegreen' => '556b2f',
51
        'darkorange' => 'ff8c00',
52
        'darkorchid' => '9932cc',
53
        'darkred' => '8b0000',
54
        'darksalmon' => 'e9967a',
55
        'darkseagreen' => '8fbc8f',
56
        'darkslateblue' => '483d8b',
57
        'darkslategray' => '2f4f4f',
58
        'darkslategrey' => '2f4f4f',
59
        'darkturquoise' => '00ced1',
60
        'darkviolet' => '9400d3',
61
        'deeppink' => 'ff1493',
62
        'deepskyblue' => '00bfff',
63
        'dimgray' => '696969',
64
        'dimgrey' => '696969',
65
        'dodgerblue' => '1e90ff',
66
        'firebrick' => 'b22222',
67
        'floralwhite' => 'fffaf0',
68
        'forestgreen' => '228b22',
69
        'fuchsia' => 'ff00ff',
70
        'gainsboro' => 'dcdcdc',
71
        'ghostwhite' => 'f8f8ff',
72
        'gold' => 'ffd700',
73
        'goldenrod' => 'daa520',
74
        'gray' => '808080',
75
        'green' => '008000',
76
        'greenyellow' => 'adff2f',
77
        'grey' => '808080',
78
        'honeydew' => 'f0fff0',
79
        'hotpink' => 'ff69b4',
80
        'indianred' => 'cd5c5c',
81
        'indigo' => '4b0082',
82
        'ivory' => 'fffff0',
83
        'khaki' => 'f0e68c',
84
        'lavender' => 'e6e6fa',
85
        'lavenderblush' => 'fff0f5',
86
        'lawngreen' => '7cfc00',
87
        'lemonchiffon' => 'fffacd',
88
        'lightblue' => 'add8e6',
89
        'lightcoral' => 'f08080',
90
        'lightcyan' => 'e0ffff',
91
        'lightgoldenrodyellow' => 'fafad2',
92
        'lightgray' => 'd3d3d3',
93
        'lightgreen' => '90ee90',
94
        'lightgrey' => 'd3d3d3',
95
        'lightpink' => 'ffb6c1',
96
        'lightsalmon' => 'ffa07a',
97
        'lightseagreen' => '20b2aa',
98
        'lightskyblue' => '87cefa',
99
        'lightslategray' => '778899',
100
        'lightslategrey' => '778899',
101
        'lightsteelblue' => 'b0c4de',
102
        'lightyellow' => 'ffffe0',
103
        'lime' => '00ff00',
104
        'limegreen' => '32cd32',
105
        'linen' => 'faf0e6',
106
        'magenta' => 'ff00ff',
107
        'maroon' => '800000',
108
        'mediumaquamarine' => '66cdaa',
109
        'mediumblue' => '0000cd',
110
        'mediumorchid' => 'ba55d3',
111
        'mediumpurple' => '9370db',
112
        'mediumseagreen' => '3cb371',
113
        'mediumslateblue' => '7b68ee',
114
        'mediumspringgreen' => '00fa9a',
115
        'mediumturquoise' => '48d1cc',
116
        'mediumvioletred' => 'c71585',
117
        'midnightblue' => '191970',
118
        'mintcream' => 'f5fffa',
119
        'mistyrose' => 'ffe4e1',
120
        'moccasin' => 'ffe4b5',
121
        'navajowhite' => 'ffdead',
122
        'navy' => '000080',
123
        'oldlace' => 'fdf5e6',
124
        'olive' => '808000',
125
        'olivedrab' => '6b8e23',
126
        'orange' => 'ffa500',
127
        'orangered' => 'ff4500',
128
        'orchid' => 'da70d6',
129
        'palegoldenrod' => 'eee8aa',
130
        'palegreen' => '98fb98',
131
        'paleturquoise' => 'afeeee',
132
        'palevioletred' => 'db7093',
133
        'papayawhip' => 'ffefd5',
134
        'peachpuff' => 'ffdab9',
135
        'peru' => 'cd853f',
136
        'pink' => 'ffc0cb',
137
        'plum' => 'dda0dd',
138
        'powderblue' => 'b0e0e6',
139
        'purple' => '800080',
140
        'rebeccapurple' => '663399',
141
        'red' => 'ff0000',
142
        'rosybrown' => 'bc8f8f',
143
        'royalblue' => '4169e1',
144
        'saddlebrown' => '8b4513',
145
        'salmon' => 'fa8072',
146
        'sandybrown' => 'f4a460',
147
        'seagreen' => '2e8b57',
148
        'seashell' => 'fff5ee',
149
        'sienna' => 'a0522d',
150
        'silver' => 'c0c0c0',
151
        'skyblue' => '87ceeb',
152
        'slateblue' => '6a5acd',
153
        'slategray' => '708090',
154
        'slategrey' => '708090',
155
        'snow' => 'fffafa',
156
        'springgreen' => '00ff7f',
157
        'steelblue' => '4682b4',
158
        'tan' => 'd2b48c',
159
        'teal' => '008080',
160
        'thistle' => 'd8bfd8',
161
        'tomato' => 'ff6347',
162
        'turquoise' => '40e0d0',
163
        'violet' => 'ee82ee',
164
        'wheat' => 'f5deb3',
165
        'white' => 'ffffff',
166
        'whitesmoke' => 'f5f5f5',
167
        'yellow' => 'ffff00',
168
        'yellowgreen' => '9acd32',
169
    );
170
171
    public $r = 0;
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $r. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
172
    public $g = 0;
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $g. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
173
    public $b = 0;
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $b. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
174
    public $a = 1;
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $a. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
175
    public $variant = null;
176
    public $implicit_label = true;
0 ignored issues
show
Coding Style introduced by
$implicit_label does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
177
    public $hints = array('color');
178
179
    public function getColor($variant = null)
180
    {
181
        switch ($variant) {
182
            case self::COLOR_NAME:
183
                $hex = sprintf('%02x%02x%02x', $this->r, $this->g, $this->b);
184
185
                return array_search($hex, self::$color_map);
186
           case self::COLOR_HEX_3:
187
                if ($this->r % 0x11 === 0 && $this->g % 0x11 === 0 && $this->b % 0x11 === 0) {
188
                    return sprintf(
189
                        '#%1X%1X%1X',
190
                        round($this->r / 0x11),
191
                        round($this->g / 0x11),
192
                        round($this->b / 0x11)
193
                    );
194
                } else {
195
                    return false;
196
                }
197
            case self::COLOR_HEX_6:
198
                return sprintf('#%02X%02X%02X', $this->r, $this->g, $this->b);
199
            case self::COLOR_RGB:
200
                return sprintf('rgb(%d, %d, %d)', $this->r, $this->g, $this->b);
201 View Code Duplication
            case self::COLOR_RGBA:
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...
202
                return sprintf('rgba(%d, %d, %d, %s)', $this->r, $this->g, $this->b, round($this->a, 4));
203
            case self::COLOR_HSL:
204
                $val = self::rgbToHsl($this->r, $this->g, $this->b);
205
206
                return vsprintf('hsl(%d, %d%%, %d%%)', $val);
207
            case self::COLOR_HSLA:
208
                $val = self::rgbToHsl($this->r, $this->g, $this->b);
209
210
                return sprintf('hsla(%d, %d%%, %d%%, %s)', $val[0], $val[1], $val[2], round($this->a, 4));
211
            case self::COLOR_HEX_4:
212
                if ($this->r % 0x11 === 0 && $this->g % 0x11 === 0 && $this->b % 0x11 === 0 && ($this->a * 255) % 0x11 === 0) {
213
                    return sprintf(
214
                        '#%1X%1X%1X%1X',
215
                        round($this->r / 0x11),
216
                        round($this->g / 0x11),
217
                        round($this->b / 0x11),
218
                        round($this->a * 0xF)
219
                    );
220
                } else {
221
                    return false;
222
                }
223 View Code Duplication
            case self::COLOR_HEX_8:
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...
224
                return sprintf('#%02X%02X%02X%02X', $this->r, $this->g, $this->b, round($this->a * 0xFF));
225
            case null:
226
                return $this->contents;
227
        }
228
229
        return false;
230
    }
231
232
    public function __construct($value)
233
    {
234
        parent::__construct('Color');
235
236
        $this->contents = $value;
237
        $this->setValues($value);
238
    }
239
240
    public function hasAlpha($variant = null)
241
    {
242
        if ($variant === null) {
243
            $variant = $this->variant;
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $variant. This often makes code more readable.
Loading history...
244
        }
245
246
        switch ($variant) {
247
            case self::COLOR_NAME:
248
                return $this->a !== 1;
249
            case self::COLOR_RGBA:
250
            case self::COLOR_HSLA:
251
            case self::COLOR_HEX_4:
252
            case self::COLOR_HEX_8:
253
                return true;
254
            default:
255
                return false;
256
        }
257
    }
258
259
    protected function setValues($value)
260
    {
261
        $value = strtolower(trim($value));
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $value. This often makes code more readable.
Loading history...
262
        // Find out which variant of color input it is
263
        if (isset(self::$color_map[$value])) {
264
            $variant = self::COLOR_NAME;
265
        } elseif ($value[0] === '#') {
266
            $value = substr($value, 1);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $value. This often makes code more readable.
Loading history...
267
268
            if (dechex(hexdec($value)) !== $value) {
269
                return;
270
            }
271
272 View Code Duplication
            switch (strlen($value)) {
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...
273
                case 3:
274
                    $variant = self::COLOR_HEX_3;
275
                    break;
276
                case 6:
277
                    $variant = self::COLOR_HEX_6;
278
                    break;
279
                case 4:
280
                    $variant = self::COLOR_HEX_4;
281
                    break;
282
                case 8:
283
                    $variant = self::COLOR_HEX_8;
284
                    break;
285
                default:
286
                    return;
287
            }
288
        } else {
289
            if (!preg_match('/^((?:rgb|hsl)a?)\s*\(([0-9\.%,\s]+)\)$/i', $value, $match)) {
290
                return;
291
            }
292
293 View Code Duplication
            switch ($match[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...
294
                case 'rgb':
295
                    $variant = self::COLOR_RGB;
296
                    break;
297
                case 'rgba':
298
                    $variant = self::COLOR_RGBA;
299
                    break;
300
                case 'hsl':
301
                    $variant = self::COLOR_HSL;
302
                    break;
303
                case 'hsla':
304
                    $variant = self::COLOR_HSLA;
305
                    break;
306
                default:
307
                    return;
308
            }
309
310
            $value = explode(',', $match[2]);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $value. This often makes code more readable.
Loading history...
311
312
            if ($this->hasAlpha($variant)) {
313
                if (count($value) !== 4) {
314
                    return;
315
                }
316
            } elseif (count($value) !== 3) {
317
                return;
318
            }
319
320
            foreach ($value as $i => &$color) {
321
                $color = trim($color);
322
323
                if (strpos($color, '%') !== false) {
324
                    $color = str_replace('%', '', $color);
325
326
                    if ($i === 3) {
327
                        $color = $color / 100;
328
                    } elseif (in_array($variant, array(self::COLOR_RGB, self::COLOR_RGBA))) {
329
                        $color = round($color / 100 * 255);
330
                    } elseif ($i === 0 && in_array($variant, array(self::COLOR_HSL, self::COLOR_HSLA))) {
331
                        $color = $color / 100 * 360;
332
                    }
333
                }
334
335
                if ($i === 0 && in_array($variant, array(self::COLOR_HSL, self::COLOR_HSLA))) {
336
                    $color = ($color % 360 + 360) % 360;
337
                }
338
            }
339
        }
340
341
        // Assign the correct properties based on the variant
342
        switch ($variant) {
343
            case self::COLOR_HEX_4:
344
                $this->a = hexdec($value[3]) / 0xF;
0 ignored issues
show
Documentation Bug introduced by
It seems like hexdec($value[3]) / 15 can also be of type double. However, the property $a is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
345
                // Fallthrough
346
            case self::COLOR_HEX_3:
347
                $this->r = hexdec($value[0]) * 0x11;
0 ignored issues
show
Documentation Bug introduced by
It seems like hexdec($value[0]) * 17 can also be of type double. However, the property $r is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
348
                $this->g = hexdec($value[1]) * 0x11;
0 ignored issues
show
Documentation Bug introduced by
It seems like hexdec($value[1]) * 17 can also be of type double. However, the property $g is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
349
                $this->b = hexdec($value[2]) * 0x11;
0 ignored issues
show
Documentation Bug introduced by
It seems like hexdec($value[2]) * 17 can also be of type double. However, the property $b is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
350
                break;
351
            case self::COLOR_NAME:
352
                $value = self::$color_map[$value].'FF';
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $value. This often makes code more readable.
Loading history...
353
               // Fallthrough
354
            case self::COLOR_HEX_8:
355
                $this->a = hexdec(substr($value, 6, 2)) / 0xFF;
0 ignored issues
show
Documentation Bug introduced by
It seems like hexdec(substr($value, 6, 2)) / 255 can also be of type double. However, the property $a is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
356
                // Fallthrough
357
            case self::COLOR_HEX_6:
358
                $value = str_split($value, 2);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $value. This often makes code more readable.
Loading history...
359
                $this->r = hexdec($value[0]);
0 ignored issues
show
Documentation Bug introduced by
It seems like hexdec($value[0]) can also be of type double. However, the property $r is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
360
                $this->g = hexdec($value[1]);
0 ignored issues
show
Documentation Bug introduced by
It seems like hexdec($value[1]) can also be of type double. However, the property $g is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
361
                $this->b = hexdec($value[2]);
0 ignored issues
show
Documentation Bug introduced by
It seems like hexdec($value[2]) can also be of type double. However, the property $b is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
362
                break;
363
            case self::COLOR_RGBA:
364
                $this->a = $value[3];
365
                // Fallthrough
366
            case self::COLOR_RGB:
367
                list($this->r, $this->g, $this->b) = $value;
368
                break;
369
            case self::COLOR_HSLA:
370
                $this->a = $value[3];
371
                // Fallthrough
372
            case self::COLOR_HSL:
373
                if (min($value) < 0 || $value[0] > 360 || max($value[1], $value[2]) > 100) {
374
                    return;
375
                }
376
                $value = self::hslToRgb($value[0], $value[1], $value[2]);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $value. This often makes code more readable.
Loading history...
377
                list($this->r, $this->g, $this->b) = $value;
378
                break;
379
        }
380
381
        // If something has gone horribly wrong
382
        if ($this->r > 0xFF || $this->g > 0xFF || $this->b > 0xFF || $this->a > 1) {
383
            $this->variant = null;
384
        } else {
385
            $this->variant = $variant;
386
        }
387
    }
388
389
    /**
390
     * Turns HSL color to RGB. Black magic.
391
     *
392
     * @param float $h Hue
393
     * @param float $s Saturation
394
     * @param float $l Lightness
395
     *
396
     * @return array RGB array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use double[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
397
     */
398
    public static function hslToRgb($h, $s, $l)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $h. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
Comprehensibility introduced by
Avoid variables with short names like $s. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
Comprehensibility introduced by
Avoid variables with short names like $l. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
399
    {
400
        if (min($h, $s, $l) < 0) {
401
            throw new InvalidArgumentException('The parameters for hslToRgb should be no less than 0');
402
        } elseif ($h > 360 || max($s, $l) > 100) {
403
            throw new InvalidArgumentException('The parameters for hslToRgb should be no more than 360, 100, and 100 respectively');
404
        }
405
406
        $h /= 360;
407
        $s /= 100;
408
        $l /= 100;
409
410
        $m2 = ($l <= 0.5) ? $l * ($s + 1) : $l + $s - $l * $s;
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $m2. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
411
        $m1 = $l * 2 - $m2;
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $m1. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
412
413
        $out = array(
414
            round(self::hueToRgb($m1, $m2, $h + 1 / 3) * 255),
415
            round(self::hueToRgb($m1, $m2, $h) * 255),
416
            round(self::hueToRgb($m1, $m2, $h - 1 / 3) * 255),
417
        );
418
419
        if (max($out) > 255) {
420
            throw new LogicException('hslToRbg return value should not contain values above 255');
421
        } else {
422
            return $out;
423
        }
424
    }
425
426
    /**
427
     * Helper function for hslToRgb. Even blacker magic.
428
     *
429
     * @return float Color value
430
     */
431
    private static function hueToRgb($m1, $m2, $hue)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $m1. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
Comprehensibility introduced by
Avoid variables with short names like $m2. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
432
    {
433
        $hue = ($hue < 0) ? $hue + 1 : (($hue > 1) ? $hue - 1 : $hue);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $hue. This often makes code more readable.
Loading history...
434 View Code Duplication
        if ($hue * 6 < 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...
435
            return $m1 + ($m2 - $m1) * $hue * 6;
436
        }
437
        if ($hue * 2 < 1) {
438
            return $m2;
439
        }
440 View Code Duplication
        if ($hue * 3 < 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...
441
            return $m1 + ($m2 - $m1) * (0.66666 - $hue) * 6;
442
        }
443
444
        return $m1;
445
    }
446
447
    /**
448
     * Converts RGB to HSL. Color inversion of previous black magic is white magic?
449
     *
450
     * @param float $red   Red
451
     * @param float $green Green
452
     * @param float $blue  Blue
453
     *
454
     * @return array HSL array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<integer|double>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
455
     */
456
    public static function rgbToHsl($red, $green, $blue)
457
    {
458
        if (min($red, $green, $blue) < 0) {
459
            throw new InvalidArgumentException('The parameters for rgbToHsl should be no less than 0');
460
        } elseif (max($red, $green, $blue) > 255) {
461
            throw new InvalidArgumentException('The parameters for rgbToHsl should be no more than 255');
462
        }
463
464
        $clrMin = min($red, $green, $blue);
465
        $clrMax = max($red, $green, $blue);
466
        $deltaMax = $clrMax - $clrMin;
467
468
        $L = ($clrMax + $clrMin) / 510;
0 ignored issues
show
Coding Style introduced by
$L does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Comprehensibility introduced by
Avoid variables with short names like $L. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
469
470
        if (0 == $deltaMax) {
471
            $H = 0;
0 ignored issues
show
Coding Style introduced by
$H does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Comprehensibility introduced by
Avoid variables with short names like $H. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
472
            $S = 0;
0 ignored issues
show
Coding Style introduced by
$S does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Comprehensibility introduced by
Avoid variables with short names like $S. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
473
        } else {
474
            if (0.5 > $L) {
0 ignored issues
show
Coding Style introduced by
$L does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
475
                $S = $deltaMax / ($clrMax + $clrMin);
0 ignored issues
show
Coding Style introduced by
$S does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
476
            } else {
477
                $S = $deltaMax / (510 - $clrMax - $clrMin);
0 ignored issues
show
Coding Style introduced by
$S does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
478
            }
479
480
            if ($clrMax == $red) {
481
                $H = ($green - $blue) / (6.0 * $deltaMax);
0 ignored issues
show
Coding Style introduced by
$H does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
482
            } elseif ($clrMax == $green) {
483
                $H = 1 / 3 + ($blue - $red) / (6.0 * $deltaMax);
0 ignored issues
show
Coding Style introduced by
$H does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
484
            } else {
485
                $H = 2 / 3 + ($red - $green) / (6.0 * $deltaMax);
0 ignored issues
show
Coding Style introduced by
$H does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
486
            }
487
488
            if (0 > $H) {
0 ignored issues
show
Coding Style introduced by
$H does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
489
                $H += 1;
0 ignored issues
show
Coding Style introduced by
$H does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
490
            }
491
            if (1 < $H) {
0 ignored issues
show
Coding Style introduced by
$H does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
492
                $H -= 1;
0 ignored issues
show
Coding Style introduced by
$H does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
493
            }
494
        }
495
496
        return array(
497
            (round($H * 360) % 360 + 360) % 360,
0 ignored issues
show
Coding Style introduced by
$H does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
498
            round($S * 100),
0 ignored issues
show
Coding Style introduced by
$S does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
499
            round($L * 100),
0 ignored issues
show
Coding Style introduced by
$L does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
500
        );
501
    }
502
}
503