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

nette / utils / 21935494777

12 Feb 2026 06:02AM UTC coverage: 93.429%. Remained the same
21935494777

push

github

dg
added CLAUDE.md

2076 of 2222 relevant lines covered (93.43%)

0.93 hits per line

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

71.43
/src/Iterators/CachingIterator.php
1
<?php
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
declare(strict_types=1);
9

10
namespace Nette\Iterators;
11

12
use Nette;
13

14

15
/**
16
 * Enhanced caching iterator with first/last/counter tracking.
17
 *
18
 * @template TKey
19
 * @template TValue
20
 * @extends \CachingIterator<TKey, TValue, \Iterator<TKey, TValue>>
21
 * @property-read bool $first
22
 * @property-read bool $last
23
 * @property-read bool $empty
24
 * @property-read bool $odd
25
 * @property-read bool $even
26
 * @property-read int $counter
27
 * @property-read TKey $nextKey
28
 * @property-read TValue $nextValue
29
 */
30
class CachingIterator extends \CachingIterator implements \Countable
31
{
32
        use Nette\SmartObject;
33

34
        private int $counter = 0;
35

36

37
        /** @param  iterable<TKey, TValue>|\stdClass  $iterable */
38
        public function __construct(iterable|\stdClass $iterable)
1✔
39
        {
40
                $iterable = $iterable instanceof \stdClass
1✔
41
                        ? new \ArrayIterator((array) $iterable)
1✔
42
                        : Nette\Utils\Iterables::toIterator($iterable);
1✔
43
                parent::__construct($iterable, 0);
1✔
44
        }
1✔
45

46

47
        /**
48
         * Is the current element the first one?
49
         */
50
        public function isFirst(?int $gridWidth = null): bool
1✔
51
        {
52
                return $this->counter === 1 || ($gridWidth && $this->counter !== 0 && (($this->counter - 1) % $gridWidth) === 0);
1✔
53
        }
54

55

56
        /**
57
         * Is the current element the last one?
58
         */
59
        public function isLast(?int $gridWidth = null): bool
1✔
60
        {
61
                return !$this->hasNext() || ($gridWidth && ($this->counter % $gridWidth) === 0);
1✔
62
        }
63

64

65
        public function isEmpty(): bool
66
        {
67
                return $this->counter === 0;
1✔
68
        }
69

70

71
        public function isOdd(): bool
72
        {
73
                return $this->counter % 2 === 1;
×
74
        }
75

76

77
        public function isEven(): bool
78
        {
79
                return $this->counter % 2 === 0;
×
80
        }
81

82

83
        public function getCounter(): int
84
        {
85
                return $this->counter;
1✔
86
        }
87

88

89
        public function count(): int
90
        {
91
                $inner = $this->getInnerIterator();
×
92
                if ($inner instanceof \Countable) {
×
93
                        return $inner->count();
×
94

95
                } else {
96
                        throw new Nette\NotSupportedException('Iterator is not countable.');
×
97
                }
98
        }
99

100

101
        /**
102
         * Forwards to the next element.
103
         */
104
        public function next(): void
105
        {
106
                parent::next();
1✔
107
                if (parent::valid()) {
1✔
108
                        $this->counter++;
1✔
109
                }
110
        }
1✔
111

112

113
        /**
114
         * Rewinds the Iterator.
115
         */
116
        public function rewind(): void
117
        {
118
                parent::rewind();
1✔
119
                $this->counter = parent::valid() ? 1 : 0;
1✔
120
        }
1✔
121

122

123
        /** @return TKey */
124
        public function getNextKey(): mixed
125
        {
126
                return $this->getInnerIterator()->key();
×
127
        }
128

129

130
        /** @return TValue */
131
        public function getNextValue(): mixed
132
        {
133
                return $this->getInnerIterator()->current();
×
134
        }
135
}
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