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

nextras / orm / 41909495460

04 Nov 2026 08:44PM UTC coverage: 92.015% (-0.002%) from 92.017%
41909495460

push

github

web-flow
Merge pull request #842 from nextras/js/dresscode

Setup DressCode

288 of 347 new or added lines in 66 files covered. (83.0%)

14 existing lines in 9 files now uncovered.

4402 of 4784 relevant lines covered (92.02%)

6.29 hits per line

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

62.82
/src/Collection/HasManyCollection.php
1
<?php declare(strict_types = 1);
2

3
namespace Nextras\Orm\Collection;
4

5

6
use Iterator;
7
use Nextras\Orm\Collection\Helpers\FetchPairsHelper;
8
use Nextras\Orm\Entity\IEntity;
9
use Nextras\Orm\Exception\MemberAccessException;
10
use Nextras\Orm\Exception\NoResultException;
11
use Nextras\Orm\Mapper\IRelationshipMapper;
12
use Nextras\Orm\Repository\IRepository;
13
use function array_map;
14
use function iterator_count;
15
use function iterator_to_array;
16
use function spl_object_id;
17

18

19
/**
20
 * @template E of IEntity
21
 * @implements ICollection<E>
22
 */
23
class HasManyCollection implements ICollection
24
{
25
        /** @var array<callable(\Traversable<E>):void> */
26
        public $onEntityFetch = [];
27

28
        /** @var ICollection<E> */
29
        private ICollection $storageCollection;
30

31
        /** @var MutableArrayCollection<E> */
32
        private MutableArrayCollection $inMemoryCollection;
33

34
        /** @var callable(): array{array<array-key, E>, array<array-key, E>} */
35
        private $diffCallback;
36

37
        /** @var Iterator<mixed, mixed>|null */
38
        private Iterator|null $fetchIterator = null;
39

40

41
        /**
42
         * @param ICollection<E> $innerCollection
43
         * @param callable():array{array<array-key, E>, array<array-key, E>} $diffCallback
44
         */
45
        public function __construct(
5 ✔
46
                /** @var IRepository<E> */
47
                private readonly IRepository $repository,
2 ✔
48
                ICollection $innerCollection,
49
                callable $diffCallback,
50
        )
51
        {
52
                $this->storageCollection = $innerCollection;
7 ✔
53
                $this->diffCallback = $diffCallback;
7 ✔
54
                $this->inMemoryCollection = new MutableArrayCollection([], $repository); // @phpstan-ignore-line
7 ✔
55
        }
7 ✔
56

57

58
        #[\NoDiscard]
59
        public function getBy(array $conds): IEntity|null
60
        {
61
                return $this->findBy($conds)->fetch();
7 ✔
62
        }
63

64

65
        #[\NoDiscard]
66
        public function getByChecked(array $conds): IEntity
67
        {
68
                return $this->findBy($conds)->fetchChecked();
×
69
        }
70

71

72
        #[\NoDiscard]
73
        public function getById($id): IEntity|null
74
        {
75
                return $this->getBy(['id' => $id]);
×
76
        }
77

78

79
        #[\NoDiscard]
80
        public function getByIdChecked($id): IEntity
81
        {
82
                $entity = $this->getById($id);
×
83
                if ($entity === null) {
×
84
                        throw new NoResultException();
×
85
                }
86
                return $entity;
×
87
        }
88

89

90
        #[\NoDiscard('Method returns a new collection instance, the original collection is not modified.')]
91
        public function findBy(array $conds): ICollection
92
        {
93
                $collection = clone $this;
7 ✔
94
                $collection->storageCollection = $this->storageCollection->findBy($conds);
7 ✔
95
                $collection->inMemoryCollection = $this->inMemoryCollection->findBy($conds);
7 ✔
96
                return $collection;
7 ✔
97
        }
98

99

100
        #[\NoDiscard('Method returns a new collection instance, the original collection is not modified.')]
101
        public function orderBy($expression, string $direction = self::ASC): ICollection
102
        {
103
                $collection = clone $this;
7 ✔
104
                $collection->storageCollection = $this->storageCollection->orderBy($expression, $direction);
7 ✔
105
                $collection->inMemoryCollection = $this->inMemoryCollection->orderBy($expression, $direction);
7 ✔
106
                return $collection;
7 ✔
107
        }
108

109

110
        #[\NoDiscard('Method returns a new collection instance, the original collection is not modified.')]
111
        public function resetOrderBy(): ICollection
112
        {
113
                $collection = clone $this;
×
114
                $collection->storageCollection = $this->storageCollection->resetOrderBy();
×
115
                $collection->inMemoryCollection = $this->inMemoryCollection->resetOrderBy();
×
116
                return $collection;
×
117
        }
118

119

120
        #[\NoDiscard('Method returns a new collection instance, the original collection is not modified.')]
121
        public function limitBy(int $limit, int|null $offset = null): ICollection
122
        {
123
                $collection = clone $this;
7 ✔
124
                $collection->storageCollection = $this->storageCollection->limitBy($limit, $offset);
7 ✔
125
                $collection->inMemoryCollection = $this->inMemoryCollection->limitBy($limit, $offset);
7 ✔
126
                return $collection;
7 ✔
127
        }
128

129

130
        #[\NoDiscard]
131
        public function fetch(): IEntity|null
132
        {
133
                if ($this->fetchIterator === null) {
7 ✔
134
                        $this->fetchIterator = $this->getIterator();
7 ✔
135
                }
136

137
                if ($this->fetchIterator->valid()) {
7 ✔
138
                        $current = $this->fetchIterator->current();
7 ✔
139
                        $this->fetchIterator->next();
7 ✔
140
                        return $current;
7 ✔
141
                }
142

143
                return null;
7 ✔
144
        }
145

146

147
        #[\NoDiscard]
148
        public function fetchChecked(): IEntity
149
        {
150
                $entity = $this->fetch();
×
151
                if ($entity === null) {
×
152
                        throw new NoResultException();
×
153
                }
154
                return $entity;
×
155
        }
156

157

158
        #[\NoDiscard]
159
        public function fetchAll(): array
160
        {
161
                return iterator_to_array($this->getIterator(), preserve_keys: false);
7 ✔
162
        }
163

164

165
        #[\NoDiscard]
166
        public function fetchPairs(string|null $key = null, string|null $value = null): array
167
        {
168
                return FetchPairsHelper::process($this->getIterator(), $key, $value);
×
169
        }
170

171

172
        #[\NoDiscard]
173
        public function getIterator(): Iterator
174
        {
175
                [$toAdd, $toRemove] = ($this->diffCallback)();
7 ✔
176

177
                $storageCollection = $this->storageCollection;
7 ✔
178
                // condition here is to maintain relationship cache
179
                if ($toRemove !== []) {
7 ✔
180
                        $toRemoveIds = array_map(fn(IEntity $entity) => $entity->getValue('id'), $toRemove);
7 ✔
181
                        $storageCollection = $storageCollection->findBy(['id!=' => $toRemoveIds]);
7 ✔
182
                }
183

184
                $all = [];
7 ✔
185
                foreach ($storageCollection as $entity) {
7 ✔
186
                        $all[spl_object_id($entity)] = $entity;
7 ✔
187
                }
188
                foreach ($toAdd as $hash => $entity) {
7 ✔
189
                        $all[$hash] = $entity;
7 ✔
190
                }
191

192
                $collection = $this->inMemoryCollection->withData(array_values($all));
7 ✔
193
                foreach ($this->onEntityFetch as $onEntityFetch) {
7 ✔
194
                        $collection->subscribeOnEntityFetch($onEntityFetch);
×
195
                }
196
                return $collection->getIterator();
7 ✔
197
        }
198

199

200
        #[\NoDiscard]
201
        public function count(): int
202
        {
203
                return iterator_count($this->getIterator());
7 ✔
204
        }
205

206

207
        #[\NoDiscard]
208
        public function countStored(): int
209
        {
210
                [$toAdd, $toRemove] = ($this->diffCallback)();
7 ✔
211
                $count = $this->storageCollection->countStored();
7 ✔
212
                $count -= $this->inMemoryCollection->withData(array_values($toRemove))->countStored();
7 ✔
213
                $count += $this->inMemoryCollection->withData(array_values($toAdd))->countStored();
7 ✔
214
                return $count;
7 ✔
215
        }
216

217

218
        #[\NoDiscard]
219
        public function toMemoryCollection(): MemoryCollection
220
        {
221
                $collection = clone $this;
7 ✔
222
                $entities = $collection->fetchAll();
7 ✔
223
                return new ArrayCollection($entities, $this->repository);
7 ✔
224
        }
225

226

227
        public function setRelationshipMapper(IRelationshipMapper|null $mapper): ICollection
228
        {
229
                $this->storageCollection->setRelationshipMapper($mapper);
×
230
                $this->inMemoryCollection->setRelationshipMapper($mapper);
×
231
                return $this;
×
232
        }
233

234

235
        #[\NoDiscard]
236
        public function getRelationshipMapper(): IRelationshipMapper|null
237
        {
238
                return $this->storageCollection->getRelationshipMapper();
×
239
        }
240

241

242
        #[\NoDiscard('Method returns a new collection instance, the original collection is not modified.')]
243
        public function setRelationshipParent(IEntity $parent): ICollection
244
        {
245
                $collection = clone $this;
×
246
                $collection->storageCollection = $this->storageCollection->setRelationshipParent($parent);
×
247
                $collection->inMemoryCollection = $this->inMemoryCollection->setRelationshipParent($parent);
×
248
                return $collection;
×
249
        }
250

251

252
        public function subscribeOnEntityFetch(callable $callback): void
253
        {
254
                $this->storageCollection->subscribeOnEntityFetch($callback);
×
255
                $this->onEntityFetch[] = $callback;
×
256
        }
×
257

258

259
        /**
260
         * @param mixed[] $args
261
         * @return never
262
         * @throws MemberAccessException
263
         */
264
        public function __call(string $name, array $args)
265
        {
NEW
266
                $class = static::class;
×
267
                throw new MemberAccessException("Call to undefined method $class::$name().");
×
268
        }
269
}
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