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

chdemko / php-sorted-collections / 42793925983

10 Nov 2026 09:49AM UTC coverage: 97.744% (-0.2%) from 97.912%
42793925983

push

github

chdemko
🐛 Fix SubMap boundary handling

Ensure bounded views return only keys within their configured range.

31 of 32 new or added lines in 1 file covered. (96.88%)

780 of 798 relevant lines covered (97.74%)

120.96 hits per line

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

99.58
/src/SortedCollection/SubMap.php
1
<?php
2

3
/**
4
 * chdemko\SortedCollection\SubMap class
5
 *
6
 * @author    Christophe Demko <chdemko@gmail.com>
7
 * @copyright Copyright (C) 2012-2024 Christophe Demko. All rights reserved.
8
 *
9
 * @license BSD 3-Clause License
10
 *
11
 * This file is part of the php-sorted-collections package https://github.com/chdemko/php-sorted-collections
12
 */
13

14
// Declare chdemko\SortedCollection namespace
15
namespace chdemko\SortedCollection;
16

17
/**
18
 * Sub map
19
 *
20
 * @package    SortedCollection
21
 * @subpackage Map
22
 *
23
 * @since 1.0.0
24
 *
25
 * @property-read callable   $comparator     The key comparison function
26
 * @property-read TreeNode   $first          The first element of the map
27
 * @property-read mixed      $firstKey       The first key of the map
28
 * @property-read mixed      $firstValue     The first value of the map
29
 * @property-read TreeNode   $last           The last element of the map
30
 * @property-read mixed      $lastKey        The last key of the map
31
 * @property-read mixed      $lastValue      The last value of the map
32
 * @property-read Iterator   $keys           The keys iterator
33
 * @property-read Iterator   $values         The values iterator
34
 * @property-read integer    $count          The number of elements in the map
35
 * @property      mixed      $fromKey        The from key
36
 * @property      boolean    $fromInclusive  The from inclusive flag
37
 * @property      mixed      $toKey          The to key
38
 * @property      boolean    $toInclusive    The to inclusive flag
39
 * @property-read SortedMap  $map            The underlying map
40
 */
