• 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/AbstractSet.php
1
<?php
2

3
/**
4
 * chdemko\SortedCollection\AbstractSet 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
 * Abstract set
19
 *
20
 * @package    SortedCollection
21
 * @subpackage Set
22
 *
23
 * @since 1.0.0
24
 *
25
 * @property-read callable   $comparator  The element comparison function
26
 * @property-read mixed      $first       The first element of the set
27
 * @property-read mixed      $last        The last element of the set
28
 * @property-read integer    $count       The number of elements in the set
29
 */
30
abstract class AbstractSet implements SortedSet
31
{
32
    /**
33
     * @var SortedMap  Underlying map
34
     *
35
     * @since 1.0.0
36
     */
37
    private $map;
38

39
    /**
40
     * Get the map
41
     *
42
     * @return SortedMap The underlying map
43
     *
44
     * @since 1.0.0
45
     */
46
    protected function getMap()
47
    {
48
        return $this->map;
75✔
49
    }
50

51
    /**
52
     * Set the map
53
     *
54
     * @param SortedMap $map The underlying map
55
     *
56
     * @return AbstractSet $this for chaining
57
     *
58
     * @since 1.0.0
59
     */
60
    protected function setMap(SortedMap $map)
61
    {
62
        $this->map = $map;
75✔
63

64
        return $this;
75✔
65
    }
66

67
    /**
68
     * Magic get method
69
     *
70
     * @param string $property The property
71
     *
72
     * @throws RuntimeException If the property does not exist
73
     *
74
     * @return mixed The value associated to the property
75
     *
76
     * @since 1.0.0
77
     */
78
    public function __get($property)
79
    {
80
        switch ($property) {
81
            case 'comparator':
10✔
82
                return $this->comparator();
4✔
83
            case 'first':
6✔
84
                return $this->first();
3✔
85
            case 'last':
3✔
86
                return $this->last();
1✔
87
            case 'count':
2✔
88
                return $this->count();
1✔
89
            default:
90
                throw new \RuntimeException('Undefined property');
1✔
91
        }
92
    }
93

94
    /**
95
     * Get the comparator
96
     *
97
     * @return callable The comparator
98
     *
99
     * @since 1.0.0
100
     */
101
    public function comparator()
102
    {
103
        return $this->map->comparator();
4✔
104
    }
105

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

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

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

150
    /**
151
     * Returns the greatest element lesser than or equal to the given element
152
     *
153
     * @param mixed $element The searched element
154
     *
155
     * @return mixed The found element
156
     *
157
     * @throws OutOfBoundsException If there is no floor element
158
     *
159
     * @since 1.0.0
160
     */
161
    public function floor($element)
162
    {
163
        return $this->map->floorKey($element);
6✔
164
    }
165

166
    /**
167
     * Returns the element equal to the given element
168
     *
169
     * @param mixed $element The searched element
170
     *
171
     * @return mixed The found element
172
     *
173
     * @throws OutOfBoundsException If there is no such element
174
     *
175
     * @since 1.0.0
176
     */
177
    public function find($element)
178
    {
179
        return $this->map->findKey($element);
1✔
180
    }
181

182
    /**
183
     * Returns the lowest element greater than or equal to the given element
184
     *
185
     * @param mixed $element The searched element
186
     *
187
     * @return mixed The found element
188
     *
189
     * @throws OutOfBoundsException If there is no ceiling element
190
     *
191
     * @since 1.0.0
192
     */
193
    public function ceiling($element)
194
    {
195
        return $this->map->ceilingKey($element);
6✔
196
    }
197

198
    /**
199
     * Returns the lowest element greater than to the given element
200
     *
201
     * @param mixed $element The searched element
202
     *
203
     * @return mixed The found element
204
     *
205
     * @throws OutOfBoundsException If there is no higher element
206
     *
207
     * @since 1.0.0
208
     */
209
    public function higher($element)
210
    {
211
        return $this->map->higherKey($element);
6✔
212
    }
213

214
    /**
215
     * Convert the object to a string
216
     *
217
     * @return string String representation of the object
218
     *
219
     * @since 1.0.0
220
     */
221
    public function __toString()
222
    {
223
        return json_encode($this->toArray());
13✔
224
    }
225

226
    /**
227
     * Convert the object to an array
228
     *
229
     * @return array Array representation of the object
230
     *
231
     * @since 1.0.0
232
     */
233
    public function toArray()
234
    {
235
        $array = array();
13✔
236

237
        foreach ($this as $value) {
13✔
238
            $array[] = $value;
12✔
239
        }
240

241
        return $array;
13✔
242
    }
243

244
    /**
245
     * Create an iterator
246
     *
247
     * @return Iterator A new iterator
248
     *
249
     * @since 1.0.0
250
     */
251
    public function getIterator(): Iterator
252
    {
253
        return Iterator::keys($this->map);
23✔
254
    }
255

256
    /**
257
     * Get the value for an element
258
     *
259
     * @param mixed $element The element
260
     *
261
     * @return mixed The found value
262
     *
263
     * @since 1.0.0
264
     */
265
    public function offsetGet($element): mixed
266
    {
267
        try {
268
            return (bool) $this->map->find($element);
9✔
269
        } catch (\OutOfBoundsException $e) {
6✔
270
            return false;
6✔
271
        }
272
    }
273

274
    /**
275
     * Test the existence of an element
276
     *
277
     * @param mixed $element The element
278
     *
279
     * @return boolean TRUE if the element exists, false otherwise
280
     *
281
     * @since 1.0.0
282
     */
283
    public function offsetExists($element): bool
284
    {
285
        return $this->offsetGet($element);
1✔
286
    }
287

288
    /**
289
     * Set the value for an element
290
     *
291
     * @param mixed $element The element
292
     * @param mixed $value   The value
293
     *
294
     * @return void
295
     *
296
     * @throws RuntimeOperation The operation is not supported by this class
297
     *
298
     * @since 1.0.0
299
     */
300
    public function offsetSet($element, $value): void
301
    {
302
        throw new \RuntimeException('Unsupported operation');
1✔
303
    }
304

305
    /**
306
     * Unset the existence of an element
307
     *
308
     * @param mixed $element The element
309
     *
310
     * @return void
311
     *
312
     * @throws RuntimeOperation The operation is not supported by this class
313
     *
314
     * @since 1.0.0
315
     */
316
    public function offsetUnset($element): void
317
    {
318
        throw new \RuntimeException('Unsupported operation');
1✔
319
    }
320

321
    /**
322
     * Count the number of elements
323
     *
324
     * @return integer
325
     *
326
     * @since 1.0.0
327
     */
328
    public function count(): int
329
    {
330
        return count($this->map);
2✔
331
    }
332
}
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