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

rgb   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 95
Duplicated Lines 7.37 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 7
loc 95
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A to_rgb() 0 3 1
A to_hex() 0 8 1
A to_cmyk() 0 16 1
B to_hsl() 7 26 3
A to_hsb() 0 22 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
namespace projectcleverweb\color\convert;
4
5
class rgb implements \projectcleverweb\color\interfaces\converter {
6
	use \projectcleverweb\color\traits\convert\meta;
7
	
8
	protected static $valid_keys = array(
9
		'r',
10
		'g',
11
		'b'
12
	);
13
	
14
	protected static $default_value = array(
15
		'r' => 0,
16
		'g' => 0,
17
		'b' => 0
18
	);
19
	
20
	public static function to_rgb($input) :array {
21
		return static::_validate_array_input($input);
22
	}
23
	
24
	public static function to_hex($input) :string {
25
		$rgb = static::_validate_array_input($input);
26
		return strtoupper(
27
			str_pad(dechex($rgb['r']), 2, '0', STR_PAD_LEFT)
28
			.str_pad(dechex($rgb['g']), 2, '0', STR_PAD_LEFT)
29
			.str_pad(dechex($rgb['b']), 2, '0', STR_PAD_LEFT)
30
		);
31
	}
32
	
33
	public static function to_cmyk($input) :array {
34
		$rgb = static::_validate_array_input($input);
35
		$c  = (255 - $rgb['r']) / 255 * 100;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 2 spaces

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...
36
		$m  = (255 - $rgb['g']) / 255 * 100;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 2 spaces

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...
37
		$y  = (255 - $rgb['b']) / 255 * 100;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 2 spaces

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...
38
		$k  = min(array($c,$m,$y));
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 2 spaces

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...
39
		$c -= $k;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 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...
40
		$m -= $k;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 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...
41
		$y -= $k;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 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...
42
		return [
43
			'c' => round($c),
44
			'm' => round($m),
45
			'y' => round($y),
46
			'k' => round($k)
47
		];
48
	}
49
	
50
	public static function to_hsl($input) :array {
51
		$rgb = static::_validate_array_input($input);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 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...
52
		$r     = $rgb['r'] / 255;
53
		$g     = $rgb['g'] / 255;
54
		$b     = $rgb['b'] / 255;
55
		$min   = min($r, $g, $b);
56
		$max   = max($r, $g, $b);
57
		$delta = $max - $min;
58
		$h     = 0;
59
		$s     = 0;
60
		$l     = ($max + $min) / 2;
61
		
62 View Code Duplication
		if ($max != $min) {
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...
63
			$s = $delta / ($max + $min);
64
			if ($l >= 0.5) {
65
				$s = $delta / (2 - $max - $min);
66
			}
67
			static::_rgbhsl_hue($h, $r, $g, $b, $max, $delta);
68
		}
69
		
70
		return [
71
			'h' => $h * 360,
72
			's' => $s * 100,
73
			'l' => $l * 100
74
		];
75
	}
76
	
77
	public static function to_hsb($input) :array {
78
		$rgb = static::_validate_array_input($input);
79
		$r   = $rgb['r'] / 255;
80
		$g   = $rgb['g'] / 255;
81
		$b   = $rgb['b'] / 255;
82
		
83
		$max = max($r, $g, $b);
84
		$min = min($r, $g, $b);
85
		$v   = $max;
86
		$d   = $max - $min;
87
		$s   = \projectcleverweb\color\regulate::div($d, $max);
88
		$h   = 0; // achromatic
89
		if ($max != $min) {
90
			static::_rgbhsl_hue($h, $r, $g, $b, $max, $d);
91
		}
92
		
93
		$h = $h * 360;
94
		$s = $s * 100;
95
		$v = $v * 100;
96
		
97
		return ['h' => $h, 's' => $s, 'b' => $v];
98
	}
99
}
100