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

nette / php-generator / 16784303509

06 Aug 2025 05:41PM UTC coverage: 93.547% (+0.6%) from 92.961%
16784303509

push

github

dg
updated phpstan-baseline

1783 of 1906 relevant lines covered (93.55%)

0.94 hits per line

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

96.77
/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, ?PhpNamespace $namespace = null)
1✔
43
        {
44
                if ($name === null) {
1✔
45
                        parent::__construct('foo', $namespace);
1✔
46
                        $this->setName(null);
1✔
47
                } else {
48
                        parent::__construct($name, $namespace);
1✔
49
                }
50
        }
1✔
51

52

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

59

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

65

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

72

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

78

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

85

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

91

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

101

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

107

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

118

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

125

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

133

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

140

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

157

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

166

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

175

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

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

188

189
        public function __clone(): void
190
        {
191
                parent::__clone();
1✔
192
                $clone = fn($item) => clone $item;
1✔
193
                $this->consts = array_map($clone, $this->consts);
1✔
194
                $this->methods = array_map($clone, $this->methods);
1✔
195
                $this->properties = array_map($clone, $this->properties);
1✔
196
                $this->traits = array_map($clone, $this->traits);
1✔
197
        }
1✔
198
}
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