regulate   A
last analyzed

Complexity

Total Complexity 28

Size/Duplication

Total Lines 228
Duplicated Lines 7.89 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 28
lcom 2
cbo 1
dl 18
loc 228
ccs 84
cts 84
cp 1
rs 10
c 0
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
A hsb_array() 6 6 1
A alpha() 0 3 1
A percent() 0 3 1
A rgb() 0 3 1
A rgb_array() 6 6 1
A hsl() 0 7 2
A hsl_array() 6 6 1
A cmyk() 0 3 1
A cmyk_array() 0 7 1
A hex() 0 5 1
A hex_int() 0 3 1
A _strip_hash() 0 5 3
A _expand_shorthand() 0 8 2
A _validate_hex_str() 0 14 3
A standardize_array() 0 7 4
A max() 0 7 2
A div() 0 6 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * I/O Regulator Class
4
 * ===================
5
 * Allows you to reasonably regulate method/function input and output for
6
 * various types of color values and color arrays.
7
 */
8
9
namespace projectcleverweb\color;
10
11
/**
12
 * I/O Regulator Class
13
 * ===================
14
 * Allows you to reasonably regulate method/function input and output for
15
 * various types of color values and color arrays.
16
 */
17
class regulate {
18
	
19
	/**
20
	 * Forces a float to be within the range of 0 and 100.
21
	 * 
22
	 * @param  float &$value The value to modify
23
	 * @return void
24
	 */
25 35
	public static function percent(float &$value) {
26 35
		$value = static::max(abs($value), 100);
27 35
	}
28
	
29
	/**
30
	 * Forces a float to be within the range of 0 and 255.
31
	 * 
32
	 * @param  float &$value The value to modify
33
	 * @return void
34
	 */
35 12
	public static function rgb(int &$value) {
36 12
		$value = static::max(abs($value), 255);
37 12
	}
38
	
39
	/**
40
	 * Forces and RGB array to have specific offsets, and max values.
41
	 * 
42
	 * @param  array &$rgb_array The RGB array to modify
43
	 * @return void
44
	 */
45 11 View Code Duplication
	public static function rgb_array(array &$rgb_array) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
46 11
		static::standardize_array($rgb_array, ['r', 'g', 'b']);
47 11
		static::rgb($rgb_array['r']);
48 11
		static::rgb($rgb_array['g']);
49 11
		static::rgb($rgb_array['b']);
50 11
	}
51
	
52
	/**
53
	 * Forces a float to be within the range of 0 and 100 or if the offset is
54
	 * 'h' 0 and 359.
55
	 * 
56
	 * @param  float &$value  The value to modify
57
	 * @param  string $offset The offset that is being modified
58
	 * @return void
59
	 */
60 29
	public static function hsl(float &$value, string $offset) {
61 29
		if (strtolower($offset) == 'h') {
62 29
			$value = static::max(abs($value), 359);
63
		} else {
64 29
			static::percent($value);
65
		}
66 29
	}
67
	
68
	/**
69
	 * Forces and HSL array to have specific offsets, and max values.
70
	 * 
71
	 * @param  array &$hsl_array The HSL array to modify
72
	 * @return void
73
	 */
74 25 View Code Duplication
	public static function hsl_array(array &$hsl_array) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
75 25
		static::standardize_array($hsl_array, ['h', 's', 'l']);
76 25
		static::hsl($hsl_array['h'], 'h');
77 25
		static::hsl($hsl_array['s'], 's');
78 25
		static::hsl($hsl_array['l'], 'l');
79 25
	}
80
	
81
	/**
82
	 * Forces and HSL array to have specific offsets, and max values.
83
	 * 
84
	 * @param  array &$hsl_array The HSL array to modify
85
	 * @return void
86
	 */
87 5 View Code Duplication
	public static function hsb_array(array &$hsl_array) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
88 5
		static::standardize_array($hsl_array, ['h', 's', 'b']);
89 5
		static::hsl($hsl_array['h'], 'h');
90 5
		static::hsl($hsl_array['s'], 's');
91 5
		static::hsl($hsl_array['b'], 'b');
92 5
	}
93
	
94
	/**
95
	 * Forces a float to be within the range of 0 and 100.
96
	 * 
97
	 * @param  float &$value The value to modify
98
	 * @return void
99
	 */
100 6
	public static function cmyk(float &$value) {
101 6
		static::percent($value);
102 6
	}
103
	
104
	
105
	/**
106
	 * Forces a float to be within the range of 0 and 100.
107
	 * 
108
	 * @param  float &$value The value to modify
109
	 * @return void
110
	 */
111 1
	public static function alpha(float &$value) {
112 1
		static::percent($value);
113 1
	}
114
	
115
	/**
116
	 * Forces and CMYK array to have specific offsets, and max values.
117
	 * 
118
	 * @param  array &$cmyk_array The CMYK array to modify
119
	 * @return void
120
	 */
