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

nette / php-generator / 12077616009

29 Nov 2024 01:41AM UTC coverage: 91.985%. First build
12077616009

push

github

dg
Factory & Extractor: added support for property hooks & asymmetric visibility

39 of 66 new or added lines in 2 files covered. (59.09%)

1733 of 1884 relevant lines covered (91.99%)

0.92 hits per line

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

98.71
/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

19

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

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

32

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

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

43

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

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

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

60

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

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

78
                return $res;
1✔
79
        }
80

81

82
        public function extractFunctionBody(string $name): ?string
1✔
83
        {
84
                $functionNode = (new NodeFinder)->findFirst(
1✔
85
                        $this->statements,
1✔
86
                        fn(Node $node) => $node instanceof Node\Stmt\Function_ && $node->namespacedName->toString() === $name,
1✔
87
                );
88
                assert($functionNode instanceof Node\Stmt\Function_);
89

90
                return $this->getReformattedContents($functionNode->stmts, 1);
1✔
91
        }
92

93

94
        /** @param  Node[]  $nodes */
95
        private function getReformattedContents(array $nodes, int $level): string
1✔
96
        {
97
                $body = $this->getNodeContents(...$nodes);
1✔
98
                $body = $this->performReplacements($body, $this->prepareReplacements($nodes, $level));
1✔
99
                return Helpers::unindent($body, $level);
1✔
100
        }
101

102

103
        /**
104
         * @param  Node[]  $nodes
105
         * @return array<array{int, int, string}>
106
         */
107
        private function prepareReplacements(array $nodes, int $level): array
1✔
108
        {
109
                $start = $this->getNodeStartPos($nodes[0]);
1✔
110
                $replacements = [];
1✔
111
                $indent = "\n" . str_repeat("\t", $level);
1✔
112
                (new NodeFinder)->find($nodes, function (Node $node) use (&$replacements, $start, $level, $indent) {
1✔
113
                        if ($node instanceof Node\Name\FullyQualified) {
1✔
114
                                if ($node->getAttribute('originalName') instanceof Node\Name) {
1✔
115
                                        $of = match (true) {
1✔
116
                                                $node->getAttribute('parent') instanceof Node\Expr\ConstFetch => PhpNamespace::NameConstant,
1✔
117
                                                $node->getAttribute('parent') instanceof Node\Expr\FuncCall => PhpNamespace::NameFunction,
1✔
118
                                                default => PhpNamespace::NameNormal,
1✔
119
                                        };
120
                                        $replacements[] = [
1✔
121
                                                $node->getStartFilePos() - $start,
1✔
122
                                                $node->getEndFilePos() - $start,
1✔
123
                                                Helpers::tagName($node->toCodeString(), $of),
1✔
124
                                        ];
125
                                }
126

127
                        } elseif (
128
                                $node instanceof Node\Scalar\String_
1✔
129
                                && in_array($node->getAttribute('kind'), [Node\Scalar\String_::KIND_SINGLE_QUOTED, Node\Scalar\String_::KIND_DOUBLE_QUOTED], true)
1✔
130
                                && str_contains($node->getAttribute('rawValue'), "\n")
1✔
131
                        ) { // multi-line strings -> single line
132
                                $replacements[] = [
1✔
133
                                        $node->getStartFilePos() - $start,
1✔
134
                                        $node->getEndFilePos() - $start,
1✔
135
                                        '"' . addcslashes($node->value, "\x00..\x1F\"") . '"',
1✔
136
                                ];
137

138
                        } elseif (
139
                                $node instanceof Node\Scalar\String_
1✔
140
                                && in_array($node->getAttribute('kind'), [Node\Scalar\String_::KIND_NOWDOC, Node\Scalar\String_::KIND_HEREDOC], true)
1✔
141
                                && Helpers::unindent($node->getAttribute('docIndentation'), $level) === $node->getAttribute('docIndentation')
1✔
142
                        ) { // fix indentation of NOWDOW/HEREDOC
143
                                $replacements[] = [
1✔
144
                                        $node->getStartFilePos() - $start,
1✔
145
                                        $node->getEndFilePos() - $start,
1✔
146
                                        str_replace("\n", $indent, $this->getNodeContents($node)),
1✔
147
                                ];
148

149
                        } elseif (
150
                                $node instanceof Node\Scalar\Encapsed
1✔
151
                                && $node->getAttribute('kind') === Node\Scalar\String_::KIND_DOUBLE_QUOTED
1✔
152
                        ) { // multi-line strings -> single line
153
                                foreach ($node->parts as $part) {
1✔
154
                                        if ($part instanceof Node\Scalar\EncapsedStringPart) {
1✔
155
                                                $replacements[] = [
1✔
156
                                                        $part->getStartFilePos() - $start,
1✔
157
                                                        $part->getEndFilePos() - $start,
1✔
158
                                                        addcslashes($part->value, "\x00..\x1F\""),
1✔
159
                                                ];
160
                                        }
161
                                }
162
                        } elseif (
163
                                $node instanceof Node\Scalar\Encapsed && $node->getAttribute('kind') === Node\Scalar\String_::KIND_HEREDOC
1✔
164
                                && Helpers::unindent($node->getAttribute('docIndentation'), $level) === $node->getAttribute('docIndentation')
1✔
165
                        ) { // fix indentation of HEREDOC
166
                                $replacements[] = [
1✔
167
                                        $tmp = $node->getStartFilePos() - $start + strlen($node->getAttribute('docLabel')) + 3, // <<<
1✔
168
                                        $tmp,
1✔
169
                                        $indent,
1✔
170
                                ];
171
                                $replacements[] = [
1✔
172
                                        $tmp = $node->getEndFilePos() - $start - strlen($node->getAttribute('docLabel')),
1✔
173
                                        $tmp,
1✔
174
                                        $indent,
1✔
175
                                ];
176
                                foreach ($node->parts as $part) {
1✔
177
                                        if ($part instanceof Node\Scalar\EncapsedStringPart) {
1✔
178
                                                $replacements[] = [
1✔
179
                                                        $part->getStartFilePos() - $start,
1✔
180
                                                        $part->getEndFilePos() - $start,
1✔
181
                                                        str_replace("\n", $indent, $this->getNodeContents($part)),
1✔
182
                                                ];
183
                                        }
184
                                }
185
                        }
186
                });
1✔
187
                return $replacements;
1✔
188
        }
189

190

191
        /** @param  array<array{int, int, string}>  $replacements */
192
        private function performReplacements(string $s, array $replacements): string
1✔
193
        {
194
                usort($replacements, fn($a, $b) => $b[0] <=> $a[0]);
1✔
195

196
                foreach ($replacements as [$start, $end, $replacement]) {
1✔
197
                        $s = substr_replace($s, $replacement, $start, $end - $start + 1);
1✔
198
                }
199

200
                return $s;
1✔
201
        }
202

203

204
        public function extractAll(): PhpFile
205
        {
206
                $phpFile = new PhpFile;
1✔
207

208
                if (
209
                        $this->statements
1✔
210
                        && !$this->statements[0] instanceof Node\Stmt\ClassLike
1✔
211
                        && !$this->statements[0] instanceof Node\Stmt\Function_
1✔
212
                ) {
213
                        $this->addCommentAndAttributes($phpFile, $this->statements[0]);
1✔
214
                }
215

216
                $namespaces = ['' => $this->statements];
1✔
217
                foreach ($this->statements as $node) {
1✔
218
                        if ($node instanceof Node\Stmt\Declare_
1✔
219
                                && $node->declares[0] instanceof Node\Stmt\DeclareDeclare
1✔
220
                                && $node->declares[0]->key->name === 'strict_types'
1✔
221
                                && $node->declares[0]->value instanceof Node\Scalar\LNumber
1✔
222
                        ) {
223
                                $phpFile->setStrictTypes((bool) $node->declares[0]->value->value);
1✔
224

225
                        } elseif ($node instanceof Node\Stmt\Namespace_) {
1✔
226
                                $namespaces[$node->name->toString()] = $node->stmts;
1✔
227
                        }
228
                }
229

230
                foreach ($namespaces as $name => $nodes) {
1✔
231
                        foreach ($nodes as $node) {
1✔
232
                                match (true) {
233
                                        $node instanceof Node\Stmt\Use_ => $this->addUseToNamespace($phpFile->addNamespace($name), $node),
1✔
234
                                        $node instanceof Node\Stmt\ClassLike => $this->addClassLikeToFile($phpFile, $node),
1✔
235
                                        $node instanceof Node\Stmt\Function_ => $this->addFunctionToFile($phpFile, $node),
1✔
236
                                        default => null,
1✔
237
                                };
238
                        }
239
                }
240

241
                return $phpFile;
1✔
242
        }
243

244

245
        private function addUseToNamespace(PhpNamespace $namespace, Node\Stmt\Use_ $node): void
1✔
246
        {
247
                $of = [
1✔
248
                        $node::TYPE_NORMAL => PhpNamespace::NameNormal,
1✔
249
                        $node::TYPE_FUNCTION => PhpNamespace::NameFunction,
1✔
250
                        $node::TYPE_CONSTANT => PhpNamespace::NameConstant,
1✔
251
                ][$node->type];
1✔
252
                foreach ($node->uses as $use) {
1✔
253
                        $namespace->addUse($use->name->toString(), $use->alias?->toString(), $of);
1✔
254
                }
255
        }
1✔
256

257

258
        private function addClassLikeToFile(PhpFile $phpFile, Node\Stmt\ClassLike $node): ClassLike
1✔
259
        {
260
                if ($node instanceof Node\Stmt\Class_) {
1✔
261
                        $class = $phpFile->addClass($node->namespacedName->toString());
1✔
262
                        $class->setFinal($node->isFinal());
1✔
263
                        $class->setAbstract($node->isAbstract());
1✔
264
                        $class->setReadOnly(method_exists($node, 'isReadonly') && $node->isReadonly());
1✔
265
                        if ($node->extends) {
1✔
266
                                $class->setExtends($node->extends->toString());
1✔
267
                        }
268
                        foreach ($node->implements as $item) {
1✔
269
                                $class->addImplement($item->toString());
1✔
270
                        }
271
                } elseif ($node instanceof Node\Stmt\Interface_) {
1✔
272
                        $class = $phpFile->addInterface($node->namespacedName->toString());
1✔
273
                        foreach ($node->extends as $item) {
1✔
274
                                $class->addExtend($item->toString());
1✔
275
                        }
276
                } elseif ($node instanceof Node\Stmt\Trait_) {
1✔
277
                        $class = $phpFile->addTrait($node->namespacedName->toString());
1✔
278

279
                } elseif ($node instanceof Node\Stmt\Enum_) {
1✔
280
                        $class = $phpFile->addEnum($node->namespacedName->toString());
1✔
281
                        $class->setType($node->scalarType?->toString());
1✔
282
                        foreach ($node->implements as $item) {
1✔
283
                                $class->addImplement($item->toString());
1✔
284
                        }
285
                }
286

287
                $this->addCommentAndAttributes($class, $node);
1✔
288
                $this->addClassMembers($class, $node);
1✔
289
                return $class;
1✔
290
        }
291

292

293
        private function addClassMembers(ClassLike $class, Node\Stmt\ClassLike $node): void
1✔
294
        {
295
                foreach ($node->stmts as $stmt) {
1✔
296
                        match (true) {
297
                                $stmt instanceof Node\Stmt\TraitUse => $this->addTraitToClass($class, $stmt),
1✔
298
                                $stmt instanceof Node\Stmt\Property => $this->addPropertyToClass($class, $stmt),
1✔
299
                                $stmt instanceof Node\Stmt\ClassMethod => $this->addMethodToClass($class, $stmt),
1✔
300
                                $stmt instanceof Node\Stmt\ClassConst => $this->addConstantToClass($class, $stmt),
1✔
301
                                $stmt instanceof Node\Stmt\EnumCase => $this->addEnumCaseToClass($class, $stmt),
1✔
302
                                default => null,
×
303
                        };
304
                }
305
        }
