ConstantsPart   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 2
dl 0
loc 139
ccs 46
cts 46
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A initConstants() 0 3 1
A setConstants() 0 18 3
A setConstant() 0 13 2
A removeConstant() 0 13 3
A hasConstant() 0 7 2
A getConstant() 0 11 3
A getConstants() 0 3 1
A getConstantNames() 0 3 1
A clearConstants() 0 5 1
1
<?php
2
declare(strict_types=1);
3
4
namespace gossi\codegen\model\parts;
5
6
use gossi\codegen\model\PhpConstant;
7
use phootwork\collection\Map;
8
use phootwork\collection\Set;
9
10
/**
11
 * Constants part
12
 *
13
 * For all models that can contain constants
14
 *
15
 * @author Thomas Gossmann
16
 */
17
trait ConstantsPart {
18
19
	/** @var Map */
20
	private $constants;
21
22 52
	private function initConstants() {
23 52
		$this->constants = new Map();
24 52
	}
25
26
	/**
27
	 * Sets a collection of constants
28
	 *
29
	 * @param array|PhpConstant[] $constants
30
	 * @return $this
31
	 */
32 1
	public function setConstants(array $constants) {
33 1
		$normalizedConstants = [];
34 1
		foreach ($constants as $name => $value) {
35 1
			if ($value instanceof PhpConstant) {
36 1
				$name = $value->getName();
37
			} else {
38 1
				$constValue = $value;
39 1
				$value = new PhpConstant($name);
40 1
				$value->setValue($constValue);
41
			}
42
43 1
			$normalizedConstants[$name] = $value;
44
		}
45
46 1
		$this->constants->setAll($normalizedConstants);
47
48 1
		return $this;
49
	}
50
51
	/**
52
	 * Adds a constant
53
	 *
54
	 * @param string|PhpConstant $nameOrConstant constant name or instance
55
	 * @param string $value
56
	 * @param bool $isExpression whether the value is an expression or not
57
	 * @return $this
58
	 */
59 8
	public function setConstant($nameOrConstant, string $value = null, bool $isExpression = false) {
60 8
		if ($nameOrConstant instanceof PhpConstant) {
61 7
			$name = $nameOrConstant->getName();
62 7
			$constant = $nameOrConstant;
63
		} else {
64 2
			$name = $nameOrConstant;
65 2
			$constant = new PhpConstant($nameOrConstant, $value, $isExpression);
66
		}
67
68 8
		$this->constants->set($name, $constant);
69
70 8
		return $this;
71
	}
72
73
	/**
74
	 * Removes a constant
75
	 *
76
	 * @param string|PhpConstant $nameOrConstant constant name
77
	 * @throws \InvalidArgumentException If the constant cannot be found
78
	 * @return $this
79
	 */
80 2
	public function removeConstant($nameOrConstant) {
81 2
		if ($nameOrConstant instanceof PhpConstant) {
82 1
			$nameOrConstant = $nameOrConstant->getName();
83
		}
84
85 2
		if (!$this->constants->has($nameOrConstant)) {
86 1
			throw new \InvalidArgumentException(sprintf('The constant "%s" does not exist.', $nameOrConstant));
87
		}
88
89 1
		$this->constants->remove($nameOrConstant);
90
91 1
		return $this;
92
	}
93
94
	/**
95
	 * Checks whether a constant exists
96
	 *
97
	 * @param string|PhpConstant $nameOrConstant
98
	 * @return bool
99
	 */
100 2
	public function hasConstant($nameOrConstant): bool {
101 2
		if ($nameOrConstant instanceof PhpConstant) {
102 1
			$nameOrConstant = $nameOrConstant->getName();
103
		}
104
105 2
		return $this->constants->has($nameOrConstant);
106
	}
107
108
	/**
109
	 * Returns a constant.
110
	 *
111
	 * @param string|PhpConstant $nameOrConstant
112
	 * @throws \InvalidArgumentException If the constant cannot be found
113
	 * @return PhpConstant
114
	 */
115 6
	public function getConstant($nameOrConstant): PhpConstant {
116 6
		if ($nameOrConstant instanceof PhpConstant) {
117 1
			$nameOrConstant = $nameOrConstant->getName();
118
		}
119
120 6
		if (!$this->constants->has($nameOrConstant)) {
121 2
			throw new \InvalidArgumentException(sprintf('The constant "%s" does not exist.', $nameOrConstant));
122
		}
123
124 5
		return $this->constants->get($nameOrConstant);
125
	}
126
127
	/**
128
	 * Returns all constants
129
	 *
130
	 * @return Map
131
	 */
132 16
	public function getConstants(): Map {
133 16
		return $this->constants;
134
	}
135
136
	/**
137
	 * Returns all constant names
138
	 *
139
	 * @return Set
140
	 */
141 1
	public function getConstantNames(): Set {
142 1
		return $this->constants->keys();
143
	}
144
145
	/**
146
	 * Clears all constants
147
	 *
148
	 * @return $this
149
	 */
150 1
	public function clearConstants() {
151 1
		$this->constants->clear();
152
153 1
		return $this;
154
	}
155
}
156