Skip to content

Commit fe865d7

Browse files
author
Bernhard Schmitt
committed
Enforce enums to have at least one case
1 parent 87ccf3b commit fe865d7

1 file changed

Lines changed: 25 additions & 34 deletions

File tree

Classes/Domain/Enum/Enum.php

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,23 @@ final class Enum
2323
private EnumType $type;
2424

2525
/**
26-
* @var null|string[]|int[]
26+
* @var string[]|int[]
2727
*/
28-
private ?array $values;
28+
private array $cases;
2929

3030
/**
3131
* @param EnumName $name
3232
* @param EnumType $type
33-
* @param null|string[]|int[] $values
33+
* @param string[]|int[] $cases
3434
*/
35-
public function __construct(EnumName $name, EnumType $type, ?array $values)
35+
public function __construct(EnumName $name, EnumType $type, array $cases)
3636
{
37+
if (empty($cases)) {
38+
throw new \InvalidArgumentException('Enums must have at least one case, none given.', 1626541482);
39+
}
3740
$this->name = $name;
3841
$this->type = $type;
39-
$this->values = $values;
42+
$this->cases = $cases;
4043
}
4144

4245
/**
@@ -145,13 +148,11 @@ public static function becauseItMustBeOneOfTheDefinedConstants(' . $this->type .
145148
private function renderConstants(): string
146149
{
147150
$constants = [];
148-
if (is_array($this->values)) {
149-
foreach ($this->values as $name => $value) {
150-
$renderedValue = $this->type->isString()
151-
? '\'' . $value . '\''
152-
: $value;
153-
$constants[] = 'const ' . $this->getConstantName($name) . ' = ' . $renderedValue . ';';
154-
}
151+
foreach ($this->cases as $name => $case) {
152+
$renderedValue = $this->type->isString()
153+
? '\'' . $case . '\''
154+
: $case;
155+
$constants[] = 'const ' . $this->getConstantName($name) . ' = ' . $renderedValue . ';';
155156
}
156157

157158
return trim(implode("\n ", $constants));
@@ -162,19 +163,15 @@ private function renderConstants(): string
162163
*/
163164
private function renderValidation(): string
164165
{
165-
if (is_array($this->values)) {
166-
$variable = '$' . $this->type;
167-
$caseChecks = [];
168-
foreach ($this->values as $name => $value) {
169-
$caseChecks[] = $variable . ' !== self::' . $this->getConstantName($name);
170-
}
166+
$variable = '$' . $this->type;
167+
$caseChecks = [];
168+
foreach ($this->cases as $name => $value) {
169+
$caseChecks[] = $variable . ' !== self::' . $this->getConstantName($name);
170+
}
171171

172-
return 'if (' . implode("\n && ", $caseChecks) . ') {
172+
return 'if (' . implode("\n && ", $caseChecks) . ') {
173173
throw ' . $this->name->getExceptionName() . '::becauseItMustBeOneOfTheDefinedConstants(' . $variable . ');
174174
}';
175-
}
176-
177-
return '';
178175
}
179176

180177
/**
@@ -183,13 +180,11 @@ private function renderValidation(): string
183180
private function renderNamedConstructors(): string
184181
{
185182
$constructors = [];
186-
if (is_array($this->values)) {
187-
foreach ($this->values as $name => $value) {
188-
$constructors[] = 'public static function ' . $name . '(): self
183+
foreach ($this->cases as $name => $case) {
184+
$constructors[] = 'public static function ' . $name . '(): self
189185
{
190186
return self::from(self::' . $this->getConstantName($name) . ');
191187
}';
192-
}
193188
}
194189

195190
return trim(implode("\n\n ", $constructors));
@@ -201,13 +196,11 @@ private function renderNamedConstructors(): string
201196
private function renderComparators(): string
202197
{
203198
$comparators = [];
204-
if (is_array($this->values)) {
205-
foreach ($this->values as $name => $value) {
206-
$comparators[] = 'public function getIs' . ucfirst($name) . '(): bool
199+
foreach ($this->cases as $name => $case) {
200+
$comparators[] = 'public function getIs' . ucfirst($name) . '(): bool
207201
{
208202
return $this->value === self::' . $this->getConstantName($name) . ';
209203
}';
210-
}
211204
}
212205

213206
return trim(implode("\n\n ", $comparators));
@@ -220,10 +213,8 @@ public function renderCases(): string
220213
{
221214
$cases = [];
222215

223-
if (is_array($this->values)) {
224-
foreach ($this->values as $name => $value) {
225-
$cases[] = 'self::from(self::' . $this->getConstantName($name) . '),';
226-
}
216+
foreach ($this->cases as $name => $case) {
217+
$cases[] = 'self::from(self::' . $this->getConstantName($name) . '),';
227218
}
228219

229220
return trim(trim(implode("\n ", $cases)), ',');

0 commit comments

Comments
 (0)