1✔
306

307

308
        private function addTraitToClass(ClassLike $class, Node\Stmt\TraitUse $node): void
1✔
309
        {
310
                foreach ($node->traits as $item) {
1✔
311
                        $trait = $class->addTrait($item->toString());
1✔
312
                }
313

314
                foreach ($node->adaptations as $item) {
1✔
315
                        $trait->addResolution(rtrim($this->getReformattedContents([$item], 0), ';'));
1✔
316
                }
317

318
                $this->addCommentAndAttributes($trait, $node);
1✔
319
        }
1✔
320

321

322
        private function addPropertyToClass(ClassLike $class, Node\Stmt\Property $node): void
1✔
323
        {
324
                foreach ($node->props as $item) {
1✔
325
                        $prop = $class->addProperty($item->name->toString());
1✔
326
                        $prop->setStatic($node->isStatic());
1✔
327
                        $prop->setVisibility($this->toVisibility($node->flags), $this->toSetterVisibility($node->flags));
1✔
328
                        $prop->setType($node->type ? $this->toPhp($node->type) : null);
1✔
329
                        if ($item->default) {
1✔
330
                                $prop->setValue($this->toValue($item->default));
1✔
331
                        }
332

333
                        $prop->setReadOnly((method_exists($node, 'isReadonly') && $node->isReadonly()) || ($class instanceof ClassType && $class->isReadOnly()));
1✔
334
                        $this->addCommentAndAttributes($prop, $node);
1✔
335

336
                        $prop->setAbstract((bool) ($node->flags & Node\Stmt\Class_::MODIFIER_ABSTRACT));
1✔
337
                        $prop->setFinal((bool) ($node->flags & Node\Stmt\Class_::MODIFIER_FINAL));
1✔
338
                        $this->addHooksToProperty($prop, $node);
1✔
339
                }
340
        }
1✔
341

342

343
        private function addHooksToProperty(Property|PromotedParameter $prop, Node\Stmt\Property|Node\Param $node): void
1✔
344
        {
345
                if (!class_exists(Node\PropertyHook::class)) {
1✔
NEW
346
                        return;
×
347
                }
348

349
                foreach ($node->hooks as $hookNode) {
1✔
350
                        $hook = $prop->addHook($hookNode->name->toString());
1✔
351
                        $hook->setFinal((bool) ($hookNode->flags & Modifiers::FINAL));
1✔
352
                        $this->setupFunction($hook, $hookNode);
1✔
353
                        if ($hookNode->body === null) {
1✔
354
                                $hook->setAbstract();
1✔
355
                        } elseif (!is_array($hookNode->body)) {
1✔
356
                                $hook->setBody($this->getReformattedContents([$hookNode->body], 1), short: true);
1✔
357
                        }
358
                }
359
        }
1✔
360

361

362
        private function addMethodToClass(ClassLike $class, Node\Stmt\ClassMethod $node): void
1✔
363
        {
364
                $method = $class->addMethod($node->name->toString());
1✔
365
                $method->setAbstract($node->isAbstract());
1✔
366
                $method->setFinal($node->isFinal());
1✔
367
                $method->setStatic($node->isStatic());
1✔
368
                $method->setVisibility($this->toVisibility($node->flags));
1✔
369
                $this->setupFunction($method, $node);
1✔
370
                if ($method->getName() === Method::Constructor && $class instanceof ClassType && $class->isReadOnly()) {
1✔
371
                        array_map(fn($param) => $param instanceof PromotedParameter ? $param->setReadOnly() : $param, $method->getParameters());
1✔
372
                }
373
        }
1✔
374

375

376
        private function addConstantToClass(ClassLike $class, Node\Stmt\ClassConst $node): void
