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

nette / neon / 39547693629

21 Oct 2026 05:06AM UTC coverage: 95.086% (-1.2%) from 96.3%
39547693629

push

github

dg
updated readme.md

2051 of 2157 relevant lines covered (95.09%)

0.95 hits per line

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

92.31
/src/Neon/Nodes/ArrayItemNode.php
1
<?php declare(strict_types=1);
1 ✔
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
namespace Nette\Neon\Nodes;
9

10
use Nette\Neon\{Builder, CommentPolicy, Editor, Exception, Journal, Node, Scalars, Token, Trivia};
11

12

13
/**
14
 * An item of an array: `- value` and `key: value` in a block, `value` and `key: value` inline, where
15
 * the separator is the comma after the item.
16
 */
17
final class ArrayItemNode extends Node
18
{
19
        public const Slots = ['bullet', 'key', 'colon', 'value', 'separator'];
20

21
        /** Whether removing the last item of the value leaves null instead of an empty collection. */
22
        public bool $nullable = false;
23

24

25
        /** @internal */
26
        public function __construct(
1 ✔
27
                public protected(set) ?Token $bullet,
28
                public protected(set) StringNode|LiteralNode|null $key,
29
                public protected(set) ?Token $colon,
30
                public protected(set) ?ValueNode $value,
31
                public protected(set) ?Token $separator = null,
32
        ) {
33
                $this->adopt($bullet, $key, $colon, $value, $separator);
1 ✔
34
        }
1 ✔
35

36

37
        public function getChildren(): array
38
        {
39
                $res = [];
1 ✔
40
                foreach ([$this->bullet, $this->key, $this->colon, $this->value, $this->separator] as $child) {
1 ✔
41
                        if ($child) {
1 ✔
42
                                $res[] = $child;
1 ✔
43
                        }
44
                }
45
                return $res;
1 ✔
46
        }
47

48

49
        public function getFirstToken(): Token
50
        {
51
                return $this->bullet ?? $this->key?->getFirstToken() ?? $this->value?->getFirstToken() ?? throw new \LogicException;
1 ✔
52
        }
53

54

55
        public function getLastToken(): Token
56
        {
57
                return $this->separator ?? $this->value?->getLastToken() ?? $this->colon ?? $this->bullet ?? throw new \LogicException;
1 ✔
58
        }
59

60

61
        /** The canonical key, or null for an item without a key. */
62
        public function getKeyValue(): ?string
63
        {
64
                return match (true) {
65
                        $this->key instanceof StringNode => Scalars::evaluateString($this->key->token->text),
1 ✔
66
                        $this->key instanceof LiteralNode => Scalars::literalKey($this->key->token->text),
1 ✔
67
                        default => null,
1 ✔
68
                };
69
        }
70

71

72
        /** The value of the item, null without one. */
73
        public function getValue(): mixed
74
        {
75
                return $this->value?->toValue();
1 ✔
76
        }
77

78

79
        /**
80
         * Writes the value: a value of PHP or a node. Writing an equal value changes nothing; a new scalar keeps the
81
         * style of the old one where it can.
82
         * @throws Exception  for a value that cannot stand in the item
83
         */
84
        public function setValue(mixed $value): void
85
        {
86
                $value = Builder::materialize($value); // iterators run before the journal records
1 ✔
87
                Journal::atomic(fn() => Editor::setItemValue($this, $value));
1 ✔
88
        }
1 ✔
89

90

91
        /**
92
         * Renames the key.
93
         * @throws Exception  for an item without a key or a key that is there already
94
         */
95
        public function setKey(string $key): void
96
        {
97
                Journal::atomic(fn() => Editor::setItemKey($this, $key));
×
98
        }
×
99

100

101
        /**
102
         * Removes the item from its array, with the comments it owns.
103
         * @throws Exception  for an item that is not in an array
104
         */
105
        public function remove(CommentPolicy $comments = CommentPolicy::Drop, bool $mergeBlankLines = false): void
106
        {
107
                Journal::atomic(fn() => Editor::removeItem($this, $comments, null, $mergeBlankLines));
1 ✔
108
        }
1 ✔
109

110

111
        /** The indentation of the line the item starts on. */
112
        public function getIndentation(): string
113
        {
114
                $leading = $this->getFirstToken()->leadingTrivia;
1 ✔
115
                $last = end($leading);
1 ✔
116
                return $last && $last->kind === Trivia::Whitespace ? $last->text : '';
1 ✔
117
        }
118
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc