• 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

86.67
/src/Neon/Nodes/ArrayNode.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, Evaluator, Exception, Journal, Scalars};
11
use function is_array;
12

13

14
/**
15
 * A block or an inline array; its items change only through its methods.
16
 */
17
abstract class ArrayNode extends ValueNode
18
{
19
        /** @var list<ArrayItemNode> */
20
        public protected(set) array $items = [];
21

22

23
        /** @return mixed[] */
24
        public function toValue(): array
25
        {
26
                $value = new Evaluator()->evaluate($this);
1 ✔
27
                return is_array($value) ? $value : throw new \LogicException('An array evaluates to an array.');
1 ✔
28
        }
29

30

31
        /**
32
         * The item with the canonical key, as the value of the document would find it.
33
         */
34
        public function findItem(string|int $key): ?ArrayItemNode
35
        {
36
                $keys = $this->getItemKeys();
1 ✔
37
                $found = null;
1 ✔
38
                foreach ($this->items as $i => $item) {
1 ✔
39
                        if ($keys[$i] === (string) $key) {
1 ✔
40
                                $found = $item; // a later explicit key overwrites the earlier one
1 ✔
41
                        }
42
                }
43
                return $found;
1 ✔
44
        }
45

46

47
        /**
48
         * The key of each item, explicit or implicit: an item without a key gets the greatest integer key so far plus one.
49
         * @return list<string>
50
         * @throws Exception  for an implicit key beyond the 64-bit range
51
         */
52
        public function getItemKeys(): array
53
        {
54
                $max = null;
1 ✔
55
                $res = [];
1 ✔
56
                foreach ($this->items as $item) {
1 ✔
57
                        $key = $item->getKeyValue()
1 ✔
58
                                ?? Scalars::nextImplicitKey($max)
1 ✔
59
                                ?? throw new Exception('Implicit key overflow', $item->getPosition());
1 ✔
60
                        $max = Scalars::maxIntegerKey($max, $key);
1 ✔
61
                        $res[] = $key;
1 ✔
62
                }
63
                return $res;
1 ✔
64
        }
65

66

67
        /**
68
         * Adds an item built from the value, a value of PHP or a node, at the end or before the item at the position.
69
         * @throws Exception  for a key that is there already, or a value that cannot stand in the array
70
         */
71
        public function addItem(mixed $value, ?string $key = null, ?int $position = null): ArrayItemNode
72
        {
73
                $value = Builder::materialize($value); // iterators run before the journal records
1 ✔
74
                return Journal::atomic(fn() => Editor::addItem($this, $key, $value, $position));
1 ✔
75
        }
76

77

78
        /** Removes the item with the comments it owns. */
79
        public function removeItem(ArrayItemNode $item, CommentPolicy $comments = CommentPolicy::Drop): void
80
        {
81
                if ($item->parent !== $this) {
×
82
                        throw new \InvalidArgumentException('The item is not in this array.');
×
83
                }
84
                Journal::atomic(fn() => Editor::removeItem($item, $comments));
×
85
        }
×
86

87

88
        /**
89
         * Inserts an item taken out of a tree by remove(), reindented to its new place, with its comments.
90
         * @throws Exception  for an item that cannot stand in the array
91
         */
92
        public function insert(int $position, ArrayItemNode $item): void
93
        {
94
                Journal::atomic(fn() => Editor::insertItem($this, $item, $position));
1 ✔
95
        }
1 ✔
96

97

98
        /** @param  list<ArrayItemNode>  $items */
99
        protected function setItems(array $items): void
100
        {
101
                $this->adopt(...$items);
1 ✔
102
                $this->items = $items;
1 ✔
103
        }
1 ✔
104
}
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