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

chdemko / php-sorted-collections / 12390158994

18 Dec 2024 09:33AM UTC coverage: 100.0%. Remained the same
12390158994

push

github

chdemko
Fix style in benchmarks

754 of 754 relevant lines covered (100.0%)

113.21 hits per line

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

100.0
/src/SortedCollection/AbstractMap.php
1
<?php
2

3
/**
4
 * chdemko\SortedCollection\AbstractMap 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
 * AbstractMap
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
 */
36
abstract class AbstractMap implements SortedMap
37
{
38
    /**
39
     * Magic get method
40
     *
41
     * @param string $property The property
42
     *
43
     * @throws RuntimeException If the property does not exist
44
     *
45
     * @return mixed The value associated to the property
46
     *
47
     * @since 1.0.0
48
     */
49
    public function __get($property)
50
    {
51
        switch ($property) {
52
            case 'comparator':
81✔
53
                return $this->comparator();
56✔
54
            case 'firstKey':
25✔
55
                return $this->firstKey();
6✔
56
            case 'lastKey':
20✔
57
                return $this->lastKey();
6✔
58
            case 'firstValue':
15✔
59
                return $this->firstValue();
1✔
60
            case 'lastValue':
15✔
61
                return $this->lastValue();
1✔
62
            case 'first':
15✔
63
                return $this->first();
6✔
64
            case 'last':
13✔
65
                return $this->last();
6✔
66
            case 'keys':
7✔
67
                return $this->keys();
2✔
68
            case 'values':
5✔
69
                return $this->values();
2✔
70
            case 'count':
3✔
71
                return $this->count();
2✔
72
            default:
73
                throw new \RuntimeException('Undefined property');
1✔
74
        }
75
    }
76

77
    /**
78
     * Get the first key
79
     *
80
     * @return mixed The first key
81
     *
82
     * @throws OutOfBoundsException If there is no element
83
     *
84
     * @since 1.0.0
85
     */
86
    public function firstKey()
87
    {
88
        return $this->first()->key;
9✔
89
    }
90

91
    /**
92
     * Get the first value
93
     *
94
     * @return mixed The first value
95
     *
96
     * @throws OutOfBoundsException If there is no element
97
     *
98
     * @since 1.0.0
99
     */
100
    public function firstValue()
101
    {
102
        return $this->first()->value;
1✔
103
    }
104

105
    /**
106
     * Get the last key
107
     *
108
     * @return mixed The last key
109
     *
110
     * @throws OutOfBoundsException If there is no element
111
     *
112
     * @since 1.0.0
113
     */
114
    public function lastKey()
115
    {
116
        return $this->last()->key;
7✔
117
    }
118

119
    /**
120
     * Get the last value
121
     *
122
     * @return mixed The last value
123
     *
124
     * @throws OutOfBoundsException If there is no element
125
     *
126
     * @since 1.0.0
127
     */
128
    public function lastValue()
129
    {
130
        return $this->last()->value;
1✔
131
    }
132

133
    /**
134
     * Returns the greatest key lesser than the given key
135
     *
136
     * @param mixed $key The searched key
137
     *
138
     * @return mixed The found key
139
     *
140
     * @throws OutOfBoundsException If there is no lower element
141
     *
142
     * @since 1.0.0
143
     */
144
    public function lowerKey($key)
145
    {
146
        return $this->lower($key)->key;
47✔
147
    }
148

149
    /**
150
     * Returns the value whose key is the greatest key lesser than the given key
151
     *
152
     * @param mixed $key The searched key
153
     *
154
     * @return mixed The found value
155
     *
156
     * @throws OutOfBoundsException If there is no lower element
157
     *
158
     * @since 1.0.0
159
     */
160
    public function lowerValue($key)
161
    {
162
        return $this->lower($key)->value;
2✔
163
    }
164

165
    /**
166
     * Returns the greatest key lesser than or equal to the given key
167
     *
168
     * @param mixed $key The searched key
169
     *
170
     * @return mixed The found key
171
     *
172
     * @throws OutOfBoundsException If there is no floor element
173
     *
174
     * @since 1.0.0
175
     */
176
    public function floorKey($key)
177
    {
178
        return $this->floor($key)->key;
43✔
179
    }
180

181
    /**
182
     * Returns the value whose key is the greatest key lesser than or equal to the given key
183
     *
184
     * @param mixed $key The searched key
185
     *
186
     * @return mixed The found value
187
     *
188
     * @throws OutOfBoundsException If there is no floor element
189
     *
190
     * @since 1.0.0
191
     */
192
    public function floorValue($key)
193
    {
194
        return $this->floor($key)->value;
4✔
195
    }
196

197
    /**
198
     * Returns the key equal to the given key
199
     *
200
     * @param mixed $key The searched key
201
     *
202
     * @return mixed The found key
203
     *
204
     * @throws OutOfBoundsException If there is no such element
205
     *
206
     * @since 1.0.0
207
     */
208
    public function findKey($key)
209
    {
210
        return $this->find($key)->key;
27✔
211
    }
212

213
    /**
214
     * Returns the value whose key equal to the given key
215
     *
216
     * @param mixed $key The searched key
217
     *
218
     * @return mixed The found value
219
     *
220
     * @throws OutOfBoundsException If there is no such element
221
     *
222
     * @since 1.0.0
223
     */
224
    public function findValue($key)
225
    {
226
        return $this->find($key)->value;
1✔
227
    }
228

229
    /**
230
     * Returns the lowest key greater than or equal to the given key
231
     *
232
     * @param mixed $key The searched key
233
     *
234
     * @return mixed The found key
235
     *
236
     * @throws OutOfBoundsException If there is no ceiling element
237
     *
238
     * @since 1.0.0
239
     */
240
    public function ceilingKey($key)
241
    {
242
        return $this->ceiling($key)->key;
41✔
243
    }
244

245
    /**
246
     * Returns the value whose key is the lowest key greater than or equal to the given key
247
     *
248
     * @param mixed $key The searched key
249
     *
250
     * @return mixed The found value
251
     *
252
     * @throws OutOfBoundsException If there is no ceiling element
253
     *
254
     * @since 1.0.0
255
     */
256
    public function ceilingValue($key)
257
    {
258
        return $this->ceiling($key)->value;
4✔
259
    }
260

261
    /**
262
     * Returns the lowest key greater than to the given key
263
     *
264
     * @param mixed $key The searched key
265
     *
266
     * @return mixed The found key
267
     *
268
     * @throws OutOfBoundsException If there is no higher element
269
     *
270
     * @since 1.0.0
271
     */
272
    public function higherKey($key)
273
    {
274
        return $this->higher($key)->key;
44✔
275
    }
276

277
    /**
278
     * Returns the value whose key is the lowest key greater than to the given key
279
     *
280
     * @param mixed $key The searched key
281
     *
282
     * @return mixed The found value
283
     *
284
     * @throws OutOfBoundsException If there is no higher element
285
     *
286
     * @since 1.0.0
287
     */
288
    public function higherValue($key)
289
    {
290
        return $this->higher($key)->value;
2✔
291
    }
292

293
    /**
294
     * Keys iterator
295
     *
296
     * @return Iterator The keys iterator
297
     *
298
     * @since 1.0.0
299
     */
300
    public function keys()
301
    {
302
        return Iterator::keys($this);
2✔
303
    }
304

305
    /**
306
     * Values iterator
307
     *
308
     * @return Iterator The values iterator
309
     *
310
     * @since 1.0.0
311
     */
312
    public function values()
313
    {
314
        return Iterator::values($this);
2✔
315
    }
316

317
    /**
318
     * Convert the object to a string
319
     *
320
     * @return string String representation of the object
321
     *
322
     * @since 1.0.0
323
     */
324
    public function __toString()
325
    {
326
        return json_encode($this->toArray());
8✔
327
    }
328

329
    /**
330
     * Convert the object to an array
331
     *
332
     * @return array Array representation of the object
333
     *
334
     * @since 1.0.0
335
     */
336
    public function toArray()
337
    {
338
        $array = array();
363✔
339

340
        foreach ($this as $key => $value) {
363✔
341
            $array[$key] = $value;
360✔
342
        }
343

344
        return $array;
363✔
345
    }
346

347
    /**
348
     * Create an iterator
349
     *
350
     * @return Iterator A new iterator
351
     *
352
     * @since 1.0.0
353
     */
354
    public function getIterator(): Iterator
355
    {
356
        return Iterator::create($this);
378✔
357
    }
358

359
    /**
360
     * Get the value for a key
361
     *
362
     * @param mixed $key The key
363
     *
364
     * @return mixed The found value
365
     *
366
     * @throws OutOfRangeException If there is no such element
367
     *
368
     * @since 1.0.0
369
     */
370
    public function offsetGet($key): mixed
371
    {
372
        try {
373
            return $this->find($key)->value;
38✔
374
        } catch (\OutOfBoundsException $e) {
21✔
375
            throw new \OutOfRangeException('Undefined offset');
21✔
376
        }
377
    }
378

379
    /**
380
     * Test the existence of a key
381
     *
382
     * @param mixed $key The key
383
     *
384
     * @return boolean TRUE if the key exists, false otherwise
385
     *
386
     * @since 1.0.0
387
     */
388
    public function offsetExists($key): bool
389
    {
390
        try {
391
            return (bool) $this->find($key);
2✔
392
        } catch (\OutOfBoundsException $e) {
2✔
393
            return false;
2✔
394
        }
395
    }
396

397
    /**
398
     * Set the value for a key
399
     *
400
     * @param mixed $key   The key
401
     * @param mixed $value The value
402
     *
403
     * @return void
404
     *
405
     * @throws RuntimeOperation The operation is not supported by this class
406
     *
407
     * @since 1.0.0
408
     */
409
    public function offsetSet($key, $value): void
410
    {
411
        throw new \RuntimeException('Unsupported operation');
1✔
412
    }
413

414
    /**
415
     * Unset the existence of a key
416
     *
417
     * @param mixed $key The key
418
     *
419
     * @return void
420
     *
421
     * @throws RuntimeOperation The operation is not supported by this class
422
     *
423
     * @since 1.0.0
424
     */
425
    public function offsetUnset($key): void
426
    {
427
        throw new \RuntimeException('Unsupported operation');
1✔
428
    }
429
}
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

© 2025 Coveralls, Inc