121 5
	public static function cmyk_array(array &$cmyk_array) {
122 5
		static::standardize_array($cmyk_array, ['c', 'm', 'y', 'k']);
123 5
		static::cmyk($cmyk_array['c']);
124 5
		static::cmyk($cmyk_array['m']);
125 5
		static::cmyk($cmyk_array['y']);
126 5
		static::cmyk($cmyk_array['k']);
127 5
	}
128
	
129
	/**
130
	 * Forces hexadecimal strings to be valid, without a "#", and not to be
131
	 * shorthand.
132
	 * 
133
	 * @param  string &$color The color string to modify
134
	 * @return void
135
	 */
136 7
	public static function hex(string &$color) {
137 7
		static::_validate_hex_str($color);
138 7
		static::_strip_hash($color);
139 7
		static::_expand_shorthand($color);
140 7
	}
141
	
142
	/**
143
	 * Forces hexadecimal integers to be within the range of 0x0 and 0xFFFFFF.
144
	 * 
145
	 * @param  int  &$value The value to modify
146
	 * @return void
147
	 */
148 3
	public static function hex_int(int &$value) {
149 3
		$value = static::max(abs($value), 0xFFFFFF);
150 3
	}
151
	
152
	/**
153
	 * Strips the hash mark (#) off the beginning of a sting if it has one.
154
	 * 
155
	 * @param  string &$hex The hex string
156
	 * @return void
157
	 */
158 8
	public static function _strip_hash(string &$hex) {
159 8
		if (is_string($hex) && substr($hex, 0 ,1) == '#') {
160 2
			$hex = substr($hex, 1);
161
		}
162 8
	}
163
	
164
	/**
165
	 * Expand a hex strings in short notation (e.g. 000 => 000000)
166
	 * 
167
	 * @param  string &$hex_str The hex string to modify
168
	 * @return void
169
	 */
170 8
	public static function _expand_shorthand(string &$hex_str) {
171 8
		if (strlen($hex_str) === 3) {
172 2
			$r = $hex_str[0];
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...
173 2
			$g = $hex_str[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...
174 2
			$b = $hex_str[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...
175 2
			$hex_str = $r.$r.$g.$g.$b.$b;
176
		}
177 8
	}
178
	
179
	/**
180
	 * Verify that the hex string is actually a hex string. If not force it to
181
	 * '000000'
182
	 * 
183
	 * @param  string &$hex_str The hex string to modify
184
	 * @return void
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
185
	 */
186 8
	public static function _validate_hex_str(string &$hex_str, bool $check_only = FALSE) :bool {
187 8
		if (preg_match('/\A#?(?:[0-9a-f]{3}|[0-9a-f]{6})\Z/i', $hex_str)) {
188 8
			return TRUE;
189
		}
190 2
		if (!$check_only) {
191 2
			error::trigger(error::INVALID_ARGUMENT, sprintf(
192 2
				'The input of %s::%s() was not a valid hex string, forcing value to 000000',
193 2
				__CLASS__,
194
				__FUNCTION__
195 2
			));
196 2
			$hex_str = '000000';
197
		}
198
		return FALSE;
199
	}
200
	
201
	/**
202
	 * Force an array to have specific lower-case keys. If the array is an indexed
203
	 * array and the keys are provided, convert it to an associative array.
204
	 * 
205 41
	 * @param  array &$array The array to be modified
206 41
	 * @return void
207 2
	 */
208
	public static function standardize_array(array &$array, array $keys = []) {
209 40
		if (!empty($keys) && array_keys($array) === range(0, count($array) - 1) && count($array) == count($keys)) {
210
			$array = array_combine($keys, $array);
211 41
		} else {
212
			$array = array_change_key_case($array) + array_fill_keys($keys, 0);
213
		}
214
	}
215
	
216
	/**
217
	 * Absolute and divide any number so it fits between 0 and $max
218
	 * 
219
	 * @param  float  $number The original number
220 46
	 * @param  float  $max    The maximum value $number should be
221 46
	 * @return float          The resulting number as a float
222 46
	 */
223 16
	public static function max(float $number, float $max) :float {
224
		$max = abs($max);
225 46
		if ($number > $max) {
226
			$number -= floor($number / ($max + 1)) * ($max + 1);
227
		}
228
		return $number;
229
	}
230
	
231
	/**
232
	 * Simple dividing method to handle division by 0
233
	 * 
234
	 * @param  float  $number  The number to divide
235 15
	 * @param  float  $divisor The number to divide by
236 15
	 * @return float           The result
237 5
	 */
238
	public static function div(float $number, float $divisor) :float {
239 15
		if ($divisor == 0) {
240
			return 0;
241
		}
242
		return $number / $divisor;
243
	}
244
}
245