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

nette / php-generator / 16781746162

06 Aug 2025 03:44PM UTC coverage: 92.006%. Remained the same
16781746162

push

github

dg
added support for final promoted property

14 of 14 new or added lines in 4 files covered. (100.0%)

15 existing lines in 4 files now uncovered.

1761 of 1914 relevant lines covered (92.01%)

0.92 hits per line

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

98.48
/src/PhpGenerator/Extractor.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 PhpParser;
14
use PhpParser\Modifiers;
15
use PhpParser\Node;
16
use PhpParser\NodeFinder;
17
use PhpParser\ParserFactory;
18
use function addcslashes, array_map, assert, class_exists, end, in_array, is_array, method_exists, rtrim, str_contains, str_repeat, str_replace, str_starts_with, strlen, substr, substr_replace, usort;
19

20

21
/**
22
 * Extracts information from PHP code.
23
 * @internal
24
 */
25
final class Extractor
26
{
27
        private string $code;
28

29
        /** @var Node[] */
30
        private array $statements;
31
        private PhpParser\PrettyPrinterAbstract $printer;
32

33

34
        public function __construct(string $code)
1✔
35
        {
36
                if (!class_exists(ParserFactory::class)) {
1✔
37
                        throw new Nette\NotSupportedException("PHP-Parser is required to load method bodies, install package 'nikic/php-parser' 4.7 or newer.");
×
38
                }
39

40
                $this->printer = new PhpParser\PrettyPrinter\Standard;
1✔
41
                $this->parseCode($code);
1✔
42
        }
1✔
43

44

45
        private function parseCode(string $code): void
1✔
46
        {
47
                if (!str_starts_with($code, '<?php')) {
1✔
48
                        throw new Nette\InvalidStateException('The input string is not a PHP code.');
1✔
49
                }
50

51
                $this->code = Nette\Utils\Strings::normalizeNewlines($code);
1✔
52
                $parser = (new ParserFactory)->createForNewestSupportedVersion();
1✔
53
                $stmts = $parser->parse($this->code);
1✔
54

55
                $traverser = new PhpParser\NodeTraverser;
1✔
56
                $traverser->addVisitor(new PhpParser\NodeVisitor\ParentConnectingVisitor);
1✔
57
                $traverser->addVisitor(new PhpParser\NodeVisitor\NameResolver(null, ['preserveOriginalNames' => true]));
1✔
58
                $this->statements = $traverser->traverse($stmts);
1✔
59
        }
1✔
60

61

62
        /** @return array<string, string> */
63
        public function extractMethodBodies(string $className): array
1✔
64
        {
65
                $nodeFinder = new NodeFinder;
1✔
66
                $classNode = $nodeFinder->findFirst(
1✔
67
                        $this->statements,
1✔
68
                        fn(Node $node) => $node instanceof Node\Stmt\ClassLike && $node->namespacedName->toString() === $className,
1✔
69
                );
70

71
                $res = [];
1✔
72
                foreach ($nodeFinder->findInstanceOf($classNode, Node\Stmt\ClassMethod::class) as $methodNode) {
1✔
73
                        assert($methodNode instanceof Node\Stmt\ClassMethod);
74
                        if ($methodNode->stmts) {
1✔
75
                                $res[$methodNode->name->toString()] = $this->getReformattedContents($methodNode->stmts, 2);
1✔
76
                        }
77
                }
78

79
                return $res;
1✔
80
        }
81

82

83
        /** @return array<string, array<string, array{string, bool}>> */
84
        public function extractPropertyHookBodies(string $className): array
1✔
85
        {
86
                if (!class_exists(Node\PropertyHook::class)) {
1✔
87
                        return [];
×
88
                }
89

90
                $nodeFinder = new NodeFinder;
1✔
91
                $classNode = $nodeFinder->findFirst(
1✔
92
                        $this->statements,
1✔
93
                        fn(Node $node) => $node instanceof Node\Stmt\ClassLike && $node->namespacedName->toString() === $className,
1✔
94
                );
95

96
                $res = [];
1✔
97
                foreach ($nodeFinder->findInstanceOf($classNode, Node\Stmt\Property::class) as $propertyNode) {
1✔
98
                        foreach ($propertyNode->props as $propNode) {
1✔
99
                                $propName = $propNode->name->toString();
1✔
100
                                foreach ($propertyNode->hooks as $hookNode) {
1✔
101
                                        $body = $hookNode->body;
1✔
102
                                        if ($body !== null) {
1✔
103
                                                $contents = $this->getReformattedContents(is_array($body) ? $body : [$body], 3);
1✔
104
                                                $res[$propName][$hookNode->name->toString()] = [$contents, !is_array($body)];
1✔
105
                                        }
106
                                }
107
                        }
108
                }
109
                return $res;
1✔
110
        }
111

112

113
        public function extractFunctionBody(string $name): ?string
1✔
114
        {
115
                $functionNode = (new NodeFinder)->findFirst(
1✔
116
                        $this->statements,
1✔
117
                        fn(Node $node) => $node instanceof Node\Stmt\Function_ && $node->namespacedName->toString() === $name,
1✔
118
                );
119
                assert($functionNode instanceof Node\Stmt\Function_);
120

121
                return $this->getReformattedContents($functionNode->stmts, 1);
1✔
122
        }
123

124

125
        /** @param  Node[]  $nodes */
126
        private function getReformattedContents(array $nodes, int $level): string
1✔
127
        {
128
                if (!$nodes) {
1✔
129
                        return '';
1✔
130
                }
131
                $body = $this->getNodeContents(...$nodes);
1✔
132
                $body = $this->performReplacements($body, $this->prepareReplacements($nodes, $level));
1✔
133
                return Helpers::unindent($body, $level);
1✔
134
        }
135

136

137
        /**
138
         * @param  Node[]  $nodes
139
         * @return array<array{int, int, string}>
140
         */
141
        private function prepareReplacements(array $nodes, int $level): array
1✔
142
        {
143
                $start = $this->getNodeStartPos($nodes[0]);
1✔
144
                $replacements = [];
1✔
145
                $indent = "\n" . str_repeat("\t", $level);
1✔
146
                (new NodeFinder)->find($nodes, function (Node $node) use (&$replacements, $start, $level, $indent) {
1✔
147
                        if ($node instanceof Node\Name\FullyQualified) {
1✔
148
                                if ($node->getAttribute('originalName') instanceof Node\Name) {
1✔
149
                                        $of = match (true) {
1✔
150
                                                $node->getAttribute('parent') instanceof Node\Expr\ConstFetch => PhpNamespace::NameConstant,
1✔
151
                                                $node->getAttribute('parent') instanceof Node\Expr\FuncCall => PhpNamespace::NameFunction,
1✔
152
                                                default => PhpNamespace::NameNormal,
1✔
153
                                        };
154
                                        $replacements[] = [
1✔
155
                                                $node->getStartFilePos() - $start,
1✔
156
                                                $node->getEndFilePos() - $start,
1✔
157
                                                Helpers::tagName($node->toCodeString(), $of),
1✔
158
                                        ];
159
                                }
160

161
                        } elseif (
162
                                $node instanceof Node\Scalar\String_
1✔
163
                                && in_array($node->getAttribute('kind'), [Node\Scalar\String_::KIND_SINGLE_QUOTED, Node\Scalar\String_::KIND_DOUBLE_QUOTED], true)
1✔
164
                                && str_contains($node->getAttribute('rawValue'), "\n")
1✔
165
                        ) { // multi-line strings -> single line
166
                                $replacements[] = [
1✔
167
                                        $node->getStartFilePos() - $start,
1✔
168
                                        $node->getEndFilePos() - $start,
1✔
169
                                        '"' . addcslashes($node->value, "\x00..\x1F\"") . '"',
1✔
170
                                ];
171

172
                        } elseif (
173
                                $node instanceof Node\Scalar\String_
1✔
174
                                && in_array($node->getAttribute('kind'), [Node\Scalar\String_::KIND_NOWDOC, Node\Scalar\String_::KIND_HEREDOC], true)
1✔
175
                                && Helpers::unindent($node->getAttribute('docIndentation'), $level) === $node->getAttribute('docIndentation')
1✔
176
                        ) { // fix indentation of NOWDOW/HEREDOC
177
                                $replacements[] = [
1✔
178
                                        $node->getStartFilePos() - $start,
1✔
179
                                        $node->getEndFilePos() - $start,
1✔
180
                                        str_replace("\n", $indent, $this->getNodeContents($node)),
1✔
181
                                ];
182

183
                        } elseif (
184
                                $node instanceof Node\Scalar\Encapsed
1✔
185
                                && $node->getAttribute('kind') === Node\Scalar\String_::KIND_DOUBLE_QUOTED
1✔
186
                        ) { // multi-line strings -> single line
187
                                foreach ($node->parts as $part) {
1✔
188
                                        if ($part instanceof Node\Scalar\EncapsedStringPart) {
1✔
189
                                                $replacements[] = [
1✔
190
                                                        $part->getStartFilePos() - $start,
1✔
191
                                                        $part->getEndFilePos() - $start,
1✔
192
                                                        addcslashes($part->value, "\x00..\x1F\""),
1✔
193
                                                ];
194
                                        }
195
                                }
196
                        } elseif (
197
                                $node instanceof Node\Scalar\Encapsed && $node->getAttribute('kind') === Node\Scalar\String_::KIND_HEREDOC
1✔
198
                                && Helpers::unindent($node->getAttribute('docIndentation'), $level) === $node->getAttribute('docIndentation')
1✔
199
                        ) { // fix indentation of HEREDOC
200
                                $replacements[] = [
1✔
201
                                        $tmp = $node->getStartFilePos() - $start + strlen($node->getAttribute('docLabel')) + 3, // <<<
1✔
202
                                        $tmp,
1✔
203
                                        $indent,
1✔
204
                                ];
205
                                $replacements[] = [
1✔
206
                                        $tmp = $node->getEndFilePos() - $start - strlen($node->getAttribute('docLabel')),
1✔
207
                                        $tmp,
1✔
208
                                        $indent,
1✔
209
                                ];
210
                                foreach ($node->parts as $part) {
1✔
211
                                        if ($part instanceof Node\Scalar\EncapsedStringPart) {
1✔
212
                                                $replacements[] = [
1✔
213
                                                        $part->getStartFilePos() - $start,
1✔
214
                                                        $part->getEndFilePos() - $start,
1✔
215
                                                        str_replace("\n", $indent, $this->getNodeContents($part)),
1✔
216
                                                ];
217
                                        }
218
                                }
219
                        }
220
                });
1✔
221
                return $replacements;
1✔
222
        }
223

224

225
        /** @param  array<array{int, int, string}>  $replacements */
226
        private function performReplacements(string $s, array $replacements): string
1✔
227
        {
228
                usort($replacements, fn($a, $b) => $b[0] <=> $a[0]);
1✔
229

230
                foreach ($replacements as [$start, $end, $replacement]) {
1✔
231
                        $s = substr_replace($s, $replacement, $start, $end - $start + 1);
1✔
232
                }
233

234
                return $s;
1✔
235
        }
236

237

238
        public function extractAll(): PhpFile
239
        {
240
                $phpFile = new PhpFile;
1✔
241

242
                if (
243
                        $this->statements
1✔
244
                        && !$this->statements[0] instanceof Node\Stmt\ClassLike
1✔
245
                        && !$this->statements[0] instanceof Node\Stmt\Function_
1✔
246
                ) {
247
                        $this->addCommentAndAttributes($phpFile, $this->statements[0]);
1✔
248
                }
249

250
                $namespaces = ['' => $this->statements];
1✔
251
                foreach ($this->statements as $node) {
1✔
252
                        if ($node instanceof Node\Stmt\Declare_
1✔
253
                                && $node->declares[0] instanceof Node\Stmt\DeclareDeclare
1✔
254
                                && $node->declares[0]->key->name === 'strict_types'
1✔
255
                                && $node->declares[0]->value instanceof Node\Scalar\LNumber
1✔
256
                        ) {
257
                                $phpFile->setStrictTypes((bool) $node->declares[0]->value->value);
1✔
258

259
                        } elseif ($node instanceof Node\Stmt\Namespace_) {
1✔
260
                                $namespaces[$node->name->toString()] = $node->stmts;
1✔
261
                        }
262
                }
263

264
                foreach ($namespaces as $name => $nodes) {
1✔
265
                        foreach ($nodes as $node) {
1✔
266
                                match (true) {
267
                                        $node instanceof Node\Stmt\Use_ => $this->addUseToNamespace($phpFile->addNamespace($name), $node),
1✔
268
                                        $node instanceof Node\Stmt\ClassLike => $this->addClassLikeToFile($phpFile, $node),
1✔
269
                                        $node instanceof Node\Stmt\Function_ => $this->addFunctionToFile($phpFile, $node),
1✔
270
                                        default => null,
1✔
271
                                };
272
                        }
273
                }
274

275
                return $phpFile;
1✔
276
        }
277

278

279
        private function addUseToNamespace(PhpNamespace $namespace, Node\Stmt\Use_ $node): void
1✔
280
        {
281
                $of = [
1✔
282
                        $node::TYPE_NORMAL => PhpNamespace::NameNormal,
1✔
283
                        $node::TYPE_FUNCTION => PhpNamespace::NameFunction,
1✔
284
                        $node::TYPE_CONSTANT => PhpNamespace::NameConstant,
1✔
285
                ][$node->type];
1✔
286
                foreach ($node->uses as $use) {
1✔
287
                        $namespace->addUse($use->name->toString(), $use->alias?->toString(), $of);
1✔
288
                }
289
        }
1✔
290

291

292
        private function addClassLikeToFile(PhpFile $phpFile, Node\Stmt\ClassLike $node): ClassLike
1✔
293
        {
294
                if ($node instanceof Node\Stmt\Class_) {
1✔
295
                        $class = $phpFile->addClass($node->namespacedName->toString());
1✔
296
                        $class->setFinal($node->isFinal());
1✔
297
                        $class->setAbstract($node->isAbstract());
1✔
298
                        $class->setReadOnly(method_exists($node, 'isReadonly') && $node->isReadonly());
1✔
299
                        if ($node->extends) {
1✔
300
                                $class->setExtends($node->extends->toString());
1✔
301
                        }
302
                        foreach ($node->implements as $item) {
1✔
303
                                $class->addImplement($item->toString());
1✔
304
                        }
305
                } elseif ($node instanceof Node\Stmt\Interface_) {
1✔
306
                        $class = $phpFile->addInterface($node->namespacedName->toString());
1✔
307
                        foreach ($node->extends as $item) {
1✔
308
                                $class->addExtend($item->toString());
1✔
309
                        }
310
                } elseif ($node instanceof Node\Stmt\Trait_) {
1✔
311
                        $class = $phpFile->addTrait($node->namespacedName->toString());
1✔
312

313
                } elseif ($node instanceof Node\Stmt\Enum_) {
1✔
314
                        $class = $phpFile->addEnum($node->namespacedName->toString());
1✔
315
                        $class->setType($node->scalarType?->toString());
1✔
316
                        foreach ($node->implements as $item) {
1✔
317
                                $class->addImplement($item->toString());
1✔
318
                        }
319
                }
320

321
                $this->addCommentAndAttributes($class, $node);
1✔
322
                $this->addClassMembers($class, $node);
1✔
323
                return $class;
1✔
324
        }
325

326

327
        private function addClassMembers(ClassLike $class, Node\Stmt\ClassLike $node): void
1✔
328
        {
329
                foreach ($node->stmts as $stmt) {
1✔
330
                        match (true) {
331
                                $stmt instanceof Node\Stmt\TraitUse => $this->addTraitToClass($class, $stmt),
1✔
332
                                $stmt instanceof Node\Stmt\Property => $this->addPropertyToClass($class, $stmt),
1✔
333
                                $stmt instanceof Node\Stmt\ClassMethod => $this->addMethodToClass($class, $stmt),
1✔
334
                                $stmt instanceof Node\Stmt\ClassConst => $this->addConstantToClass($class, $stmt),
1✔
335
                                $stmt instanceof Node\Stmt\EnumCase => $this->addEnumCaseToClass($class, $stmt),
1✔
336
                                default => null,
×
337
                        };
338
                }
339
        }
1✔
340

341

342
        private function addTraitToClass(ClassLike $class, Node\Stmt\TraitUse $node): void
1✔
343
        {
344
                foreach ($node->traits as $item) {
1✔
345
                        $trait = $class->addTrait($item->toString());
1✔
346
                }
347

348
                foreach ($node->adaptations as $item) {
1✔
349
                        $trait->addResolution(rtrim($this->getReformattedContents([$item], 0), ';'));
1✔
350
                }
351

352
                $this->addCommentAndAttributes($trait, $node);
1✔
353
        }
1✔
354

355

356
        private function addPropertyToClass(ClassLike $class, Node\Stmt\Property $node): void
1✔
357
        {
358
                foreach ($node->props as $item) {
1✔
359
                        $prop = $class->addProperty($item->name->toString());
1✔
360
                        $prop->setStatic($node->isStatic());
1✔
361
                        $prop->setVisibility($this->toVisibility($node->flags), $this->toSetterVisibility($node->flags));
1✔
362
                        $prop->setType($node->type ? $this->toPhp($node->type) : null);
1✔
363
                        if ($item->default) {
1✔
364
                                $prop->setValue($this->toValue($item->default));
1✔
365
                        }
366

367
                        $prop->setReadOnly((method_exists($node, 'isReadonly') && $node->isReadonly()) || ($class instanceof ClassType && $class->isReadOnly()));
1✔
368
                        $this->addCommentAndAttributes($prop, $node);
1✔
369

370
                        $prop->setAbstract((bool) ($node->flags & Node\Stmt\Class_::MODIFIER_ABSTRACT));
1✔
371
                        $prop->setFinal((bool) ($node->flags & Node\Stmt\Class_::MODIFIER_FINAL));
1✔
372
                        $this->addHooksToProperty($prop, $node);
1✔
373
                }
374
        }
1✔
375

376

377
        private function addHooksToProperty(Property|PromotedParameter $prop, Node\Stmt\Property|Node\Param $node): void
1✔
378
        {
379
                if (!class_exists(Node\PropertyHook::class)) {
1✔
380
                        return;
×
381
                }
382

383
                foreach ($node->hooks as $hookNode) {
1✔
384
                        $hook = $prop->addHook($hookNode->name->toString());
1✔
385
                        $hook->setFinal((bool) ($hookNode->flags & Modifiers::FINAL));
1✔
386
                        $this->setupFunction($hook, $hookNode);
1✔
387
                        if ($hookNode->body === null) {
1✔
388
                                $hook->setAbstract();
1✔
389
                        } elseif (!is_array($hookNode->body)) {
1✔
390
                                $hook->setBody($this->getReformattedContents([$hookNode->body], 1), short: true);
1✔
391
                        }
392
                }
393
        }
1✔
394

395

396
        private function addMethodToClass(ClassLike $class, Node\Stmt\ClassMethod $node): void
1✔
397
        {
398
                $method = $class->addMethod($node->name->toString());
1✔
399
                $method->setAbstract($node->isAbstract());
1✔
400
                $method->setFinal($node->isFinal());
1✔
401
                $method->setStatic($node->isStatic());
1✔
402
                $method->setVisibility($this->toVisibility($node->flags));
1✔
403
                $this->setupFunction($method, $node);
1✔
404
                if ($method->getName() === Method::Constructor && $class instanceof ClassType && $class->isReadOnly()) {
1✔
405
                        array_map(fn($param) => $param instanceof PromotedParameter ? $param->setReadOnly() : $param, $method->getParameters());
1✔
406
                }
407
        }
1✔
408

409

410
        private function addConstantToClass(ClassLike $class, Node\Stmt\ClassConst $node): void
1✔
411
        {
412
                foreach ($node->consts as $item) {
1✔
413
                        $const = $class->addConstant($item->name->toString(), $this->toValue($item->value));
1✔
414
                        $const->setVisibility($this->toVisibility($node->flags));
1✔
415
                        $const->setFinal(method_exists($node, 'isFinal') && $node->isFinal());
1✔
416
                        $this->addCommentAndAttributes($const, $node);
1✔
417
                }
418
        }
1✔
419

420

421
        private function addEnumCaseToClass(EnumType $class, Node\Stmt\EnumCase $node): void
1✔
422
        {
423
                $value = match (true) {
1✔
424
                        $node->expr === null => null,
1✔
425
                        $node->expr instanceof Node\Scalar\LNumber, $node->expr instanceof Node\Scalar\String_ => $node->expr->value,
1✔
426
                        default => $this->toValue($node->expr),
1✔
427
                };
428
                $case = $class->addCase($node->name->toString(), $value);
1✔
429
                $this->addCommentAndAttributes($case, $node);
1✔
430
        }
1✔
431

432

433
        private function addFunctionToFile(PhpFile $phpFile, Node\Stmt\Function_ $node): void
1✔
434
        {
435
                $function = $phpFile->addFunction($node->namespacedName->toString());
1✔
436
                $this->setupFunction($function, $node);
1✔
437
        }
1✔
438

439

440
        private function addCommentAndAttributes(
1✔
441
                PhpFile|ClassLike|Constant|Property|GlobalFunction|Method|Parameter|EnumCase|TraitUse|PropertyHook $element,
442
                Node $node,
443
        ): void
444
        {
445
                if ($node->getDocComment()) {
1✔
446
                        $comment = $node->getDocComment()->getReformattedText();
1✔
447
                        $comment = Helpers::unformatDocComment($comment);
1✔
448
                        $element->setComment($comment);
1✔
449
                        $node->setDocComment(new PhpParser\Comment\Doc(''));
1✔
450
                }
451

452
                foreach ($node->attrGroups ?? [] as $group) {
1✔
453
                        foreach ($group->attrs as $attribute) {
1✔
454
                                $args = [];
1✔
455
                                foreach ($attribute->args as $arg) {
1✔
456
                                        if ($arg->name) {
1✔
457
                                                $args[$arg->name->toString()] = $this->toValue($arg->value);
1✔
458
                                        } else {
459
                                                $args[] = $this->toValue($arg->value);
1✔
460
                                        }
461
                                }
462

463
                                $element->addAttribute($attribute->name->toString(), $args);
1✔
464
                        }
465
                }
466
        }
1✔
467

468

469
        private function setupFunction(GlobalFunction|Method|PropertyHook $function, Node\FunctionLike $node): void
1✔
470
        {
471
                $function->setReturnReference($node->returnsByRef());
1✔
472
                if (!$function instanceof PropertyHook) {
1✔
473
                        $function->setReturnType($node->getReturnType() ? $this->toPhp($node->getReturnType()) : null);
1✔
474
                }
475

476
                foreach ($node->getParams() as $item) {
1✔
477
                        $getVisibility = $this->toVisibility($item->flags);
1✔
478
                        $setVisibility = $this->toSetterVisibility($item->flags);
1✔
479
                        $final = (bool) ($item->flags & Modifiers::FINAL);
1✔
480
                        if ($getVisibility || $setVisibility || $final) {
1✔
481
                                $param = $function->addPromotedParameter($item->var->name)
1✔
482
                                        ->setVisibility($getVisibility, $setVisibility)
1✔
483
                                        ->setReadonly((bool) ($item->flags & Node\Stmt\Class_::MODIFIER_READONLY))
1✔
484
                                        ->setFinal($final);
1✔
485
                                $this->addHooksToProperty($param, $item);
1✔
486
                        } else {
487
                                $param = $function->addParameter($item->var->name);
1✔
488
                        }
489
                        $param->setType($item->type ? $this->toPhp($item->type) : null);
1✔
490
                        $param->setReference($item->byRef);
1✔
491
                        if (!$function instanceof PropertyHook) {
1✔
492
                                $function->setVariadic($item->variadic);
1✔
493
                        }
494
                        if ($item->default) {
1✔
495
                                $param->setDefaultValue($this->toValue($item->default));
1✔
496
                        }
497

498
                        $this->addCommentAndAttributes($param, $item);
1✔
499
                }
500

501
                $this->addCommentAndAttributes($function, $node);
1✔
502
                if ($node->getStmts()) {
1✔
503
                        $indent = $function instanceof GlobalFunction ? 1 : 2;
1✔
504
                        $function->setBody($this->getReformattedContents($node->getStmts(), $indent));
1✔
505
                }
506
        }
1✔
507

508

509
        private function toValue(Node\Expr $node): mixed
1✔
510
        {
511
                if ($node instanceof Node\Expr\ConstFetch) {
1✔
512
                        return match ($node->name->toLowerString()) {
1✔
513
                                'null' => null,
1✔
514
                                'true' => true,
1✔
515
                                'false' => false,
1✔
516
                                default => new Literal($this->getReformattedContents([$node], 0)),
1✔
517
                        };
518
                } elseif ($node instanceof Node\Scalar\LNumber
1✔
519
                        || $node instanceof Node\Scalar\DNumber
1✔
520
                        || $node instanceof Node\Scalar\String_
1✔
521
                ) {
522
                        return $node->value;
1✔
523

524
                } elseif ($node instanceof Node\Expr\Array_) {
1✔
525
                        $res = [];
1✔
526
                        foreach ($node->items as $item) {
1✔
527
                                if ($item->unpack) {
1✔
528
                                        return new Literal($this->getReformattedContents([$node], 0));
1✔
529

530
                                } elseif ($item->key) {
1✔
531
                                        $key = $item->key instanceof Node\Identifier
1✔
UNCOV
532
                                                ? $item->key->name
×
533
                                                : $this->toValue($item->key);
1✔
534

535
                                        if ($key instanceof Literal) {
1✔
536
                                                return new Literal($this->getReformattedContents([$node], 0));
1✔
537
                                        }
538

539
                                        $res[$key] = $this->toValue($item->value);
1✔
540

541
                                } else {
542
                                        $res[] = $this->toValue($item->value);
1✔
543
                                }
544
                        }
545
                        return $res;
1✔
546

547
                } else {
548
                        return new Literal($this->getReformattedContents([$node], 0));
1✔
549
                }
550
        }
551

552

553
        private function toVisibility(int $flags): ?string
1✔
554
        {
555
                return match (true) {
556
                        (bool) ($flags & Node\Stmt\Class_::MODIFIER_PUBLIC) => Visibility::Public,
1✔
557
                        (bool) ($flags & Node\Stmt\Class_::MODIFIER_PROTECTED) => Visibility::Protected,
1✔
558
                        (bool) ($flags & Node\Stmt\Class_::MODIFIER_PRIVATE) => Visibility::Private,
1✔
559
                        default => null,
1✔
560
                };
561
        }
562

563

564
        private function toSetterVisibility(int $flags): ?string
1✔
565
        {
566
                return match (true) {
567
                        !class_exists(Node\PropertyHook::class) => null,
1✔
568
                        (bool) ($flags & Modifiers::PUBLIC_SET) => Visibility::Public,
1✔
569
                        (bool) ($flags & Modifiers::PROTECTED_SET) => Visibility::Protected,
1✔
570
                        (bool) ($flags & Modifiers::PRIVATE_SET) => Visibility::Private,
1✔
571
                        default => null,
1✔
572
                };
573
        }
574

575

576
        private function toPhp(Node $value): string
1✔
577
        {
578
                $dolly = clone $value;
1✔
579
                $dolly->setAttribute('comments', []);
1✔
580
                return $this->printer->prettyPrint([$dolly]);
1✔
581
        }
582

583

584
        private function getNodeContents(Node ...$nodes): string
1✔
585
        {
586
                $start = $this->getNodeStartPos($nodes[0]);
1✔
587
                return substr($this->code, $start, end($nodes)->getEndFilePos() - $start + 1);
1✔
588
        }
589

590

591
        private function getNodeStartPos(Node $node): int
1✔
592
        {
593
                return ($comments = $node->getComments())
1✔
594
                        ? $comments[0]->getStartFilePos()
1✔
595
                        : $node->getStartFilePos();
1✔
596
        }
597
}
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