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

nette / php-generator / 20357425652

19 Dec 2025 02:04AM UTC coverage: 93.614%. Remained the same
20357425652

push

github

dg
Factory::fromClassReflection() refactoring

15 of 15 new or added lines in 1 file covered. (100.0%)

21 existing lines in 2 files now uncovered.

1803 of 1926 relevant lines covered (93.61%)

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
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
        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
        /**
107
         * @param  string[]  $names
108
         */
109
        public function setImplements(array $names): static
1✔
110
        {
111
                $this->validateNames($names);
1✔
112
                $this->implements = $names;
1✔
113
                return $this;
1✔
114
        }
115

116

117
        /** @return string[] */
118
        public function getImplements(): array
119
        {
120
                return $this->implements;
1✔
121
        }
122

123

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

131

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

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
        {
UNCOV
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
        {
UNCOV
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
                $clone = fn($item) => clone $item;
1✔
191
                $this->consts = array_map($clone, $this->consts);
1✔
192
                $this->methods = array_map($clone, $this->methods);
1✔
193
                $this->properties = array_map($clone, $this->properties);
1✔
194
                $this->traits = array_map($clone, $this->traits);
1✔
195
        }
1✔
196
}
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