Completed
Pull Request — master (#5)
by
unknown
02:59
created

ColorValue   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 43
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B getCompiled() 0 21 7
A initializeFromOldFormat() 0 10 2
1
<?php
2
namespace LesserPhp\Compiler\Value;
3
4
/**
5
 * lesserphp
6
 * https://www.maswaba.de/lesserphp
7
 *
8
 * LESS CSS compiler, adapted from http://lesscss.org
9
 *
10
 * Copyright 2013, Leaf Corcoran <[email protected]>
11
 * Copyright 2016, Marcus Schwarz <[email protected]>
12
 * Copyright 2017, Stefan Pöhner <[email protected]>
13
 * Licensed under MIT or GPLv3, see LICENSE
14
 *
15
 * @package LesserPhp
16
 */
17
18
class ColorValue extends AbstractValue
19
{
20
	private $red, $green, $blue, $alpha;
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
21
22
	/**
23
	 * @inheritdoc
24
	 */
25 13
	public function getCompiled()
26
	{
27 13
		$red   = round($this->red);
28 13
		$green = round($this->green);
29 13
		$blue  = round($this->blue);
30
31 13
		if ($this->alpha !== null && $this->alpha != 1) {
32 4
			return 'rgba('.$red.','.$green.','.$blue.','.$this->alpha.')';
33
		}
34
35 12
		$hex = sprintf("#%02x%02x%02x", $red, $green, $blue);
36
37 12
		if ($this->options['compressColors']) {
38
			// Converting hex color to short notation (e.g. #003399 to #039)
39 3
			if ($hex[1] === $hex[2] && $hex[3] === $hex[4] && $hex[5] === $hex[6]) {
40 3
				$hex = '#'.$hex[1].$hex[3].$hex[5];
41
			}
42
		}
43
44 12
		return $hex;
45
	}
46
47
	/**
48
	 * @inheritdoc
49
	 */
50 13
	public function initializeFromOldFormat(array $value)
51
	{
52 13
		$this->red   = $value[1];
53 13
		$this->green = $value[2];
54 13
		$this->blue  = $value[3];
55
56 13
		if (isset($value[4])) {
57 6
			$this->alpha = $value[4];
58
		}
59
	}
60
}