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

nette / php-generator / 15763583786

19 Jun 2025 05:44PM UTC coverage: 91.981% (+0.004%) from 91.977%
15763583786

push

github

dg
optimized global function calls

1755 of 1908 relevant lines covered (91.98%)

0.92 hits per line

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

83.33
/src/PhpGenerator/ClassType.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
use function array_diff, array_map, strtolower;
14

15

16
/**
17
 * Definition of a class with properties, methods, constants, traits and PHP attributes.
18
 */
19
final class ClassType extends ClassLike
20
{
21
        use Traits\ConstantsAware;
22
        use Traits\MethodsAware;
23
        use Traits\PropertiesAware;
24
        use Traits\TraitsAware;
25

26
        /** @deprecated */
27
        public const
28
                TYPE_CLASS = 'class',
29
                TYPE_INTERFACE = 'interface',
30
                TYPE_TRAIT = 'trait',
31
                TYPE_ENUM = 'enum';
32

33
        private bool $final = false;
34
        private bool $abstract = false;
35
        private ?string $extends = null;
36
        private bool $readOnly = false;
37

38
        /** @var string[] */
39
        private array $implements = [];
40

41

42
        /** @deprecated  create object using 'new Nette\PhpGenerator\ClassType' */
43
        public static function class(?string $name): self
44
        {
45
                trigger_error(__METHOD__ . "() is deprecated, create object using 'new Nette\\PhpGenerator\\ClassType", E_USER_DEPRECATED);
×
46
                return new self($name);
×
47
        }
48

49

50
        /** @deprecated  create object using 'new Nette\PhpGenerator\InterfaceType' */
51
        public static function interface(string $name): InterfaceType
52
        {
53
                trigger_error(__METHOD__ . "() is deprecated, create object using 'new Nette\\PhpGenerator\\InterfaceType'", E_USER_DEPRECATED);
×
54
                return new InterfaceType($name);
×
55
        }
56

57

58
        /** @deprecated  create object using 'new Nette\PhpGenerator\TraitType' */
59
        public static function trait(string $name): TraitType
60
        {
61
                trigger_error(__METHOD__ . "() is deprecated, create object using 'new Nette\\PhpGenerator\\TraitType'", E_USER_DEPRECATED);
×
62
                return new TraitType($name);
×
63
        }
64

65

66
        /** @deprecated  create object using 'new Nette\PhpGenerator\EnumType' */
67
        public static function enum(string $name): EnumType
68
        {
69
                trigger_error(__METHOD__ . "() is deprecated, create object using 'new Nette\\PhpGenerator\\EnumType'", E_USER_DEPRECATED);
×
70
                return new EnumType($name);
×
71
        }
72

73

74
        public function __construct(?string $name = null, ?PhpNamespace $namespace = null)
1✔
75
        {
76
                if ($name === null) {
1✔
77
                        parent::__construct('foo', $namespace);
1✔
78
                        $this->setName(null);
1✔
79
                } else {
80
                        parent::__construct($name, $namespace);
1✔
81
                }
82
        }
1✔
83

84

85
        /** @deprecated */
86
        public function getType(): string
87
        {
88
                trigger_error(__METHOD__ . "() is deprecated, method always returns 'class'", E_USER_DEPRECATED);
×
89
                return self::TYPE_CLASS;
×
90
        }
91

92

93
        public function setFinal(bool $state = true): static
1✔
94
        {
95
                $this->final = $state;
1✔
96
                return $this;
1✔
97
        }
98

99

100
        public function isFinal(): bool
101
        {
102
                return $this->final;
1✔
103
        }
104

105

106
        public function setAbstract(bool $state = true): static
1✔
107
        {
108
                $this->abstract = $state;
1✔
109
                return $this;
1✔
110
        }
111

112

113
        public function isAbstract(): bool
114
        {
115
                return $this->abstract;
1✔
116
        }
117

118

119
        public function setReadOnly(bool $state = true): static
1✔
120
        {
121
                $this->readOnly = $state;
1✔
122
                return $this;
1✔
123
        }
124

125

126
        public function isReadOnly(): bool
127
        {
128
                return $this->readOnly;
1✔
129
        }
130

131

132
        public function setExtends(?string $name): static
1✔
133
        {
134
                if ($name) {
1✔
135
                        $this->validateNames([$name]);
1✔
136
                }
137
                $this->extends = $name;
1✔
138
                return $this;
1✔
139
        }
140

141

142
        public function getExtends(): ?string
143
        {
144
                return $this->extends;
1✔
145
        }
146

147

148
        /**
149
         * @param  string[]  $names
150
         */
151
        public function setImplements(array $names): static
1✔
152
        {
153
                $this->validateNames($names);
1✔
154
                $this->implements = $names;
1✔
155
                return $this;
1✔
156
        }
157

158

159
        /** @return string[] */
160
        public function getImplements(): array
161
        {
162
                return $this->implements;
1✔
163
        }
164

165

166
        public function addImplement(string $name): static
1✔
167
        {
168
                $this->validateNames([$name]);
1✔
169
                $this->implements[] = $name;
1✔
170
                return $this;
1✔
171
        }
172

173

174
        public function removeImplement(string $name): static
1✔
175
        {
176
                $this->implements = array_diff($this->implements, [$name]);
1✔
177
                return $this;
1✔
178
        }
179

180

181
        public function addMember(Method|Property|Constant|TraitUse $member, bool $overwrite = false): static
1✔
182
        {
183
                $name = $member->getName();
1✔
184
                [$type, $n] = match (true) {
1✔
185
                        $member instanceof Constant => ['consts', $name],
1✔
186
                        $member instanceof Method => ['methods', strtolower($name)],
1✔
187
                        $member instanceof Property => ['properties', $name],
1✔
188
                        $member instanceof TraitUse => ['traits', $name],
1✔
189
                };
190
                if (!$overwrite && isset($this->$type[$n])) {
1✔
191
                        throw new Nette\InvalidStateException("Cannot add member '$name', because it already exists.");
1✔
192
                }
193
                $this->$type[$n] = $member;
1✔
194
                return $this;
1✔
195
        }
196

197

198
        /**
199
         * @deprecated use ClassManipulator::inheritProperty()
200
         */
201
        public function inheritProperty(string $name, bool $returnIfExists = false): Property
202
        {
203
                return (new ClassManipulator($this))->inheritProperty($name, $returnIfExists);
×
204
        }
205

206

207
        /**
208
         * @deprecated use ClassManipulator::inheritMethod()
209
         */
210
        public function inheritMethod(string $name, bool $returnIfExists = false): Method
211
        {
212
                return (new ClassManipulator($this))->inheritMethod($name, $returnIfExists);
×
213
        }
214

215

216
        /** @throws Nette\InvalidStateException */
217
        public function validate(): void
218
        {
219
                $name = $this->getName();
1✔
220
                if ($name === null && ($this->abstract || $this->final)) {
1✔
221
                        throw new Nette\InvalidStateException('Anonymous class cannot be abstract or final.');
1✔
222

223
                } elseif ($this->abstract && $this->final) {
1✔
224
                        throw new Nette\InvalidStateException("Class '$name' cannot be abstract and final at the same time.");
1✔
225
                }
226
        }
1✔
227

228

229
        public function __clone(): void
230
        {
231
                parent::__clone();
1✔
232
                $clone = fn($item) => clone $item;
1✔
233
                $this->consts = array_map($clone, $this->consts);
1✔
234
                $this->methods = array_map($clone, $this->methods);
1✔
235
                $this->properties = array_map($clone, $this->properties);
1✔
236
                $this->traits = array_map($clone, $this->traits);
1✔
237
        }
1✔
238
}
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