Completed
Push — master ( 699f98...edb5e2 )
by Nicholas
03:56
created

regulate::percent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
4
namespace projectcleverweb\color;
5
6
class regulate {
7
	
8
	public static function percent(float &$value) {
9
		$value = abs($value) % 101;
10
	}
11
	
12
	public static function rgb(int &$value) {
13
		$value = abs($value) % 256;
14
	}
15
	
16
	public static function rgb_array(array &$rgb_array) {
17
		static::rgb($rgb_array['r']);
18
		static::rgb($rgb_array['g']);
19
		static::rgb($rgb_array['b']);
20
	}
21
	
22
	public static function hsl(float &$value, string $offset) {
23
		if (strtolower($offset) == 'h') {
24
			$value = abs($value) % 360;
25
		}
26
		static::percent($value);
27
	}
28
	
29
	public static function hsl_array(array &$hsl_array) {
30
		static::hsl($hsl_array['h'], 'h');
31
		static::hsl($hsl_array['s'], 's');
32
		static::hsl($hsl_array['l'], 'l');
33
	}
34
	
35
	public static function cmyk(float &$value) {
36
		static::percent($value);
37
	}
38
	
39
	public static function cmyk_array(array &$cmyk_array) {
40
		static::cmyk($cmyk_array['c']);
41
		static::cmyk($cmyk_array['m']);
42
		static::cmyk($cmyk_array['y']);
43
		static::cmyk($cmyk_array['k']);
44
	}
45
	
46
	public static function hex(string &$str) {
0 ignored issues
show
Unused Code introduced by
The parameter $str is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
47
		static::_validate_hex_str($color);
48
		static::_strip_hash($color);
49
		static::_expand_shorthand($color);
50
	}
51
	
52
	public static function hex_int(int &$value) {
53
		$value = abs($value) % 0x1FFFFFF;
54
	}
55
	
56
	
57
	protected static function _strip_hash(&$hex) {
58
		if (is_string($hex) && substr($hex, 0 ,1) == '#') {
59
			$hex = substr($hex, 1);
60
		}
61
	}
62
	
63
	protected static function _expand_shorthand(string &$hex_str) {
64
		if (strlen($hex) === 3) {
65
			$r = $hex[0];
0 ignored issues
show
Bug introduced by
The variable $hex does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
66
			$g = $hex[1];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
67
			$b = $hex[2];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
68
			$hex_str = $r.$r.$g.$g.$b.$b;
69
		}
70
	}
71
	
72
	protected static function _validate_hex_str(&$hex_str) {
73
		if (is_string($hex_str) && preg_match('/\A#?(?:[0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})\Z/i', $hex_str)) {
74
			return;
75
		}
76
		// Error - Force color and trigger notice
77
		$hex_str = '000000';
78
		// [todo] Trigger Error
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
79
	}
80
	
81
}
82