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

nette / php-generator / 9052389242

12 May 2024 03:19PM UTC coverage: 93.686% (-0.1%) from 93.792%
9052389242

push

github

dg
Factory, Extractor: sets flag readonly for promoted properties [Closes #158]

6 of 6 new or added lines in 3 files covered. (100.0%)

5 existing lines in 4 files now uncovered.

1558 of 1663 relevant lines covered (93.69%)

0.94 hits per line

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

32.56
/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
 * Enum description.
17
 */
18
final class EnumType extends ClassLike
19
{
20
        use Traits\ConstantsAware;
21
        use Traits\MethodsAware;
22
        use Traits\TraitsAware;
23

24
        /** @var 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
        /**
46
         * @param  string[]  $names
47
         */
48
        public function setImplements(array $names): static
49
        {
50
                $this->validateNames($names);
×
51
                $this->implements = $names;
×
52
                return $this;
×
53
        }
54

55

56
        /** @return string[] */
57
        public function getImplements(): array
58
        {
59
                return $this->implements;
1✔
60
        }
61

62

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

70

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

77

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

90
                return $this;
×
91
        }
92

93

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

100

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

111

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

118

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

138

139
        public function __clone(): void
140
        {
141
                parent::__clone();
×
142
                $clone = fn($item) => clone $item;
×
143
                $this->consts = array_map($clone, $this->consts);
×
144
                $this->methods = array_map($clone, $this->methods);
×
145
                $this->traits = array_map($clone, $this->traits);
×
UNCOV
146
                $this->cases = array_map($clone, $this->cases);
×
147
        }
148
}
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