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

nette / php-generator / 6016511873

29 Aug 2023 07:09PM UTC coverage: 93.385%. Remained the same
6016511873

push

github

dg
ClassType::class(), interface(), traits() & enum() are deprecated

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

1454 of 1557 relevant lines covered (93.38%)

0.93 hits per line

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

88.73
/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
 * Class description.
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 string $type = self::TYPE_CLASS;
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
        /** @deprecated  create object using 'new Nette\PhpGenerator\ClassType' */
43
        public static function class(?string $name): self
44
        {
45
                trigger_error(__METHOD__ . "() is deprecated, create object using 'new Nette\\PhpGenerator\\ClassType", E_USER_DEPRECATED);
×
46
                return new self($name);
×
47
        }
48

49

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

57

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

65

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

73

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

84

85
        public function isClass(): bool
86
        {
87
                return $this->type === self::TYPE_CLASS;
1✔
88
        }
89

90

91
        public function isInterface(): bool
92
        {
93
                return $this->type === self::TYPE_INTERFACE;
1✔
94
        }
95

96

97
        public function isTrait(): bool
98
        {
99
                return $this->type === self::TYPE_TRAIT;
1✔
100
        }
101

102

103
        /** @deprecated */
104
        public function getType(): string
105
        {
106
                return $this->type;
1✔
107
        }
108

109

110
        public function setFinal(bool $state = true): static
1✔
111
        {
112
                $this->final = $state;
1✔
113
                return $this;
1✔
114
        }
115

116

117
        public function isFinal(): bool
118
        {
119
                return $this->final;
1✔
120
        }
121

122

123
        public function setAbstract(bool $state = true): static
1✔
124
        {
125
                $this->abstract = $state;
1✔
126
                return $this;
1✔
127
        }
128

129

130
        public function isAbstract(): bool
131
        {
132
                return $this->abstract;
1✔
133
        }
134

135

136
        public function setReadOnly(bool $state = true): static
1✔
137
        {
138
                $this->readOnly = $state;
1✔
139
                return $this;
1✔
140
        }
141

142

143
        public function isReadOnly(): bool
144
        {
145
                return $this->readOnly;
1✔
146
        }
147

148

149
        public function setExtends(?string $name): static
1✔
150
        {
151
                if ($name) {
1✔
152
                        $this->validateNames([$name]);
1✔
153
                }
154
                $this->extends = $name;
1✔
155
                return $this;
1✔
156
        }
157

158

159
        public function getExtends(): ?string
160
        {
161
                return $this->extends;
1✔
162
        }
163

164

165
        /**
166
         * @param  string[]  $names
167
         */
168
        public function setImplements(array $names): static
1✔
169
        {
170
                $this->validateNames($names);
1✔
171
                $this->implements = $names;
1✔
172
                return $this;
1✔
173
        }
174

175

176
        /** @return string[] */
177
        public function getImplements(): array
178
        {
179
                return $this->implements;
1✔
180
        }
181

182

183
        public function addImplement(string $name): static
1✔
184
        {
185
                $this->validateNames([$name]);
1✔
186
                $this->implements[] = $name;
1✔
187
                return $this;
1✔
188
        }
189

190

191
        public function removeImplement(string $name): static
1✔
192
        {
193
                $this->implements = array_diff($this->implements, [$name]);
1✔
194
                return $this;
1✔
195
        }
196

197

198
        public function addMember(Method|Property|Constant|TraitUse $member): static
1✔
199
        {
200
                $name = $member->getName();
1✔
201
                [$type, $n] = match (true) {
1✔
202
                        $member instanceof Constant => ['consts', $name],
1✔
203
                        $member instanceof Method => ['methods', strtolower($name)],
1✔
204
                        $member instanceof Property => ['properties', $name],
1✔
205
                        $member instanceof TraitUse => ['traits', $name],
1✔
206
                };
207
                if (isset($this->$type[$n])) {
1✔
208
                        throw new Nette\InvalidStateException("Cannot add member '$name', because it already exists.");
1✔
209
                }
210
                $this->$type[$n] = $member;
1✔
211
                return $this;
1✔
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()
229
        {
230
                $clone = fn($item) => clone $item;
1✔
231
                $this->consts = array_map($clone, $this->consts);
1✔
232
                $this->methods = array_map($clone, $this->methods);
1✔
233
                $this->properties = array_map($clone, $this->properties);
1✔
234
                $this->traits = array_map($clone, $this->traits);
1✔
235
        }
1✔
236
}
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

© 2025 Coveralls, Inc