Completed
Pull Request — master (#68)
by Cristiano
05:36
created

ConstantsPart   A

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
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of the php-code-generator package.
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 *
7
 * @license Apache-2.0
8
 */
9
10
namespace gossi\codegen\model\parts;
11
12
use gossi\codegen\model\PhpConstant;
13
use phootwork\collection\Map;
14
use phootwork\collection\Set;
15
16
/**
17
 * Constants part
18
 *
19
 * For all models that can contain constants
20
 *
21
 * @author Thomas Gossmann
22 52
 */
23 52
trait ConstantsPart {
24 52
25
	/** @var Map */
26
	private Map $constants;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
27
28
	private function initConstants() {
29
		$this->constants = new Map();
30
	}
31
32 1
	/**
33 1
	 * Sets a collection of constants
34 1
	 *
35 1
	 * @param array|PhpConstant[] $constants
36 1
	 *
37
	 * @return $this
38 1
	 */
39 1
	public function setConstants(array $constants): self {
40 1
		$normalizedConstants = [];
41
		foreach ($constants as $name => $value) {
42
			if ($value instanceof PhpConstant) {
43 1
				$name = $value->getName();
44
			} else {
45
				$constValue = $value;
46 1
				$value = new PhpConstant($name);
47
				$value->setValue($constValue);
48 1
			}
49
50
			$normalizedConstants[$name] = $value;
51
		}
52
53
		$this->constants->setAll($normalizedConstants);
54
55
		return $this;
56
	}
57
58
	/**
59 8
	 * Adds a constant
60 8
	 *
61 7
	 * @param string|PhpConstant $nameOrConstant constant name or instance
62 7
	 * @param string|int|float|bool|null $value
63
	 * @param bool $isExpression whether the value is an expression or not
64 2
	 *
65 2
	 * @return $this
66
	 */
67
	public function setConstant(PhpConstant|string $nameOrConstant, string|int|float|bool|null $value = '', bool $isExpression = false): self {
68 8
		if ($nameOrConstant instanceof PhpConstant) {
69
			$name = $nameOrConstant->getName();
70 8
			$constant = $nameOrConstant;
71
		} else {
72
			$name = $nameOrConstant;
73
			$constant = new PhpConstant($nameOrConstant, $value, $isExpression);
74
		}
75
76
		$this->constants->set($name, $constant);
77
78
		return $this;
79
	}
80 2
81 2
	/**
82 1
	 * Removes a constant
83
	 *
84
	 * @param string|PhpConstant $nameOrConstant constant name
85 2
	 *
86 1
	 * @throws \InvalidArgumentException If the constant cannot be found
87
	 *
88
	 * @return $this
89 1
	 */
90
	public function removeConstant(PhpConstant|string $nameOrConstant): self {
91 1
		if (!$this->constants->has((string) $nameOrConstant)) {
92
			throw new \InvalidArgumentException(sprintf('The constant "%s" does not exist.', $nameOrConstant));
93
		}
94
95
		$this->constants->remove((string) $nameOrConstant);
96
97
		return $this;
98
	}
99
100 2
	/**
101 2
	 * Checks whether a constant exists
102 1
	 *
103
	 * @param string|PhpConstant $nameOrConstant
104
	 *
105 2
	 * @return bool
106
	 */
107
	public function hasConstant(PhpConstant|string $nameOrConstant): bool {
108
		return $this->constants->has((string) $nameOrConstant);
109
	}
110
111
	/**
112
	 * Returns a constant.
113
	 *
114
	 * @param string $name
115 6
	 *
116 6
	 * @throws \InvalidArgumentException If the constant cannot be found
117 1
	 *
118
	 * @return PhpConstant
119
	 */
120 6
	public function getConstant(string $name): PhpConstant {
121 2
		if (!$this->constants->has($name)) {
122
			throw new \InvalidArgumentException(sprintf('The constant "%s" does not exist.', $name));
123
		}
124 5
125
		return $this->constants->get($name);
126
	}
127
128
	/**
129
	 * Returns all constants
130
	 *
131
	 * @return Map
132 16
	 */
133 16
	public function getConstants(): Map {
134
		return $this->constants;
135
	}
136
137
	/**
138
	 * Returns all constant names
139
	 *
140
	 * @return Set
141 1
	 */
142 1
	public function getConstantNames(): Set {
143
		return $this->constants->keys();
144
	}
145
146
	/**
147
	 * Clears all constants
148
	 *
149
	 * @return $this
150 1
	 */
151 1
	public function clearConstants(): self {
152
		$this->constants->clear();
153 1
154
		return $this;
155
	}
156
}
157