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

nette / php-generator / 14161583165

31 Mar 2025 12:29AM UTC coverage: 91.977%. First build
14161583165

push

github

dg
Factory::fromClassReflection() extracts property hook bodies (#172)

22 of 24 new or added lines in 2 files covered. (91.67%)

1754 of 1907 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

14

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

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

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

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

40

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

48

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

56

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

64

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

72

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

83

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

91

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

98

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

104

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

111

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

117

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

124

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

130

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

140

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

146

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

157

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

164

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

172

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

179

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

196

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

205

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

214

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

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

227

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