• 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

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

13

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

24
        #[\Deprecated]
25
        public const
26
                TYPE_CLASS = 'class',
27
                TYPE_INTERFACE = 'interface',
28
                TYPE_TRAIT = 'trait',
29
                TYPE_ENUM = 'enum';
30

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

36
        /** @var list<string> */
37
        private array $implements = [];
38

39

40
        public function __construct(?string $name = null)
1✔
41
        {
42
                parent::__construct($name ?? 'foo', func_num_args() > 1 ? func_get_arg(1) : null); // backward compatibility
1✔
43
                if ($name === null) {
1✔
44
                        $this->setName(null);
1✔
45
                }
46
        }
1✔
47

48

49
        public function setFinal(bool $state = true): static
1✔
50
        {
51
                $this->final = $state;
1✔
52
                return $this;
1✔
53
        }
54

55

56
        public function isFinal(): bool
57
        {
58
                return $this->final;
1✔
59
        }
60

61

62
        public function setAbstract(bool $state = true): static
1✔
63
        {
64
                $this->abstract = $state;
1✔
65
                return $this;
1✔
66
        }
67

68

69
        public function isAbstract(): bool
70
        {
71
                return $this->abstract;
1✔
72
        }
73

74

75
        public function setReadOnly(bool $state = true): static
1✔
76
        {
77
                $this->readOnly = $state;
1✔
78
                return $this;
1✔
79
        }
80

81

82
        public function isReadOnly(): bool
83
        {
84
                return $this->readOnly;
1✔
85
        }
86

87

88
        public function setExtends(?string $name): static
1✔
89
        {
90
                if ($name) {
1✔
91
                        $this->validateNames([$name]);
1✔
92
                }
93
                $this->extends = $name;
1✔
94
                return $this;
1✔
95
        }
96

97

98
        public function getExtends(): ?string
99
        {
100
                return $this->extends;
1✔
101
        }
102

103

104
        /** @param list<string>  $names */
105
        public function setImplements(array $names): static
1✔
106
        {
107
                $this->validateNames($names);
1✔
108
                $this->implements = $names;
1✔
109
                return $this;
1✔
110
        }
111

112

113
        /** @return list<string> */
114
        public function getImplements(): array
115
        {
116
                return $this->implements;
1✔
117
        }
118

119

120
        public function addImplement(string $name): static
1✔
121
        {
122
                $this->validateNames([$name]);
1✔
123
                $this->implements[] = $name;
1✔
124
                return $this;
1✔
125
        }
126

127

128
        public function removeImplement(string $name): static
1✔
129
        {
130
                $this->implements = array_values(array_diff($this->implements, [$name]));
1✔
131
                return $this;
1✔
132
        }
133

134

135
        /**
136
         * Adds a member to the class.
137
         * @throws Nette\InvalidStateException if the member already exists and $overwrite is false
138
         */
139
        public function addMember(Method|Property|Constant|TraitUse $member, bool $overwrite = false): static
1✔
140
        {
141
                $name = $member->getName();
1✔
142
                [$type, $n] = match (true) {
1✔
143
                        $member instanceof Constant => ['consts', $name],
1✔
144
                        $member instanceof Method => ['methods', strtolower($name)],
1✔
145
                        $member instanceof Property => ['properties', $name],
1✔
146
                        $member instanceof TraitUse => ['traits', $name],
1✔
147
                };
148
                if (!$overwrite && isset($this->$type[$n])) {
1✔
149
                        throw new Nette\InvalidStateException("Cannot add member '$name', because it already exists.");
1✔
150
                }
151
                $this->$type[$n] = $member;
1✔
152
                return $this;
1✔
153
        }
154

155

156
        /**
157
         * @deprecated use ClassManipulator::inheritProperty()
158
         */
159
        public function inheritProperty(string $name, bool $returnIfExists = false): Property
160
        {
161
                return (new ClassManipulator($this))->inheritProperty($name, $returnIfExists);
×
162
        }
163

164

165
        /**
166
         * @deprecated use ClassManipulator::inheritMethod()
167
         */
168
        public function inheritMethod(string $name, bool $returnIfExists = false): Method
169
        {
170
                return (new ClassManipulator($this))->inheritMethod($name, $returnIfExists);
×
171
        }
172

173

174
        /** @throws Nette\InvalidStateException */
175
        public function validate(): void
176
        {
177
                $name = $this->getName();
1✔
178
                if ($name === null && ($this->abstract || $this->final)) {
1✔
179
                        throw new Nette\InvalidStateException('Anonymous class cannot be abstract or final.');
1✔
180

181
                } elseif ($this->abstract && $this->final) {
1✔
182
                        throw new Nette\InvalidStateException("Class '$name' cannot be abstract and final at the same time.");
1✔
183
                }
184
        }
1✔
185

186

187
        public function __clone(): void
188
        {
189
                parent::__clone();
1✔
190
                $this->consts = array_map(fn(Constant $c) => clone $c, $this->consts);
1✔
191
                $this->methods = array_map(fn(Method $m) => clone $m, $this->methods);
1✔
192
                $this->properties = array_map(fn(Property $p) => clone $p, $this->properties);
1✔
193
                $this->traits = array_map(fn(TraitUse $t) => clone $t, $this->traits);
1✔
194
        }
1✔
195
}
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