41
class SubMap extends AbstractMap
42
{
43
    /**
44
     * When the from or to key is unused
45
     *
46
     * @since 1.0.0
47
     */
48
    private const UNUSED = 0;
49

50
    /**
51
     * When the from or to key is inclusive
52
     *
53
     * @since 1.0.0
54
     */
55
    private const INCLUSIVE = 1;
56

57
    /**
58
     * When the from or to key is exclusive
59
     *
60
     * @since 1.0.0
61
     */
62
    private const EXCLUSIVE = 2;
63

64
    /**
65
     * @var SortedMap  Internal map
66
     *
67
     * @since 1.0.0
68
     */
69
    private $mapInternal;
70

71
    /**
72
     * @var integer  from option
73
     *
74
     * @since 1.0.0
75
     */
76
    private $fromOption;
77

78
    /**
79
     * @var mixed  from key
80
     *
81
     * @since 1.0.0
82
     */
83
    private $fromKey;
84

85
    /**
86
     * @var integer  to option
87
     *
88
     * @since 1.0.0
89
     */
90
    private $toOption;
91

92
    /**
93
     * @var mixed  to key
94
     *
95
     * @since 1.0.0
96
     */
97
    private $toKey;
98

99
    /**
100
     * @var boolean  Empty flag
101
     *
102
     * @since 1.0.0
103
     */
104
    private $empty;
105

106
    /**
107
     * Magic get method
108
     *
109
     * @param string $property The property
110
     *
111
     * @throws RuntimeException If the property does not exist
112
     *
113
     * @return mixed The value associated to the property
114
     *
115
     * @since 1.0.0
116
     */
117
    public function __get($property)
118
    {
119
        switch ($property) {
120
            case 'fromKey':
213 ✔
121
                if ($this->fromOption == self::UNUSED) {
14 ✔
122
                    throw new \RuntimeException('Undefined property');
3 ✔
123
                } else {
124
                    return $this->fromKey;
11 ✔
125
                }
126

127
            case 'toKey':
210 ✔
128
                if ($this->toOption == self::UNUSED) {
14 ✔
129
                    throw new \RuntimeException('Undefined property');
3 ✔
130
                } else {
131
                    return $this->toKey;
11 ✔
132
                }
133

134
            case 'fromInclusive':
207 ✔
135
                if ($this->fromOption == self::UNUSED) {
12 ✔
136
                    throw new \RuntimeException('Undefined property');
1 ✔
137
                } else {
138
                    return $this->fromOption == self::INCLUSIVE;
11 ✔
139
                }
140

141
            case 'toInclusive':
204 ✔
142
                if ($this->toOption == self::UNUSED) {
12 ✔
143
                    throw new \RuntimeException('Undefined property');
1 ✔
144
                } else {
145
                    return $this->toOption == self::INCLUSIVE;
11 ✔
146
                }
147

148
            case 'map':
194 ✔
149
                return $this->mapInternal;
184 ✔
150
            default:
151
                return parent::__get($property);
14 ✔
152
        }
153
    }
154

155
    /**
156
     * Magic set method
157
     *
158
     * @param string $property The property
159
     * @param mixed  $value    The new value
160
     *
161
     * @throws RuntimeException If the property does not exist
162
     *
163
     * @return void
164
     *
165
     * @since 1.0.0
166
     */
167
    public function __set($property, $value)
168
    {
169
        switch ($property) {
170
            case 'fromKey':
7 ✔
171
                $this->fromKey = $value;
2 ✔
172

173
                if ($this->fromOption == self::UNUSED) {
2 ✔
174
                     $this->fromOption = self::INCLUSIVE;
1 ✔
175
                }
176
                break;
2 ✔
177
            case 'toKey':
6 ✔
178
                $this->toKey = $value;
2 ✔
179

180
                if ($this->toOption == self::UNUSED) {
2 ✔
181
                    $this->toOption = self::EXCLUSIVE;
1 ✔
182
                }
183
                break;
2 ✔
184
            case 'fromInclusive':
5 ✔
185
                if ($this->fromOption == self::UNUSED) {
3 ✔
186
                    throw new \RuntimeException('Undefined property');
1 ✔
187
                } else {
188
                    $this->fromOption = $value ? self::INCLUSIVE : self::EXCLUSIVE;
2 ✔
189
                }
190
                break;
2 ✔
191
            case 'toInclusive':
4 ✔
192
                if ($this->toOption == self::UNUSED) {
3 ✔
193
                    throw new \RuntimeException('Undefined property');
1 ✔
194
                } else {
195
                    $this->toOption = $value ? self::INCLUSIVE : self::EXCLUSIVE;
2 ✔
196
                }
197
                break;
2 ✔
198
            default:
199
                throw new \RuntimeException('Undefined property');
1 ✔
200
        }
201

202
        $this->setEmpty();
4 ✔
203
    }
204

205
    /**
206
     * Magic unset method
207
     *
208
     * @param string $property The property
209
     *
210
     * @throws RuntimeException If the property does not exist
211
     *
212
     * @return void
213
     *
214
     * @since 1.0.0
215
     */
216
    public function __unset($property)
217
    {
218
        switch ($property) {
219
            case 'fromKey':
10 ✔
220
            case 'fromInclusive':
7 ✔
221
                $this->fromOption = self::UNUSED;
5 ✔
222
                break;
5 ✔
223
            case 'toKey':
6 ✔
224
            case 'toInclusive':
3 ✔
225
                $this->toOption = self::UNUSED;
5 ✔
226
                break;
5 ✔
227
            default:
228
                throw new \RuntimeException('Undefined property');
1 ✔
229
        }
230

231
        $this->setEmpty();
9 ✔
232
    }
233

234
    /**
235
     * Magic isset method
236
     *
237
     * @param string $property The property
238
     *
239
     * @return boolean
240
     *
241
     * @since 1.0.0
242
     */
243
    public function __isset($property)
244
    {
245
        switch ($property) {
246
            case 'fromKey':
13 ✔
247
            case 'fromInclusive':
13 ✔
248
                return $this->fromOption != self::UNUSED;
11 ✔
249
            case 'toKey':
12 ✔
250
            case 'toInclusive':
3 ✔
251
                return $this->toOption != self::UNUSED;
11 ✔
252
            default:
253
                return false;
1 ✔
254
        }
255
    }
256

257
    /**
258
     * Constructor
259
     *
260
     * @param SortedMap $map        Internal map
261
     * @param mixed     $fromKey    The from key
262
     * @param integer   $fromOption The option for from (SubMap::UNUSED, SubMap::INCLUSIVE or SubMap::EXCLUSIVE)
263
     * @param mixed     $toKey      The to key
264
     * @param integer   $toOption   The option for to (SubMap::UNUSED, SubMap::INCLUSIVE or SubMap::EXCLUSIVE)
265
     *
266
     * @since 1.0.0
267
     */
268
    protected function __construct(SortedMap $map, $fromKey, $fromOption, $toKey, $toOption)
269
    {
270
        $this->mapInternal = $map;
232 ✔
271
        $this->fromKey = $fromKey;
232 ✔
272
        $this->fromOption = $fromOption;
232 ✔
273
        $this->toKey = $toKey;
232 ✔
274
        $this->toOption = $toOption;
232 ✔
275
        $this->setEmpty();
232 ✔
276
    }
277

278
    /**
279
     * Set the empty flag
280
     *
281
     * @return void
282
     *
283
     * @since 1.0.0
284
     */
285
    protected function setEmpty()
286
    {
287
        if ($this->fromOption != self::UNUSED && $this->toOption != self::UNUSED) {
232 ✔
288
            $cmp = call_user_func($this->mapInternal->comparator(), $this->fromKey, $this->toKey);
181 ✔
289

290
            $this->empty = $cmp > 0
181 ✔
291
              || $cmp == 0 && ($this->fromOption == self::EXCLUSIVE || $this->toOption == self::EXCLUSIVE);
181 ✔
292
        } else {
293
            $this->empty = false;
56 ✔
294
        }
295
    }
296

297
    /**
298
     * Create
299
     *
300
     * @param SortedMap $map           A sorted map
301
     * @param mixed     $fromKey       The from key
302
     * @param mixed     $toKey         The to key
303
     * @param boolean   $fromInclusive The inclusive flag for from
304
     * @param boolean   $toInclusive   The inclusive flag for to
305
     *
306
     * @return SubMap A new sub map
307
     *
308
     * @since 1.0.0
309
     */
310
    public static function create(SortedMap $map, $fromKey, $toKey, $fromInclusive = true, $toInclusive = false)
311
    {
312
        return new static(
179 ✔
313
            $map,
179 ✔
314
            $fromKey,
179 ✔
315
            $fromInclusive ? self::INCLUSIVE : self::EXCLUSIVE,
179 ✔
316
            $toKey,
179 ✔
317
            $toInclusive ? self::INCLUSIVE : self::EXCLUSIVE
179 ✔
318
        );
179 ✔
319
    }
320

321
    /**
322
     * Return a head portion of a sorted map
323
     *
324
     * @param SortedMap $map         A sorted map
325
     * @param mixed     $toKey       The to key
326
     * @param boolean   $toInclusive The inclusive flag for to
327
     *
328
     * @return SubMap A new head map
329
     *
330
     * @since 1.0.0
331
     */
332
    public static function head(SortedMap $map, $toKey, $toInclusive = false)
333
    {
334
        return new static($map, null, self::UNUSED, $toKey, $toInclusive ? self::INCLUSIVE : self::EXCLUSIVE);
23 ✔
335
    }
336

337
    /**
338
     * Return a tail portion of a sorted map
339
     *
340
     * @param SortedMap $map           A sorted map
341
     * @param mixed     $fromKey       The from key
342
     * @param boolean   $fromInclusive The inclusive flag for from
343
     *
344
     * @return SubMap A new tail map
345
     *
346
     * @since 1.0.0
347
     */
348
    public static function tail(SortedMap $map, $fromKey, $fromInclusive = true)
349
    {
350
        return new static($map, $fromKey, $fromInclusive ? self::INCLUSIVE : self::EXCLUSIVE, null, self::UNUSED);
26 ✔
351
    }
352

353
    /**
354
     * Return a view of the map
355
     *
356
     * @param SortedMap $map A sorted map
357
     *
358
     * @return SubMap A new sub map
359
     *
360
     * @since 1.0.0
361
     */
362
    public static function view(SortedMap $map)
363
    {
364
        return new static($map, null, self::UNUSED, null, self::UNUSED);
4 ✔
365
    }
366

367
    /**
368
     * Get the comparator
369
     *
370
     * @return callable The comparator
371
     *
372
     * @since 1.0.0
373
     */
374
    public function comparator()
375
    {
376
        return $this->mapInternal->comparator();
1 ✔
377
    }
378

379
    /**
380
     * Get the first element
381
     *
382
     * @return mixed The first element
383
     *
384
     * @throws OutOfBoundsException If there is no element
385
     *
386
     * @since 1.0.0
387
     */
388
    public function first()
389
    {
390
        if ($this->empty) {
41 ✔
391
            throw new \OutOfBoundsException('First element unexisting');
5 ✔
392
        }
393

394
        switch ($this->fromOption) {
37 ✔
395
            case self::INCLUSIVE:
396
                $first = $this->mapInternal->ceiling($this->fromKey);
14 ✔
397
                break;
14 ✔
398
            case self::EXCLUSIVE:
399
                $first = $this->mapInternal->higher($this->fromKey);
18 ✔
400
                break;
18 ✔
401
            default:
402
                $first = $this->mapInternal->first();
5 ✔
403
                break;
5 ✔
404
        }
405

406
        if ($this->isAboveRange($first->key)) {
37 ✔
407
            throw new \OutOfBoundsException('First element unexisting');
1 ✔
408
        }
409

410
        return $first;
37 ✔
411
    }
412

413
    /**
414
     * Get the last element
415
     *
416
     * @return mixed The last element
417
     *
418
     * @throws OutOfBoundsException If there is no element
419
     *
420
     * @since 1.0.0
421
     */
422
    public function last()
423
    {
424
        if ($this->empty) {
18 ✔
425
            throw new \OutOfBoundsException('Last element unexisting');
2 ✔
426
        }
427

428
        switch ($this->toOption) {
16 ✔
429
            case self::INCLUSIVE:
430
                $last = $this->map->floor($this->toKey);
4 ✔
431
                break;
4 ✔
432
            case self::EXCLUSIVE:
433
                $last = $this->map->lower($this->toKey);
11 ✔
434
                break;
11 ✔
435
            default:
436
                $last = $this->map->last();
1 ✔
437
                break;
1 ✔
438
        }
439

440
        if ($this->isBelowRange($last->key)) {
16 ✔
441
            throw new \OutOfBoundsException('Last element unexisting');
1 ✔
442
        }
443

444
        return $last;
16 ✔
445
    }
446

447
    /**
448
     * Check whether a key is below the lower bound
449
     *
450
     * @param mixed $key The key
451
     *
452
     * @return boolean
453
     *
454
     * @since 1.0.0
455
     */
456
    private function isBelowRange($key)
457
    {
458
        if ($this->fromOption == self::UNUSED) {
96 ✔
459
            return false;
9 ✔
460
        }
461

462
        $cmp = call_user_func($this->mapInternal->comparator(), $key, $this->fromKey);
87 ✔
463

464
        return $cmp < 0 || $cmp == 0 && $this->fromOption == self::EXCLUSIVE;
87 ✔
465
    }
466

467
    /**
468
     * Check whether a key is above the upper bound
469
     *
470
     * @param mixed $key The key
471
     *
472
     * @return boolean
473
     *
474
     * @since 1.0.0
475
     */
476
    private function isAboveRange($key)
477
    {
478
        if ($this->toOption == self::UNUSED) {
97 ✔
479
            return false;
14 ✔
480
        }
481

482
        $cmp = call_user_func($this->mapInternal->comparator(), $key, $this->toKey);
83 ✔
483

484
        return $cmp > 0 || $cmp == 0 && $this->toOption == self::EXCLUSIVE;
83 ✔
485
    }
486

487
    /**
488
     * Get the predecessor element
489
     *
490
     * @param TreeNode $element A tree node member of the underlying TreeMap
491
     *
492
     * @return mixed The predecessor element
493
     *
494
     * @throws OutOfBoundsException If there is no predecessor
495
     *
496
     * @since 1.0.0
497
     */
498
    public function predecessor($element)
499
    {
500
        $predecessor = $this->map->predecessor($element);
4 ✔
501

502
        if ($this->isBelowRange($predecessor->key) || $this->isAboveRange($predecessor->key)) {
4 ✔
503
            throw new \OutOfBoundsException('Predecessor element unexisting');
2 ✔
504
        }
505

506
        return $predecessor;
2 ✔
507
    }
508

509
    /**
510
     * Get the successor element
511
     *
512
     * @param TreeNode $element A tree node member of the underlying TreeMap
513
     *
514
     * @return mixed The successor element
515
     *
516
     * @throws OutOfBoundsException If there is no successor
517
     *
518
     * @since 1.0.0
519
     */
520
    public function successor($element)
521
    {
522
        $successor = $this->map->successor($element);
22 ✔
523

524
        if ($this->isBelowRange($successor->key) || $this->isAboveRange($successor->key)) {
22 ✔
525
            throw new \OutOfBoundsException('Successor element unexisting');
17 ✔
526
        }
527

528
        return $successor;
19 ✔
529
    }
530

531
    /**
532
     * Returns the element whose key is the greatest key lesser than the given key
533
     *
534
     * @param mixed $key The searched key
535
     *
536
     * @return mixed The found element
537
     *
538
     * @throws OutOfBoundsException If there is no lower element
539
     *
540
     * @since 1.0.0
541
     */
542
    public function lower($key)
543
    {
544
        if ($this->empty) {
30 ✔
545
            throw new \OutOfBoundsException('Lower element unexisting');
2 ✔
546
        }
547

548
        switch ($this->fromOption) {
28 ✔
549
            case self::UNUSED:
550
                $lower = $this->map->lower($key);
4 ✔
551
                break;
3 ✔
552
            default:
553
                if (call_user_func($this->map->comparator(), $key, $this->fromKey) <= 0) {
24 ✔
554
                    throw new \OutOfBoundsException('Lower element unexisting');
6 ✔
555
                } else {
556
                    $lower = $this->map->lower($key);
18 ✔
557

558
                    if (
559
                        $this->fromOption == self::EXCLUSIVE
18 ✔
560
                          && call_user_func($this->map->comparator(), $lower->key, $this->fromKey) <= 0
18 ✔
561
                    ) {
562
                        throw new \OutOfBoundsException('Lower element unexisting');
2 ✔
563
                    }
564
                }
565
                break;
16 ✔
566
        }
567

568
        if ($this->isBelowRange($lower->key)) {
19 ✔
569
            throw new \OutOfBoundsException('Lower element unexisting');
1 ✔
570
        }
571

572
        if ($this->isAboveRange($lower->key)) {
19 ✔
573
            return $this->last();
4 ✔
574
        }
575

576
        return $lower;
15 ✔
577
    }
578

579
    /**
580
     * Returns the element whose key is the greatest key lesser than or equal to the given key
581
     *
582
     * @param mixed $key The searched key
583
     *
584
     * @return mixed The found element
585
     *
586
     * @throws OutOfBoundsException If there is no floor element
587
     *
588
     * @since 1.0.0
589
     */
590
    public function floor($key)
591
    {
592
        if ($this->empty) {
26 ✔
593
            throw new \OutOfBoundsException('Floor element unexisting');
2 ✔
594
        }
595

596
        switch ($this->fromOption) {
24 ✔
597
            case self::INCLUSIVE:
598
                if (call_user_func($this->map->comparator(), $key, $this->fromKey) < 0) {
9 ✔
599
                    throw new \OutOfBoundsException('Floor element unexisting');
2 ✔
600
                } else {
601
                    $floor = $this->map->floor($key);
7 ✔
602
                }
603
                break;
7 ✔
604
            case self::EXCLUSIVE:
605
                if (call_user_func($this->map->comparator(), $key, $this->fromKey) <= 0) {
12 ✔
606
                    throw new \OutOfBoundsException('Floor element unexisting');
4 ✔
607
                } else {
608
                    $floor = $this->map->floor($key);
8 ✔
609
                }
610
                break;
8 ✔
611
            default:
612
                $floor = $this->map->floor($key);
3 ✔
613
                break;
3 ✔
614
        }
615

616
        if ($this->isBelowRange($floor->key)) {
18 ✔
617
            throw new \OutOfBoundsException('Floor element unexisting');
1 ✔
618
        }
619

620
        if ($this->isAboveRange($floor->key)) {
18 ✔
621
            return $this->last();
8 ✔
622
        }
623

624
        return $floor;
10 ✔
625
    }
626

627
    /**
628
     * Returns the element whose key is equal to the given key
629
     *
630
     * @param mixed $key The searched key
631
     *
632
     * @return mixed The found element
633
     *
634
     * @throws OutOfBoundsException  If there is no such element
635
     *
636
     * @since 1.0.0
637
     */
638
    public function find($key)
639
    {
640
        switch ($this->fromOption) {
56 ✔
641
            case self::INCLUSIVE:
642
                if (call_user_func($this->map->comparator(), $key, $this->fromKey) < 0) {
26 ✔
643
                     throw new \OutOfBoundsException('Element unexisting');
8 ✔
644
                }
645
                break;
18 ✔
646
            case self::EXCLUSIVE:
647
                if (call_user_func($this->map->comparator(), $key, $this->fromKey) <= 0) {
30 ✔
648
                    throw new \OutOfBoundsException('Element unexisting');
8 ✔
649
                }
650
                break;
22 ✔
651
        }
652

653
        switch ($this->toOption) {
40 ✔
654
            case self::INCLUSIVE:
655
                if (call_user_func($this->map->comparator(), $key, $this->toKey) > 0) {
18 ✔
656
                    throw new \OutOfBoundsException('Element unexisting');
4 ✔
657
                }
658
                break;
14 ✔
659
            case self::EXCLUSIVE:
660
                if (call_user_func($this->map->comparator(), $key, $this->toKey) >= 0) {
22 ✔
661
                    throw new \OutOfBoundsException('Element unexisting');
8 ✔
662
                }
663
                break;
14 ✔
664
        }
665

666
        return $this->map->find($key);
28 ✔
667
    }
668

669
    /**
670
     * Returns the element whose key is the lowest key greater than or equal to the given key
671
     *
672
     * @param mixed $key The searched key
673
     *
674
     * @return mixed The found element
675
     *
676
     * @throws OutOfBoundsException If there is no ceiling element
677
     *
678
     * @since 1.0.0
679
     */
680
    public function ceiling($key)
681
    {
682
        if ($this->empty) {
24 ✔
683
            throw new \OutOfBoundsException('Ceiling element unexisting');
2 ✔
684
        }
685

686
        switch ($this->toOption) {
22 ✔
687
            case self::INCLUSIVE:
688
                if (call_user_func($this->map->comparator(), $key, $this->toKey) > 0) {
9 ✔
689
                    throw new \OutOfBoundsException('Ceiling element unexisting');
2 ✔
690
                } else {
691
                    $ceiling = $this->map->ceiling($key);
7 ✔
692
                }
693
                break;
7 ✔
694
            case self::EXCLUSIVE:
695
                if (call_user_func($this->map->comparator(), $key, $this->toKey) >= 0) {
7 ✔
696
                    throw new \OutOfBoundsException('Ceiling element unexisting');
2 ✔
697
                } else {
698
                    $ceiling = $this->map->ceiling($key);
5 ✔
699
                }
700
                break;
5 ✔
701
            default:
702
                $ceiling = $this->map->ceiling($key);
6 ✔
703
                break;
5 ✔
704
        }
705

706
        if ($this->isAboveRange($ceiling->key)) {
17 ✔
707
            throw new \OutOfBoundsException('Ceiling element unexisting');
1 ✔
708
        }
709

710
        if ($this->isBelowRange($ceiling->key)) {
17 ✔
711
            return $this->first();
9 ✔
712
        }
713

714
        return $ceiling;
8 ✔
715
    }
716

717
    /**
718
     * Returns the element whose key is the lowest key greater than to the given key
719
     *
720
     * @param mixed $key The searched key
721
     *
722
     * @return mixed The found element
723
     *
724
     * @throws OutOfBoundsException If there is no higher element
725
     *
726
     * @since 1.0.0
727
     */
728
    public function higher($key)
729
    {
730
        if ($this->empty) {
27 ✔
731
            throw new \OutOfBoundsException('Higher element unexisting');
2 ✔
732
        }
733

734
        switch ($this->toOption) {
25 ✔
735
            case self::UNUSED:
736
                $higher = $this->map->higher($key);
7 ✔
737
                break;
5 ✔
738
            default:
739
                if (call_user_func($this->map->comparator(), $key, $this->toKey) >= 0) {
18 ✔
740
                    throw new \OutOfBoundsException('Higher element unexisting');
5 ✔
741
                } else {
742
                    $higher = $this->map->higher($key);
13 ✔
743

744
                    if (
745
                        $this->toOption == self::EXCLUSIVE
13 ✔
746
                          && call_user_func($this->map->comparator(), $higher->key, $this->toKey) >= 0
13 ✔
747
                    ) {
748
                        throw new \OutOfBoundsException('Higher element unexisting');
2 ✔
749
                    }
750
                }
751
                break;
12 ✔
752
        }
753

754
        if ($this->isAboveRange($higher->key)) {
17 ✔
NEW
755
            throw new \OutOfBoundsException('Higher element unexisting');
×
756
        }
757

758
        if ($this->isBelowRange($higher->key)) {
17 ✔
759
            return $this->first();
4 ✔
760
        }
761

762
        return $higher;
13 ✔
763
    }
764

765
    /**
766
     * Serialize the object
767
     *
768
     * @return array Array of values
769
     *
770
     * @since 1.0.0
771
     */
772
    public function jsonSerialize(): array
773
    {
774
        if ($this->fromOption == self::UNUSED) {
8 ✔
775
            if ($this->toOption == self::UNUSED) {
2 ✔
776
                return array(
1 ✔
777
                    'ViewMap' => array(
1 ✔
778
                        'map' => $this->map->jsonSerialize(),
1 ✔
779
                     )
1 ✔
780
                );
1 ✔
781
            } else {
782
                return array(
1 ✔
783
                    'HeadMap' => array(
1 ✔
784
                        'map' => $this->map->jsonSerialize(),
1 ✔
785
                        'toKey' => $this->toKey,
1 ✔
786
                        'toInclusive' => $this->toOption == self::INCLUSIVE,
1 ✔
787
                    )
1 ✔
788
                );
1 ✔
789
            }
790
        } else {
791
            if ($this->toOption == self::UNUSED) {
6 ✔
792
                return array(
1 ✔
793
                    'TailMap' => array(
1 ✔
794
                        'map' => $this->map->jsonSerialize(),
1 ✔
795
                        'fromKey' => $this->fromKey,
1 ✔
796
                        'fromInclusive' => $this->fromOption == self::INCLUSIVE,
1 ✔
797
                    )
1 ✔
798
                );
1 ✔
799
            } else {
800
                return array(
5 ✔
801
                    'SubMap' => array(
5 ✔
802
                        'map' => $this->map->jsonSerialize(),
5 ✔
803
                        'fromKey' => $this->fromKey,
5 ✔
804
                        'fromInclusive' => $this->fromOption == self::INCLUSIVE,
5 ✔
805
                        'toKey' => $this->toKey,
5 ✔
806
                        'toInclusive' => $this->toOption == self::INCLUSIVE,
5 ✔
807
                    )
5 ✔
808
                );
5 ✔
809
            }
810
        }
811
    }
812

813
    /**
814
     * Count the number of key/value pairs
815
     *
816
     * @return integer
817
     *
818
     * @since 1.0.0
819
     */
820
    public function count(): int
821
    {
822
        $count = 0;
6 ✔
823

824
        foreach ($this as $value) {
6 ✔
825
            $count++;
5 ✔
826
        }
827

828
        return $count;
6 ✔
829
    }
830
}
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