1✔
377
        {
378
                foreach ($node->consts as $item) {
1✔
379
                        $const = $class->addConstant($item->name->toString(), $this->toValue($item->value));
1✔
380
                        $const->setVisibility($this->toVisibility($node->flags));
1✔
381
                        $const->setFinal(method_exists($node, 'isFinal') && $node->isFinal());
1✔
382
                        $this->addCommentAndAttributes($const, $node);
1✔
383
                }
384
        }
1✔
385

386

387
        private function addEnumCaseToClass(EnumType $class, Node\Stmt\EnumCase $node): void
1✔
388
        {
389
                $value = match (true) {
1✔
390
                        $node->expr === null => null,
1✔
391
                        $node->expr instanceof Node\Scalar\LNumber, $node->expr instanceof Node\Scalar\String_ => $node->expr->value,
1✔
392
                        default => $this->toValue($node->expr),
1✔
393
                };
394
                $case = $class->addCase($node->name->toString(), $value);
1✔
395
                $this->addCommentAndAttributes($case, $node);
1✔
396
        }
1✔
397

398

399
        private function addFunctionToFile(PhpFile $phpFile, Node\Stmt\Function_ $node): void
1✔
400
        {
401
                $function = $phpFile->addFunction($node->namespacedName->toString());
1✔
402
                $this->setupFunction($function, $node);
1✔
403
        }
1✔
404

405

406
        private function addCommentAndAttributes(
1✔
407
                PhpFile|ClassLike|Constant|Property|GlobalFunction|Method|Parameter|EnumCase|TraitUse|PropertyHook $element,
408
                Node $node,
409
        ): void
410
        {
411
                if ($node->getDocComment()) {
1✔
412
                        $comment = $node->getDocComment()->getReformattedText();
1✔
413
                        $comment = Helpers::unformatDocComment($comment);
1✔
414
                        $element->setComment($comment);
1✔
415
                        $node->setDocComment(new PhpParser\Comment\Doc(''));
1✔
416
                }
417

418
                foreach ($node->attrGroups ?? [] as $group) {
1✔
419
                        foreach ($group->attrs as $attribute) {
1✔
420
                                $args = [];
1✔
421
                                foreach ($attribute->args as $arg) {
1✔
422
                                        if ($arg->name) {
1✔
423
                                                $args[$arg->name->toString()] = $this->toValue($arg->value);
1✔
424
                                        } else {
425
                                                $args[] = $this->toValue($arg->value);
1✔
426
                                        }
427
                                }
428

429
                                $element->addAttribute($attribute->name->toString(), $args);
1✔
430
                        }
431
                }
432
        }
1✔
433

434

435
        private function setupFunction(GlobalFunction|Method|PropertyHook $function, Node\FunctionLike $node): void
1✔
436
        {
437
                $function->setReturnReference($node->returnsByRef());
1✔
438
                if (!$function instanceof PropertyHook) {
1✔
439
                        $function->setReturnType($node->getReturnType() ? $this->toPhp($node->getReturnType()) : null);
1✔
440
                }
441

442
                foreach ($node->getParams() as $item) {
1✔
443
                        $getVisibility = $this->toVisibility($item->flags);
1✔
444
                        $setVisibility = $this->toSetterVisibility($item->flags);
1✔
445
                        if ($getVisibility || $setVisibility) {
1✔
446
                                $param = $function->addPromotedParameter($item->var->name)
1✔
447
                                        ->setVisibility($getVisibility, $setVisibility)
1✔
448
                                        ->setReadonly((bool) ($item->flags & Node\Stmt\Class_::MODIFIER_READONLY));
1✔
449
                                $this->addHooksToProperty($param, $item);
1✔
450
                        } else {
451
                                $param = $function->addParameter($item->var->name);
1✔
452
                        }
453
                        $param->setType($item->type ? $this->toPhp($item->type) : null);
1✔
454
                        $param->setReference($item->byRef);
1✔
455
                        if (!$function instanceof PropertyHook) {
1✔
456
                                $function->setVariadic($item->variadic);
1✔
457
                        }
458
                        if ($item->default) {
1✔
459
                                $param->setDefaultValue($this->toValue($item->default));
1✔
460
                        }
461

462
                        $this->addCommentAndAttributes($param, $item);
1✔
463
                }
464

465
                $this->addCommentAndAttributes($function, $node);
1✔
466
                if ($node->getStmts()) {
1✔
467
                        $indent = $function instanceof GlobalFunction ? 1 : 2;
1✔
468
                        $function->setBody($this->getReformattedContents($node->getStmts(), $indent));
1✔
469
                }
470
        }
