Completed
Push — master ( e3e926...51577e )
by Nicholas
02:23
created

modify::hsl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 12
ccs 0
cts 10
cp 0
rs 9.4285
cc 2
eloc 10
nc 2
nop 5
crap 6
1
<?php
2
3
4
namespace projectcleverweb\color;
5
6
class modify {
7
	
8
	public static function rgb(data $data, string $scope, float $adjustment, bool $as_percentage, bool $set_absolute) {
9
		$current         = array_combine(['red', 'green', 'blue'], $data->rgb);
0 ignored issues
show
Bug introduced by
The property rgb cannot be accessed from this context as it is declared private in class projectcleverweb\color\data.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
10
		$scope           = strtolower($scope); // [todo] Validate scope
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...
11
		$adjustment      = max(min($adjustment, 255), 0 - 255); // Force valid range
12
		$adjustment      = static::_convert_to_exact($adjustment, 255, $as_percentage);
13
		$current[$scope] = static::_convert_to_abs($current[$scope], $adjustment, $set_absolute, 0, 255);
14
		return static::regenerate_rgb($data, $current);
15
	}
16
	
17
	public static function hsl(data $data, string $scope, float $adjustment, bool $as_percentage, bool $set_absolute) {
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...
18
		$current = array_combine(['hue', 'saturation', 'light'], $data->hsl->hsl);
0 ignored issues
show
Bug introduced by
The property hsl cannot be accessed from this context as it is declared private in class projectcleverweb\color\data.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
Bug introduced by
The property hsl cannot be accessed from this context as it is declared private in class projectcleverweb\color\hsl.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
19
		$scope   = strtolower($scope); // [todo] Validate scope
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...
20
		$max     = 100;
21
		if ($scope == 'hue') {
22
			$max = 359;
23
		}
24
		$adjustment = max(min($adjustment, $max), 0 - $max); // Force valid range
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 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...
25
		$adjustment = static::_convert_to_exact($adjustment, $max, $as_percentage);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 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...
26
		$current[$scope] = static::_convert_to_abs($current[$scope], $adjustment, $set_absolute, 0, $max);
27
		return static::regenerate_hsl($data, $current);
0 ignored issues
show
Bug introduced by
The method regenerate_hsl() does not seem to exist on object<projectcleverweb\color\modify>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
28
	}
29
	
30
	protected static function _convert_to_exact(float $adjustment, float $max, bool $as_percentage) {
31
		if ($as_percentage) {
32
			return ($adjustment / 100) * abs($max);
33
		}
34
		return $adjustment;
35
	}
36
	
37
	protected static function _convert_to_abs(float $current, float $adjustment, bool $set_absolute, float $min = 0, float $max = PHP_INT_MAX) {
38
		if ($set_absolute) {
39
			return abs($adjustment);
40
		}
41
		return max(min($current + $adjustment, $max), $min);
42
	}
43
	
44
	protected static function regenerate_rgb(data $data, array $update) {
45
		$data->import_rgb([
46
			'r' => $update['red'],
47
			'g' => $update['green'],
48
			'b' => $update['blue']
49
		]);
50
	}
51
}
52