Completed
Branch 0.3.0 (b16461)
by Anton
04:03
created

Config   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0
Metric Value
wmc 16
lcom 1
cbo 0
dl 0
loc 72
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 8 2
A set() 0 11 4
B cast() 0 17 6
A get() 0 6 2
A __set() 0 4 1
A __get() 0 4 1
1
<?php
2
3
namespace {
4
5
	class Config {
6
7
		private $config = [], $values = [];
8
9
		# Add rule
10
11
		public function add(string $name, $default, callable $handler) {
12
13
			if ('' === $name) return;
14
15
			$this->config[$name] = $handler; $this->values[$name] = null;
16
17
			$this->set([$name => $default]);
18
		}
19
20
		# Set array of values
21
22
		public function set(array $data) {
23
24
			foreach ($data as $name => $value) {
25
26
				if (!isset($this->config[$name])) continue;
27
28
				try { $this->values[$name] = $this->config[$name]($value); }
29
30
				catch (\TypeError $e) {}
1 ignored issue
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
Bug introduced by
The class TypeError does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
31
			}
32
		}
33
34
		# Cast array of values
35
36
		public function cast(array $data, bool $process_all = false) {
37
38
			$cast = [];
39
40
			foreach ($this->config as $name => $handler) {
41
42
				if (!($isset = isset($data[$name])) && !$process_all) continue;
43
44
				try { $cast[$name] = ($isset ? $handler($data[$name]) : $this->values[$name]); }
45
46
				catch (\TypeError $e) { $cast[$name] = $this->values[$name]; }
1 ignored issue
show
Bug introduced by
The class TypeError does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
47
			}
48
49
			# ------------------------
50
51
			return $cast;
52
		}
53
54
		# Get value or array of values
55
56
		public function get(string $name = null) {
57
58
			if (null === $name) return $this->values;
59
60
			return ($this->values[$name] ?? null);
61
		}
62
63
		# Setter
64
65
		public function __set(string $name, $value) {
66
67
			return $this->set([$name => $value]);
68
		}
69
70
		# Getter
71
72
		public function __get(string $name) {
73
74
			return $this->get($name);
75
		}
76
	}
77
}
78