• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nette / php-generator / 21808450793

09 Feb 2026 12:39AM UTC coverage: 93.947% (+0.1%) from 93.811%
21808450793

push

github

dg
added CLAUDE.md

1816 of 1933 relevant lines covered (93.95%)

0.94 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

54.55
/src/PhpGenerator/EnumType.php
1
<?php
2

3
/**
4
 * This file is part of the Nette Framework (https://nette.org)
5
 * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6
 */
7

8
declare(strict_types=1);
9

10
namespace Nette\PhpGenerator;
11

12
use Nette;
13

14

15
/**
16
 * Definition of an enum with cases, methods, constants and traits.
17
 */
18
final class EnumType extends ClassLike
19
{
20
        use Traits\ConstantsAware;
21
        use Traits\MethodsAware;
22
        use Traits\TraitsAware;
23

24
        /** @var list<string> */
25
        private array $implements = [];
26

27
        /** @var array<string, EnumCase> */
28
        private array $cases = [];
29
        private ?string $type = null;
30

31

32
        public function setType(?string $type): static
1✔
33
        {
34
                $this->type = $type;
1✔
35
                return $this;
1✔
36
        }
37

38

39
        public function getType(): ?string
40
        {
41
                return $this->type;
1✔
42
        }
43

44

45
        /** @param list<string>  $names */
46
        public function setImplements(array $names): static
1✔
47
        {
48
                $this->validateNames($names);
1✔
49
                $this->implements = $names;
1✔
50
                return $this;
1✔
51
        }
52

53

54
        /** @return list<string> */
55
        public function getImplements(): array
56
        {
57
                return $this->implements;
1✔
58
        }
59

60

61
        public function addImplement(string $name): static
1✔
62
        {
63
                $this->validateNames([$name]);
1✔
64
                $this->implements[] = $name;
1✔
65
                return $this;
1✔
66
        }
67

68

69
        public function removeImplement(string $name): static
70
        {
71
                $this->implements = array_values(array_diff($this->implements, [$name]));
×
72
                return $this;
×
73
        }
74

75

76
        /**
77
         * Sets cases to enum
78
         * @param  EnumCase[]  $cases
79
         */
80
        public function setCases(array $cases): static
1✔
81
        {
82
                (function (EnumCase ...$cases) {})(...$cases);
1✔
83
                $this->cases = [];
1✔
84
                foreach ($cases as $case) {
1✔
85
                        $this->cases[$case->getName()] = $case;
1✔
86
                }
87

88
                return $this;
1✔
89
        }
90

91

92
        /** @return EnumCase[] */
93
        public function getCases(): array
94
        {
95
                return $this->cases;
1✔
96
        }
97

98

99
        /** Adds case to enum */
100
        public function addCase(string $name, string|int|Literal|null $value = null, bool $overwrite = false): EnumCase
1✔
101
        {
102
                if (!$overwrite && isset($this->cases[$name])) {
1✔
103
                        throw new Nette\InvalidStateException("Cannot add cases '$name', because it already exists.");
×
104
                }
105
                return $this->cases[$name] = (new EnumCase($name))
1✔
106
                        ->setValue($value);
1✔
107
        }
108

109

110
        public function removeCase(string $name): static
111
        {
112
                unset($this->cases[$name]);
×
113
                return $this;
×
114
        }
115

116

117
        /**
118
         * Adds a member. If it already exists, throws an exception or overwrites it if $overwrite is true.
119
         */
120
        public function addMember(Method|Constant|EnumCase|TraitUse $member, bool $overwrite = false): static
121
        {
122
                $name = $member->getName();
×
123
                [$type, $n] = match (true) {
×
124
                        $member instanceof Constant => ['consts', $name],
×
125
                        $member instanceof Method => ['methods', strtolower($name)],
×
126
                        $member instanceof TraitUse => ['traits', $name],
×
127
                        $member instanceof EnumCase => ['cases', $name],
×
128
                };
129
                if (!$overwrite && isset($this->$type[$n])) {
×
130
                        throw new Nette\InvalidStateException("Cannot add member '$name', because it already exists.");
×
131
                }
132
                $this->$type[$n] = $member;
×
133
                return $this;
×
134
        }
135

136

137
        public function __clone(): void
138
        {
139
                parent::__clone();
×
140
                $this->consts = array_map(fn(Constant $c) => clone $c, $this->consts);
×
141
                $this->methods = array_map(fn(Method $m) => clone $m, $this->methods);
×
142
                $this->traits = array_map(fn(TraitUse $t) => clone $t, $this->traits);
×
143
                $this->cases = array_map(fn(EnumCase $c) => clone $c, $this->cases);
×
144
        }
145
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc