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

nette / utils / 21636962765

03 Feb 2026 03:39PM UTC coverage: 93.312% (+0.05%) from 93.264%
21636962765

push

github

dg
added CLAUDE.md

2065 of 2213 relevant lines covered (93.31%)

0.93 hits per line

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

70.37
/src/Iterators/CachingIterator.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\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 mixed $nextKey
28
 * @property-read mixed $nextValue
29
 */
30
class CachingIterator extends \CachingIterator implements \Countable
31
{
32
        use Nette\SmartObject;
33

34
        private int $counter = 0;
35

36

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

48

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

57

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

66

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

72

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

78

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

84

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

90

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

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

102

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

114

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

124

125
        public function getNextKey(): mixed
126
        {
127
                return $this->getInnerIterator()->key();
×
128
        }
129

130

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