• 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

87.5
/src/PhpGenerator/Factory.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 Nette\Utils\Reflection;
14
use function array_diff, array_filter, array_key_exists, array_map, count, explode, file_get_contents, implode, is_object, is_subclass_of, method_exists, reset;
15
use const PHP_VERSION_ID;
16

17

18
/**
19
 * Creates a representations based on reflection or source code.
20
 */
21
final class Factory
22
{
23
        /** @var array<string, array<string, string>> */
24
        private array $bodyCache = [];
25

26
        /** @var array<string, Extractor> */
27
        private array $extractorCache = [];
28

29

30
        /** @param  \ReflectionClass<object>  $from */
31
        public function fromClassReflection(
1✔
32
                \ReflectionClass $from,
33
                bool $withBodies = false,
34
        ): ClassLike
35
        {
36
                if ($withBodies && ($from->isAnonymous() || $from->isInternal() || $from->isInterface())) {
1✔
37
                        throw new Nette\NotSupportedException('The $withBodies parameter cannot be used for anonymous or internal classes or interfaces.');
1✔
38
                }
39

40
                $class = $this->createClassObject($from);
1✔
41
                $this->setupInheritance($class, $from);
1✔
42
                $this->populateMembers($class, $from, $withBodies);
1✔
43
                return $class;
1✔
44
        }
45

46

47
        /** @param \ReflectionClass<object> $from */
48
        private function createClassObject(\ReflectionClass &$from): ClassLike
1✔
49
        {
50
                if ($from->isAnonymous()) {
1✔
51
                        return new ClassType;
1✔
52
                } elseif ($from->isEnum()) {
1✔
53
                        $from = new \ReflectionEnum($from->getName());
1✔
54
                        $class = new EnumType($from->getName());
1✔
55
                } elseif ($from->isInterface()) {
1✔
56
                        $class = new InterfaceType($from->getName());
1✔
57
                } elseif ($from->isTrait()) {
1✔
58
                        $class = new TraitType($from->getName());
1✔
59
                } else {
60
                        $class = new ClassType($from->getShortName());
1✔
61
                        $class->setFinal($from->isFinal() && $class->isClass());
1✔
62
                        $class->setAbstract($from->isAbstract() && $class->isClass());
1✔
63
                        $class->setReadOnly(PHP_VERSION_ID >= 80200 && $from->isReadOnly());
1✔
64
                }
65

66
                (new PhpNamespace($from->getNamespaceName()))->add($class);
1✔
67
                return $class;
1✔
68
        }
69

70

71
        /** @param \ReflectionClass<object> $from */
72
        private function setupInheritance(ClassLike $class, \ReflectionClass $from): void
1✔
73
        {
74
                $ifaces = $from->getInterfaceNames();
1✔
75
                foreach ($ifaces as $iface) {
1✔
76
                        $ifaces = array_filter($ifaces, fn(string $item): bool => !is_subclass_of($iface, $item));
1✔
77
                }
78

79
                if ($from->isInterface()) {
1✔
80
                        assert($class instanceof InterfaceType);
81
                        $class->setExtends(array_values($ifaces));
1✔
82
                } elseif ($ifaces) {
1✔
83
                        assert($class instanceof ClassType || $class instanceof EnumType);
84
                        $ifaces = array_diff($ifaces, [\BackedEnum::class, \UnitEnum::class]);
1✔
85
                        $class->setImplements(array_values($ifaces));
1✔
86
                }
87

88
                $class->setComment(Helpers::unformatDocComment((string) $from->getDocComment()));
1✔
89
                $class->setAttributes($this->formatAttributes($from->getAttributes()));
1✔
90
                if ($from->getParentClass()) {
1✔
91
                        assert($class instanceof ClassType);
92
                        $class->setExtends($from->getParentClass()->name);
1✔
93
                        $class->setImplements(array_values(array_diff($class->getImplements(), $from->getParentClass()->getInterfaceNames())));
1✔
94
                }
95
        }
1✔
96

97

98
        /** @param \ReflectionClass<object> $from */
99
        private function populateMembers(ClassLike $class, \ReflectionClass $from, bool $withBodies): void
1✔
100
        {
101
                // Properties
102
                $props = [];
1✔
103
                foreach ($from->getProperties() as $prop) {
1✔
104
                        $declaringClass = Reflection::getPropertyDeclaringClass($prop);
1✔
105

106
                        if ($prop->isDefault()
1✔
107
                                && $declaringClass->name === $from->name
1✔
108
                                && !$prop->isPromoted()
1✔
109
                                && !$class->isEnum()
1✔
110
                        ) {
111
                                $props[] = $p = $this->fromPropertyReflection($prop);
1✔
112
                                if ($withBodies && ($file = $declaringClass->getFileName())) {
1✔
113
                                        $hookBodies ??= $this->getExtractor($file)->extractPropertyHookBodies($declaringClass->name);
1✔
114
                                        foreach ($hookBodies[$prop->getName()] ?? [] as $hookType => [$body, $short]) {
1✔
115
                                                $p->getHook($hookType)->setBody($body, short: $short);
×
116
                                        }
117
                                }
118
                        }
119
                }
120

121
                if ($props) {
1✔
122
                        assert($class instanceof ClassType || $class instanceof InterfaceType || $class instanceof TraitType);
123
                        $class->setProperties($props);
1✔
124
                }
125

126
                // Methods and trait resolutions
127
                $methods = $resolutions = [];
1✔
128
                foreach ($from->getMethods() as $method) {
1✔
129
                        $declaringMethod = Reflection::getMethodDeclaringMethod($method);
1✔
130
                        $declaringClass = $declaringMethod->getDeclaringClass();
1✔
131

132
                        if (
133
                                $declaringClass->name === $from->name
1✔
134
                                && (!$from instanceof \ReflectionEnum || !method_exists($from->isBacked() ? \BackedEnum::class : \UnitEnum::class, $method->name))
1✔
135
                        ) {
136
                                $methods[] = $m = $this->fromMethodReflection($method);
1✔
137
                                if ($withBodies && ($file = $declaringClass->getFileName())) {
1✔
138
                                        $bodies = &$this->bodyCache[$declaringClass->name];
1✔
139
                                        $bodies ??= $this->getExtractor($file)->extractMethodBodies($declaringClass->name);
1✔
140
                                        if (isset($bodies[$declaringMethod->name])) {
1✔
141
                                                $m->setBody($bodies[$declaringMethod->name]);
1✔
142
                                        }
143
                                }
144
                        }
145

146
                        $modifier = $declaringMethod->getModifiers() !== $method->getModifiers()
1✔
147
                                ? ' ' . $this->getVisibility($method)->value
1✔
148
                                : null;
1✔
149
                        $alias = $declaringMethod->name !== $method->name ? ' ' . $method->name : '';
1✔
150
                        if ($modifier || $alias) {
1✔
151
                                $resolutions[] = $declaringMethod->name . ' as' . $modifier . $alias;
1✔
152
                        }
153
                }
154

155
                assert($class instanceof ClassType || $class instanceof InterfaceType || $class instanceof TraitType || $class instanceof EnumType);
156
                $class->setMethods($methods);
1✔
157

158
                // Traits
159
                foreach ($from->getTraitNames() as $trait) {
1✔
160
                        assert($class instanceof ClassType || $class instanceof TraitType || $class instanceof EnumType);
161
                        $trait = $class->addTrait($trait);
1✔
162
                        foreach ($resolutions as $resolution) {
1✔
163
                                $trait->addResolution($resolution);
1✔
164
                        }
165
                        $resolutions = [];
1✔
166
                }
167

168
                // Constants and enum cases
169
                $consts = $cases = [];
1✔
170
                foreach ($from->getReflectionConstants() as $const) {
1✔
171
                        if ($from instanceof \ReflectionEnum && $from->hasCase($const->name)) {
1✔
172
                                $cases[] = $this->fromCaseReflection($const);
1✔
173
                        } elseif ($const->getDeclaringClass()->name === $from->name) {
1✔
174
                                $consts[] = $this->fromConstantReflection($const);
1✔
175
                        }
176
                }
177

178
                if ($consts) {
1✔
179
                        $class->setConstants($consts);
1✔
180
                }
181
                if ($cases) {
1✔
182
                        assert($class instanceof EnumType);
183
                        $class->setCases($cases);
1✔
184
                }
185
        }
1✔
186

187

188
        public function fromMethodReflection(\ReflectionMethod $from): Method
1✔
189
        {
190
                $method = new Method($from->name);
1✔
191
                $method->setParameters(array_map($this->fromParameterReflection(...), $from->getParameters()));
1✔
192
                $method->setStatic($from->isStatic());
1✔
193
                $isInterface = $from->getDeclaringClass()->isInterface();
1✔
194
                $method->setVisibility($isInterface ? null : $this->getVisibility($from));
1✔
195
                $method->setFinal($from->isFinal());
1✔
196
                $method->setAbstract($from->isAbstract() && !$isInterface);
1✔
197
                $method->setReturnReference($from->returnsReference());
1✔
198
                $method->setVariadic($from->isVariadic());
1✔
199
                $method->setComment(Helpers::unformatDocComment((string) $from->getDocComment()));
1✔
200
                $method->setAttributes($this->formatAttributes($from->getAttributes()));
1✔
201
                $method->setReturnType((string) $from->getReturnType());
1✔
202

203
                return $method;
1✔
204
        }
205

206

207
        public function fromFunctionReflection(\ReflectionFunction $from, bool $withBody = false): GlobalFunction|Closure
1✔
208
        {
209
                $function = $from->isClosure() ? new Closure : new GlobalFunction($from->name);
1✔
210
                $function->setParameters(array_map($this->fromParameterReflection(...), $from->getParameters()));
1✔
211
                $function->setReturnReference($from->returnsReference());
1✔
212
                $function->setVariadic($from->isVariadic());
1✔
213
                if (!$from->isClosure()) {
1✔
214
                        assert($function instanceof GlobalFunction);
215
                        $function->setComment(Helpers::unformatDocComment((string) $from->getDocComment()));
1✔
216
                }
217

218
                $function->setAttributes($this->formatAttributes($from->getAttributes()));
1✔
219
                $function->setReturnType((string) $from->getReturnType());
1✔
220

221
                if ($withBody) {
1✔
222
                        if ($from->isClosure() || $from->isInternal() || !($file = $from->getFileName())) {
1✔
223
                                throw new Nette\NotSupportedException('The $withBody parameter cannot be used for closures or internal functions.');
×
224
                        }
225

226
                        $function->setBody($this->getExtractor($file)->extractFunctionBody($from->name));
1✔
227
                }
228

229
                return $function;
1✔
230
        }
231

232

233
        /** @param callable(): mixed $from */
234
        public function fromCallable(callable $from): Method|GlobalFunction|Closure
1✔
235
        {
236
                $ref = Nette\Utils\Callback::toReflection($from);
1✔
237
                return $ref instanceof \ReflectionMethod
1✔
238
                        ? $this->fromMethodReflection($ref)
1✔
239
                        : $this->fromFunctionReflection($ref);
1✔
240
        }
241

242

243
        public function fromParameterReflection(\ReflectionParameter $from): Parameter
1✔
244
        {
245
                if ($from->isPromoted()) {
1✔
246
                        $property = $from->getDeclaringClass()->getProperty($from->name);
1✔
247
                        $param = (new PromotedParameter($from->name))
1✔
248
                                ->setVisibility($this->getVisibility($property))
1✔
249
                                ->setReadOnly($property->isReadonly())
1✔
250
                                ->setFinal(PHP_VERSION_ID >= 80500 && $property->isFinal() && !$property->isPrivateSet());
1✔
251
                        $this->addHooks($property, $param);
1✔
252
                } else {
253
                        $param = new Parameter($from->name);
1✔
254
                }
255
                $param->setReference($from->isPassedByReference());
1✔
256
                $param->setType((string) $from->getType());
1✔
257

258
                if ($from->isDefaultValueAvailable()) {
1✔
259
                        if ($from->isDefaultValueConstant()) {
1✔
260
                                $parts = explode('::', $from->getDefaultValueConstantName());
1✔
261
                                if (count($parts) > 1) {
1✔
262
                                        $parts[0] = Helpers::tagName($parts[0]);
1✔
263
                                }
264

265
                                $param->setDefaultValue(new Literal(implode('::', $parts)));
1✔
266
                        } elseif (is_object($from->getDefaultValue())) {
1✔
267
                                $param->setDefaultValue($this->fromObject($from->getDefaultValue()));
1✔
268
                        } else {
269
                                $param->setDefaultValue($from->getDefaultValue());
1✔
270
                        }
271
                }
272

273
                $param->setAttributes($this->formatAttributes($from->getAttributes()));
1✔
274
                return $param;
1✔
275
        }
276

277

278
        public function fromConstantReflection(\ReflectionClassConstant $from): Constant
1✔
279
        {
280
                $const = new Constant($from->name);
1✔
281
                $const->setValue($from->getValue());
1✔
282
                $const->setVisibility($this->getVisibility($from));
1✔
283
                $const->setFinal($from->isFinal());
1✔
284
                $const->setComment(Helpers::unformatDocComment((string) $from->getDocComment()));
1✔
285
                $const->setAttributes($this->formatAttributes($from->getAttributes()));
1✔
286
                return $const;
1✔
287
        }
288

289

290
        public function fromCaseReflection(\ReflectionClassConstant $from): EnumCase
1✔
291
        {
292
                $const = new EnumCase($from->name);
1✔
293
                $const->setValue($from->getValue()->value ?? null);
1✔
294
                $const->setComment(Helpers::unformatDocComment((string) $from->getDocComment()));
1✔
295
                $const->setAttributes($this->formatAttributes($from->getAttributes()));
1✔
296
                return $const;
1✔
297
        }
298

299

300
        public function fromPropertyReflection(\ReflectionProperty $from): Property
1✔
301
        {
302
                $defaults = $from->getDeclaringClass()->getDefaultProperties();
1✔
303
                $prop = new Property($from->name);
1✔
304
                $prop->setValue($defaults[$prop->getName()] ?? null);
1✔
305
                $prop->setStatic($from->isStatic());
1✔
306
                $prop->setVisibility($this->getVisibility($from));
1✔
307
                $prop->setType((string) $from->getType());
1✔
308
                $prop->setInitialized($from->hasType() && array_key_exists($prop->getName(), $defaults));
1✔
309
                $prop->setReadOnly($from->isReadOnly());
1✔
310
                $prop->setComment(Helpers::unformatDocComment((string) $from->getDocComment()));
1✔
311
                $prop->setAttributes($this->formatAttributes($from->getAttributes()));
1✔
312

313
                if (PHP_VERSION_ID >= 80400) {
1✔
314
                        $this->addHooks($from, $prop);
×
315
                        $isInterface = $from->getDeclaringClass()->isInterface();
×
316
                        $prop->setFinal($from->isFinal() && !$prop->isPrivate(PropertyAccessMode::Set));
×
317
                        $prop->setAbstract($from->isAbstract() && !$isInterface);
×
318
                }
319
                return $prop;
1✔
320
        }
321

322

323
        private function addHooks(\ReflectionProperty $from, Property|PromotedParameter $prop): void
1✔
324
        {
325
                if (PHP_VERSION_ID < 80400) {
1✔
326
                        return;
1✔
327
                }
328

329
                $getV = $this->getVisibility($from);
×
330
                $setV = $from->isPrivateSet()
×
331
                        ? Visibility::Private
×
332
                        : ($from->isProtectedSet() ? Visibility::Protected : $getV);
×
333
                $defaultSetV = $from->isReadOnly() && $getV !== Visibility::Private
×
334
                        ? Visibility::Protected
×
335
                        : $getV;
×
336
                if ($setV !== $defaultSetV) {
×
337
                        $prop->setVisibility($getV === Visibility::Public ? null : $getV, $setV);
×
338
                }
339

340
                foreach ($from->getHooks() as $type => $hook) {
×
341
                        $params = $hook->getParameters();
×
342
                        if (
343
                                count($params) === 1
×
344
                                && $params[0]->getName() === 'value'
×
345
                                && $params[0]->getType() == $from->getType() // intentionally ==
×
346
                        ) {
347
                                $params = [];
×
348
                        }
349
                        $prop->addHook($type)
×
350
                                ->setParameters(array_map($this->fromParameterReflection(...), $params))
×
351
                                ->setAbstract($hook->isAbstract())
×
352
                                ->setFinal($hook->isFinal())
×
353
                                ->setReturnReference($hook->returnsReference())
×
354
                                ->setComment(Helpers::unformatDocComment((string) $hook->getDocComment()))
×
355
                                ->setAttributes($this->formatAttributes($hook->getAttributes()));
×
356
                }
357
        }
358

359

360
        public function fromObject(object $obj): Literal
1✔
361
        {
362
                return new Literal('new \\' . $obj::class . '(/* unknown */)');
1✔
363
        }
364

365

366
        public function fromClassCode(string $code): ClassLike
1✔
367
        {
368
                $classes = $this->fromCode($code)->getClasses();
1✔
369
                return reset($classes) ?: throw new Nette\InvalidStateException('The code does not contain any class.');
1✔
370
        }
371

372

373
        public function fromCode(string $code): PhpFile
1✔
374
        {
375
                $reader = new Extractor($code);
1✔
376
                return $reader->extractAll();
1✔
377
        }
378

379

380
        /**
381
         * @param  list<\ReflectionAttribute<object>>  $attrs
382
         * @return list<Attribute>
383
         */
384
        private function formatAttributes(array $attrs): array
1✔
385
        {
386
                $res = [];
1✔
387
                foreach ($attrs as $attr) {
1✔
388
                        $args = $attr->getArguments();
1✔
389
                        foreach ($args as &$arg) {
1✔
390
                                if (is_object($arg)) {
1✔
391
                                        $arg = $this->fromObject($arg);
1✔
392
                                }
393
                        }
394
                        $res[] = new Attribute($attr->getName(), $args);
1✔
395
                }
396
                return $res;
1✔
397
        }
398

399

400
        private function getVisibility(\ReflectionProperty|\ReflectionMethod|\ReflectionClassConstant $from): Visibility
1✔
401
        {
402
                return $from->isPrivate()
1✔
403
                        ? Visibility::Private
1✔
404
                        : ($from->isProtected() ? Visibility::Protected : Visibility::Public);
1✔
405
        }
406

407

408
        private function getExtractor(string $file): Extractor
1✔
409
        {
410
                $cache = &$this->extractorCache[$file];
1✔
411
                $cache ??= new Extractor(file_get_contents($file) ?: throw new Nette\InvalidStateException("Unable to read file '$file'."));
1✔
412
                return $cache;
1✔
413
        }
414
}
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