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

nette / php-generator / 21808450793

09 Feb 2026 12:39AM UTC coverage: 93.947% (+0.1%) from 93.811%
21808450793

push

github

dg
added CLAUDE.md

1816 of 1933 relevant lines covered (93.95%)

0.94 hits per line

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

96.67
/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 list<string> */
39
        private array $implements = [];
40

41

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

50

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

57

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

63

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

70

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

76

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

83

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

89

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

99

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

105

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

114

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

121

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

129

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

136

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

153

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

162

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

171

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

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

184

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