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

nette / php-generator / 22836119803

09 Mar 2026 02:40AM UTC coverage: 94.264% (+0.2%) from 94.029%
22836119803

push

github

dg
added CLAUDE.md

1890 of 2005 relevant lines covered (94.26%)

0.94 hits per line

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

55.56
/src/PhpGenerator/EnumType.php
1
<?php declare(strict_types=1);
1✔
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
namespace Nette\PhpGenerator;
9

10
use Nette;
11

12

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

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

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

29

30
        /**
31
         * Sets the backing type of a backed enum ('int' or 'string').
32
         */
33
        public function setType(?string $type): static
1✔
34
        {
35
                $this->type = $type;
1✔
36
                return $this;
1✔
37
        }
38

39

40
        /**
41
         * Returns the backing type ('int' or 'string'), or null for a pure enum.
42
         */
43
        public function getType(): ?string
44
        {
45
                return $this->type;
1✔
46
        }
47

48

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

57

58
        /** @return list<string> */
59
        public function getImplements(): array
60
        {
61
                return $this->implements;
1✔
62
        }
63

64

65
        public function addImplement(string $name): static
1✔
66
        {
67
                $this->validateNames([$name]);
1✔
68
                $this->implements[] = $name;
1✔
69
                return $this;
1✔
70
        }
71

72

73
        public function removeImplement(string $name): static
74
        {
75
                $this->implements = array_values(array_diff($this->implements, [$name]));
×
76
                return $this;
×
77
        }
78

79

80
        /**
81
         * Replaces all cases.
82
         * @param  list<EnumCase>  $cases
83
         */
84
        public function setCases(array $cases): static
1✔
85
        {
86
                (function (EnumCase ...$cases) {})(...$cases);
1✔
87
                $this->cases = [];
1✔
88
                foreach ($cases as $case) {
1✔
89
                        $this->cases[$case->getName()] = $case;
1✔
90
                }
91

92
                return $this;
1✔
93
        }
94

95

96
        /** @return array<string, EnumCase> */
97
        public function getCases(): array
98
        {
99
                return $this->cases;
1✔
100
        }
101

102

103
        /**
104
         * Adds a case to the enum.
105
         * @throws Nette\InvalidStateException if the case already exists and $overwrite is false
106
         */
107
        public function addCase(string $name, string|int|Literal|null $value = null, bool $overwrite = false): EnumCase
1✔
108
        {
109
                if (!$overwrite && isset($this->cases[$name])) {
1✔
110
                        throw new Nette\InvalidStateException("Cannot add cases '$name', because it already exists.");
×
111
                }
112
                return $this->cases[$name] = (new EnumCase($name))
1✔
113
                        ->setValue($value);
1✔
114
        }
115

116

117
        public function removeCase(string $name): static
118
        {
119
                unset($this->cases[$name]);
×
120
                return $this;
×
121
        }
122

123

124
        /**
125
         * Adds a member to the enum.
126
         * @throws Nette\InvalidStateException if the member already exists and $overwrite is false
127
         */
128
        public function addMember(Method|Constant|EnumCase|TraitUse $member, bool $overwrite = false): static
129
        {
130
                $name = $member->getName();
×
131
                [$type, $n] = match (true) {
×
132
                        $member instanceof Constant => ['consts', $name],
×
133
                        $member instanceof Method => ['methods', strtolower($name)],
×
134
                        $member instanceof TraitUse => ['traits', $name],
×
135
                        $member instanceof EnumCase => ['cases', $name],
×
136
                };
137
                if (!$overwrite && isset($this->$type[$n])) {
×
138
                        throw new Nette\InvalidStateException("Cannot add member '$name', because it already exists.");
×
139
                }
140
                $this->$type[$n] = $member;
×
141
                return $this;
×
142
        }
143

144

145
        public function __clone(): void
146
        {
147
                parent::__clone();
×
148
                $this->consts = array_map(fn(Constant $c) => clone $c, $this->consts);
×
149
                $this->methods = array_map(fn(Method $m) => clone $m, $this->methods);
×
150
                $this->traits = array_map(fn(TraitUse $t) => clone $t, $this->traits);
×
151
                $this->cases = array_map(fn(EnumCase $c) => clone $c, $this->cases);
×
152
        }
153
}
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