Test Failed
Push — master ( 2ae2a8...2e5515 )
by Nicholas
04:58
created

meta   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 155
Duplicated Lines 26.45 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 1
dl 41
loc 155
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A error() 0 3 1
B _validate_array_input() 0 24 4
A _validate_hex_input() 0 12 3
A _validate_hex_input_str() 0 11 3
A _expand_hex_str() 0 6 2
A _rgbhsl_delta_rgb() 0 3 1
A _rgbhsl_hue() 15 15 4
A _hslrgb_low() 15 15 3
A _hslrgb_high() 11 11 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
 * Conversion Class
4
 * ================
5
 * Responsible for all the conversion method between Hex, RGB, HSL, HSB, and CMYK
6
 */
7
8
namespace projectcleverweb\color\traits\convert;
9
10
/**
11
 * Conversion Class
12
 * ================
13
 * Responsible for all the conversion method between Hex, RGB, HSL, HSB, and CMYK
14
 */
15
trait meta {
16
	
17
	// protected static $valid_keys = array();
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% 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...
18
	// protected static $default_value;
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
19
	
20
	protected static function error($message) {
21
		return \projectcleverweb\color\error::call($message);
22
	}
23
	
24
	protected static function _validate_array_input($input) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
25
		if (!is_array($input)) {
26
			static::error(sprintf(
27
				'%s input must be of type "array", input was of type "%s"',
28
				strtoupper(implode('', static::$valid_keys)),
29
				gettype($input)
30
			));
31
			$input = static::$default_value;
32
		}
33
		$input = array_change_key_case($input);
34
		$array = static::$default_value;
35
		foreach (static::$valid_keys as $key) {
36
			if (!isset($input[$key])) {
37
				static::error(sprintf(
38
					'Key "%s" missing from %s array',
39
					$key,
40
					strtoupper(implode('', static::$valid_keys))
41
				));
42
				$input[$key] = $array[$key];
43
			}
44
			$array[$key] = (float) filter_var($input[$key], FILTER_VALIDATE_FLOAT);
45
		}
46
		return $array;
47
	}
48
	
49
	public static function _validate_hex_input($input) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
50
		if (is_int($input)) {
51
			return str_pad(dechex($input % 16777216), 6, '0', STR_PAD_LEFT);
52
		} elseif (is_string($input)) {
53
			return static::_validate_hex_input_str($input);
54
		}
55
		static::error(sprintf(
56
			'hex input must be of type "string" or "integer", input was of type "%s"',
57
			gettype($input)
58
		));
59
		return static::$default_value;
60
	}
61
	
62
	protected static function _validate_hex_input_str(string $input) :string {
63
		$input = strtolower($input);
64
		if ($input[0] == '#') {
65
			$input = substr($input, 1);
66
		}
67
		if (!preg_match('([0-9a-f]{6}|[0-9a-f]{3})', $input)) {
68
			static::error('invalid hex string input');
69
			return static::$default_value;
70
		}
71
		return static::_expand_hex_str($input);
72
	}
73
	
74
	protected static function _expand_hex_str(string $input) : string {
75
		if (strlen($input) == 3) {
76
			$input = $input[0].$input[0].$input[1].$input[1].$input[2].$input[2];
77
		}
78
		return $input;
79
	}
80
	
81
	/**
82
	 * Color delta algorithm
83
	 * 
84
	 * @param  float $rgb   The R, G, or B value
85
	 * @param  float $max   The max RGB value
86
	 * @param  float $delta The delta value ($max - $min)
87
	 * @return float        The color delta
88
	 */
89
	protected static function _rgbhsl_delta_rgb(float $rgb, float $max, float $delta) {
90
		return ((($max - $rgb) / 6) + ($delta / 2)) / $delta;
91
	}
92
	
93
	/**
94
	 * Calculate the hue as a percentage from RGB
95
	 * 
96
	 * @param  float &$h    The variable to modify as hue
97
	 * @param  float $r     The red value as a percentage
98
	 * @param  float $g     The green value as a percentage
99
	 * @param  float $b     The blue value as a percentage
100
	 * @param  float $max   The max RGB value
101
	 * @param  float $delta The delta value ($max - $min)
102
	 * @return void
103
	 */
104 View Code Duplication
	protected static function _rgbhsl_hue(float &$h, float $r, float $g, float $b, float $max, float $delta) {
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...
105
		$delta_r = static::_rgbhsl_delta_rgb($r, $max, $delta);
106
		$delta_g = static::_rgbhsl_delta_rgb($g, $max, $delta);
107
		$delta_b = static::_rgbhsl_delta_rgb($b, $max, $delta);
108
		
109
		$h = (2 / 3) + $delta_g - $delta_r;
110
		if ($r == $max) {
111
			$h = $delta_b - $delta_g;
112
		} elseif ($g == $max) {
113
			$h = (1 / 3) + $delta_r - $delta_b;
114
		}
115
		if ($h < 0) {
116
			$h++;
117
		}
118
	}
119
	
120
	/**
121
	 * Handle low hue values
122
	 * 
123
	 * @param  float  &$r The red value to modify
124
	 * @param  float  &$g The green value to modify
125
	 * @param  float  &$b The blue value to modify
126
	 * @param  float  $c  Potential R, G, or B value
127
	 * @param  float  $x  Potential R, G, or B value
128
	 * @param  float  $h  The hue
129
	 * @return void
130
	 */
131 View Code Duplication
	protected static function _hslrgb_low(float &$r, float &$g, float &$b, float $c, float $x, float $h) {
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...
132
		if ($h < 60) {
133
			$r = $c;
134
			$g = $x;
135
			$b = 0;
136
		} elseif ($h < 120) {
137
			$r = $x;
138
			$g = $c;
139
			$b = 0;
140
		} else {
141
			$r = 0;
142
			$g = $c;
143
			$b = $x;
144
		}
145
	}
146
	
147
	/**
148
	 * Handle high hue values
149
	 * 
150
	 * @param  float  &$r The red value to modify
151
	 * @param  float  &$g The green value to modify
152
	 * @param  float  &$b The blue value to modify
153
	 * @param  float  $c  Potential R, G, or B value
154
	 * @param  float  $x  Potential R, G, or B value
155
	 * @param  float  $h  The hue
156
	 * @return void
157
	 */
158 View Code Duplication
	protected static function _hslrgb_high(float &$r, float &$g, float &$b, float $c, float $x, float $h) {
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...
159
		if ($h < 240) {
160
			$r = 0;
161
			$g = $x;
162
			$b = $c;
163
		} else {
164
			$r = $x;
165
			$g = 0;
166
			$b = $c;
167
		}
168
	}
169
}
170