main_peripheral   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 2
dl 0
loc 91
ccs 0
cts 34
cp 0
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __clone() 0 4 1
A serialize() 0 3 1
A jsonSerialize() 0 3 1
A get_scheme() 0 8 2
A _scheme() 0 12 2
A unserialize() 0 5 1
A cache() 0 3 1
A reset_cache() 0 3 1
1
<?php
2
/**
3
 * Handles the "extra" functionality for the main class.
4
 */
5
6
namespace projectcleverweb\color;
7
8
/**
9
 * Handles the "extra" functionality for the main class.
10
 */
11
abstract class main_peripheral implements \Serializable, \JsonSerializable {
12
	
13
	/**
14
	 * Makes sure cloning works as expected
15
	 * 
16
	 * @return void
17
	 */
18
	public function __clone() {
19
		$this->color = clone $this->color;
20
		$this->cache = clone $this->cache;
21
	}
22
	
23
	/**
24
	 * Custom serialize function
25
	 * 
26
	 * @return string This instance serialized as an RGB string
27
	 */
28
	public function serialize() :string {
29
		return $this->color->serialize();
30
	}
31
	
32
	/**
33
	 * Custom unserialize function
34
	 * 
35
	 * @param  string $serialized This instance serialized as an RGB string
36
	 * @return void
37
	 */
38
	public function unserialize($serialized) {
39
		$unserialized = (array) json_decode((string) $serialized);
40
		regulate::rgb_array($unserialized);
41
		$this->set($unserialized, 'rgb');
42
	}
43
	
44
	/**
45
	 * Custom JSON serialize function
46
	 * 
47
	 * @return string This instance serialized as an JSON RGB string
0 ignored issues
show
Documentation introduced by
Should the return type not be array? Also, consider making the array more specific, something like array<String>, or String[].

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.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
48
	 */
49
	public function jsonSerialize() :array {
50
		return $this->color->jsonSerialize();
51
	}
52
	
53
	protected function get_scheme(string $scheme_name, string $return_type = 'hex', $scheme_class) :array {
0 ignored issues
show
Coding Style introduced by
Parameters which have default values should be placed at the end.

If you place a parameter with a default value before a parameter with a default value, the default value of the first parameter will never be used as it will always need to be passed anyway:

// $a must always be passed; it's default value is never used.
function someFunction($a = 5, $b) { }
Loading history...
54
		if (!is_null($cached = $this->cache->get(get_class($scheme_class).'_'.$scheme_name.'_'.$return_type, $this->hex()))) {
55
			return $cached;
56
		}
57
		$result = static::_scheme($scheme_name, [$scheme_class, strtolower($return_type)], $this->hsl(3));
58
		$this->cache->set(get_class($scheme_class).'_'.$scheme_name.'_'.$return_type, $this->hex(), $result);
59
		return $result;
60
	}
61
	
62
	/**
63
	 * Handles scheme generator callbacks
64
	 * 
65
	 * @param  string $scheme_name The name of the scheme algorithm to use
66
	 * @param  string $callback    The return type callback
0 ignored issues
show
Documentation introduced by
Should the type for parameter $callback not be array? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
67
	 * @param  array  $hsl         The base color as an HSL array
68
	 * @return array               The resulting scheme in the proper format, OR an empty array on failure.
69
	 */
70
	protected static function _scheme(string $scheme_name, array $callback, array $hsl) :array {
71
		if (is_callable($callback)) {
72
			return call_user_func($callback, $hsl['h'], $hsl['s'], $hsl['l'], $scheme_name);
73
		}
74
		error::trigger(error::INVALID_ARGUMENT, sprintf(
75
			'The $callback "%s" is not a valid callback',
76
			print_r($callback, 1),
77
			__CLASS__,
78
			__FUNCTION__
79
		));
80
		return [];
81
	}
82
	
83
	/**
84
	 * Set whether or not caching should be active.
85
	 * 
86
	 * @param  bool $active If TRUE caching is turned on, otherwise cashing is turned off.
87
	 * @return void
88
	 */
89
	public function cache(bool $active = TRUE) {
90
		$this->cache->active = $active;
91
	}
92
	
93
	/**
94
	 * Reset the cache for this instance
95
	 * 
96
	 * @return void
97
	 */
98
	public function reset_cache() {
99
		$this->cache->reset();
100
	}
101
}
102