1✔
471

472

473
        private function toValue(Node\Expr $node): mixed
1✔
474
        {
475
                if ($node instanceof Node\Expr\ConstFetch) {
1✔
476
                        return match ($node->name->toLowerString()) {
1✔
477
                                'null' => null,
1✔
478
                                'true' => true,
1✔
479
                                'false' => false,
1✔
480
                                default => new Literal($this->getReformattedContents([$node], 0)),
1✔
481
                        };
482
                } elseif ($node instanceof Node\Scalar\LNumber
1✔
483
                        || $node instanceof Node\Scalar\DNumber
1✔
484
                        || $node instanceof Node\Scalar\String_
1✔
485
                ) {
486
                        return $node->value;
1✔
487

488
                } elseif ($node instanceof Node\Expr\Array_) {
1✔
489
                        $res = [];
1✔
490
                        foreach ($node->items as $item) {
1✔
491
                                if ($item->unpack) {
1✔
492
                                        return new Literal($this->getReformattedContents([$node], 0));
1✔
493

494
                                } elseif ($item->key) {
1✔
495
                                        $key = $item->key instanceof Node\Identifier
1✔
496
                                                ? $item->key->name
×
497
                                                : $this->toValue($item->key);
1✔
498

499
                                        if ($key instanceof Literal) {
1✔
500
                                                return new Literal($this->getReformattedContents([$node], 0));
1✔
501
                                        }
502

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

505
                                } else {
506
                                        $res[] = $this->toValue($item->value);
1✔
507
                                }
508
                        }
509
                        return $res;
1✔
510

511
                } else {
512
                        return new Literal($this->getReformattedContents([$node], 0));
1✔
513
                }
514
        }
515

516

517
        private function toVisibility(int $flags): ?string
1✔
518
        {
519
                return match (true) {
520
                        (bool) ($flags & Node\Stmt\Class_::MODIFIER_PUBLIC) => Visibility::Public,
1✔
521
                        (bool) ($flags & Node\Stmt\Class_::MODIFIER_PROTECTED) => Visibility::Protected,
1✔
522
                        (bool) ($flags & Node\Stmt\Class_::MODIFIER_PRIVATE) => Visibility::Private,
1✔
523
                        default => null,
1✔
524
                };
525
        }
526

527

528
        private function toSetterVisibility(int $flags): ?string
1✔
529
        {
530
                return match (true) {
531
                        !class_exists(Node\PropertyHook::class) => null,
1✔
532
                        (bool) ($flags & Modifiers::PUBLIC_SET) => Visibility::Public,
1✔
533
                        (bool) ($flags & Modifiers::PROTECTED_SET) => Visibility::Protected,
1✔
534
                        (bool) ($flags & Modifiers::PRIVATE_SET) => Visibility::Private,
1✔
535
                        default => null,
1✔
536
                };
537
        }
538

539

540
        private function toPhp(Node $value): string
1✔
541
        {
542
                $dolly = clone $value;
1✔
543
                $dolly->setAttribute('comments', []);
1✔
544
                return $this->printer->prettyPrint([$dolly]);
1✔
545
        }
546

547

548
        private function getNodeContents(Node ...$nodes): string
1✔
549
        {
550
                $start = $this->getNodeStartPos($nodes[0]);
1✔
551
                return substr($this->code, $start, end($nodes)->getEndFilePos() - $start + 1);
1✔
552
        }
553

554

555
        private function getNodeStartPos(Node $node): int
1✔
556
        {
557
                return ($comments = $node->getComments())
1✔
558
                        ? $comments[0]->getStartFilePos()
1✔
559
                        : $node->getStartFilePos();
1✔
560
        }
561
}
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