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

nette / utils / 22290136219

23 Feb 2026 01:47AM UTC coverage: 93.125% (-0.003%) from 93.128%
22290136219

push

github

dg
added CLAUDE.md

2086 of 2240 relevant lines covered (93.13%)

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 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\Iterators;
9

10
use Nette;
11

12

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

32
        private int $counter = 0;
33

34

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

44

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

53

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

62

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

68

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

74

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

80

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

86

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

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

98

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

110

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

120

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

127

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