Passed
Push — consistent-method-naming ( ecd7a5 )
by Marc
04:08
created

EnumSet::withoutIterable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MabeEnum;
6
7
use Countable;
8
use InvalidArgumentException;
9
use Iterator;
10
use IteratorAggregate;
11
12
/**
13
 * A set of enumerators of the given enumeration (EnumSet<T extends Enum>)
14
 * based on an integer or binary bitset depending of given enumeration size.
15
 *
16
 * @copyright 2019 Marc Bennewitz
17
 * @license http://github.com/marc-mabe/php-enum/blob/master/LICENSE.txt New BSD License
18
 * @link http://github.com/marc-mabe/php-enum for the canonical source repository
19
 */
3 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
20
class EnumSet implements IteratorAggregate, Countable
21
{
22
    /**
23
     * The classname of the Enumeration
24
     * @var string
25
     */
26
    private $enumeration;
1 ignored issue
show
Coding Style introduced by
Expected 1 blank line before member var; 0 found
Loading history...
Coding Style introduced by
Private member variable "enumeration" must contain a leading underscore
Loading history...
27
28
    /**
29
     * Number of enumerators defined in the enumeration
30
     * @var int
31
     */
32
    private $enumerationCount;
1 ignored issue
show
Coding Style introduced by
Private member variable "enumerationCount" must contain a leading underscore
Loading history...
33
34
    /**
35
     * Integer or binary (little endian) bitset
36
     * @var int|string
37
     */
38
    private $bitset = 0;
1 ignored issue
show
Coding Style introduced by
Private member variable "bitset" must contain a leading underscore
Loading history...
39
40
    /**#@+
41
     * Defines private method names to be called depended of how the bitset type was set too.
42
     * ... Integer or binary bitset.
43
     * ... *Int or *Bin method
44
     * 
45
     * @var string
46
     */
47
    private $fnDoGetIterator       = 'doGetIteratorInt';
1 ignored issue
show
Coding Style introduced by
Private member variable "fnDoGetIterator" must contain a leading underscore
Loading history...
48
    private $fnDoCount             = 'doCountInt';
1 ignored issue
show
Coding Style introduced by
Private member variable "fnDoCount" must contain a leading underscore
Loading history...
Coding Style introduced by
Expected 1 blank line before member var; 0 found
Loading history...
49
    private $fnDoGetOrdinals       = 'doGetOrdinalsInt';
1 ignored issue
show
Coding Style introduced by
Expected 1 blank line before member var; 0 found
Loading history...
Coding Style introduced by
Private member variable "fnDoGetOrdinals" must contain a leading underscore
Loading history...
50
    private $fnDoGetBit            = 'doGetBitInt';
1 ignored issue
show
Coding Style introduced by
Expected 1 blank line before member var; 0 found
Loading history...
Coding Style introduced by
Private member variable "fnDoGetBit" must contain a leading underscore
Loading history...
51
    private $fnDoSetBit            = 'doSetBitInt';
1 ignored issue
show
Coding Style introduced by
Expected 1 blank line before member var; 0 found
Loading history...
Coding Style introduced by
Private member variable "fnDoSetBit" must contain a leading underscore
Loading history...
52
    private $fnDoUnsetBit          = 'doUnsetBitInt';
1 ignored issue
show
Coding Style introduced by
Expected 1 blank line before member var; 0 found
Loading history...
Coding Style introduced by
Private member variable "fnDoUnsetBit" must contain a leading underscore
Loading history...
53
    private $fnDoGetBinaryBitsetLe = 'doGetBinaryBitsetLeInt';
1 ignored issue
show
Coding Style introduced by
Expected 1 blank line before member var; 0 found
Loading history...
Coding Style introduced by
Private member variable "fnDoGetBinaryBitsetLe" must contain a leading underscore
Loading history...
54
    private $fnDoSetBinaryBitsetLe = 'doSetBinaryBitsetLeInt';
1 ignored issue
show
Coding Style introduced by
Expected 1 blank line before member var; 0 found
Loading history...
Coding Style introduced by
Private member variable "fnDoSetBinaryBitsetLe" must contain a leading underscore
Loading history...
55
    /**#@-*/
56
57
    /**
58
     * Constructor
59
     *
60
     * @param string $enumeration The classname of the enumeration
1 ignored issue
show
introduced by
Parameter comment must end with a full stop
Loading history...
Coding Style introduced by
Expected 8 spaces after parameter type; 1 found
Loading history...
61
     * @param iterable|null $enumerators iterable list of enumerators initializing the set
1 ignored issue
show
introduced by
Parameter comment must start with a capital letter
Loading history...
introduced by
Parameter comment must end with a full stop
Loading history...
62
     * @throws InvalidArgumentException
0 ignored issues
show
introduced by
Comment missing for @throws tag in function comment
Loading history...
63
     */
64 95
    public function __construct(string $enumeration, iterable $enumerators = null)
2 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$enumerators" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$enumerators"; expected 0 but found 1
Loading history...
65
    {
66 95
        if (!\is_subclass_of($enumeration, Enum::class)) {
67 1
            throw new InvalidArgumentException(\sprintf(
68 1
                '%s can handle subclasses of %s only',
69 1
                __METHOD__,
70 1
                Enum::class
71
            ));
72
        }
73
74 94
        $this->enumeration      = $enumeration;
75 94
        $this->enumerationCount = \count($enumeration::getConstants());
76
77
        // By default the bitset is initialized as integer bitset
78
        // in case the enumeraton has more enumerators then integer bits
79
        // we will switch this into a binary bitset
80 94
        if ($this->enumerationCount > \PHP_INT_SIZE * 8) {
81
            // init binary bitset with zeros
82 23
            $this->bitset = \str_repeat("\0", (int)\ceil($this->enumerationCount / 8));
83
84
            // switch internal binary bitset functions
85 23
            $this->fnDoGetIterator       = 'doGetIteratorBin';
86 23
            $this->fnDoCount             = 'doCountBin';
87 23
            $this->fnDoGetOrdinals       = 'doGetOrdinalsBin';
88 23
            $this->fnDoGetBit            = 'doGetBitBin';
89 23
            $this->fnDoSetBit            = 'doSetBitBin';
90 23
            $this->fnDoUnsetBit          = 'doUnsetBitBin';
91 23
            $this->fnDoGetBinaryBitsetLe = 'doGetBinaryBitsetLeBin';
92 23
            $this->fnDoSetBinaryBitsetLe = 'doSetBinaryBitsetLeBin';
93
        }
94
95 94
        if ($enumerators !== null) {
96 3
            foreach ($enumerators as $enumerator) {
97 3
                $this->{$this->fnDoSetBit}($enumeration::get($enumerator)->getOrdinal());
98
            }
99
        }
100 94
    }
101
102
    /**
103
     * Get the classname of the enumeration
104
     * @return string
105
     */
106 2
    public function getEnumeration(): string
107
    {
108 2
        return $this->enumeration;
109
    }
110
111
    /* write access (mutable) */
112
113
    /**
114
     * Adds an enumerator object or value
115
     * @param Enum|null|bool|int|float|string|array $enumerator Enumerator object or value
2 ignored issues
show
Coding Style introduced by
Expected "Enum|null|boolean|integer|float|string|array" but found "Enum|null|bool|int|float|string|array" for parameter type
Loading history...
introduced by
Parameter comment must end with a full stop
Loading history...
116
     * @return void
117
     * @throws InvalidArgumentException On an invalid given enumerator
0 ignored issues
show
introduced by
@throws tag comment must end with a full stop
Loading history...
118
     */
119 21
    public function add($enumerator): void
120
    {
121 21
        $this->{$this->fnDoSetBit}(($this->enumeration)::get($enumerator)->getOrdinal());
122 21
    }
123
124
    /**
125
     * Adds all enumerator objects or values of the given iterable
126
     * @param iterable $enumerators Iterable list of enumerator objects or values
1 ignored issue
show
introduced by
Parameter comment must end with a full stop
Loading history...
127
     * @return void
128
     * @throws InvalidArgumentException On an invalid given enumerator
0 ignored issues
show
introduced by
@throws tag comment must end with a full stop
Loading history...
129
     */
130 7
    public function addIterable(iterable $enumerators): void
131
    {
132 7
        $bitset = $this->bitset;
133
134
        try {
135 7
            foreach ($enumerators as $enumerator) {
136 7
                $this->{$this->fnDoSetBit}(($this->enumeration)::get($enumerator)->getOrdinal());
137
            }
138 1
        } catch (\Throwable $e) {
139
            // reset all changes until error happened
140 1
            $this->bitset = $bitset;
141 1
            throw $e;
142
        }
143 6
    }
144
145
    /**
146
     * Removes the given enumerator object or value
147
     * @param Enum|null|bool|int|float|string|array $enumerator Enumerator object or value
2 ignored issues
show
Coding Style introduced by
Expected "Enum|null|boolean|integer|float|string|array" but found "Enum|null|bool|int|float|string|array" for parameter type
Loading history...
introduced by
Parameter comment must end with a full stop
Loading history...
148
     * @return void
149
     * @throws InvalidArgumentException On an invalid given enumerator
0 ignored issues
show
introduced by
@throws tag comment must end with a full stop
Loading history...
150
     */
151 8
    public function remove($enumerator): void
152
    {
153 8
        $this->{$this->fnDoUnsetBit}(($this->enumeration)::get($enumerator)->getOrdinal());
154 8
    }
155
156
    /**
157
     * Adds an enumerator object or value
158
     * @param Enum|null|bool|int|float|string|array $enumerator Enumerator object or value
2 ignored issues
show
Coding Style introduced by
Expected "Enum|null|boolean|integer|float|string|array" but found "Enum|null|bool|int|float|string|array" for parameter type
Loading history...
introduced by
Parameter comment must end with a full stop
Loading history...
159
     * @return void
160
     * @throws InvalidArgumentException On an invalid given enumerator
0 ignored issues
show
introduced by
@throws tag comment must end with a full stop
Loading history...
161
     * @see add()
162
     * @deprecated Will trigger deprecation warning in last 4.x and removed in 5.x
163
     */
164 1
    public function attach($enumerator): void
165
    {
166 1
        $this->add($enumerator);
167 1
    }
168
169
    /**
170
     * Removes the given enumerator object or value
171
     * @param Enum|null|bool|int|float|string|array $enumerator Enumerator object or value
2 ignored issues
show
Coding Style introduced by
Expected "Enum|null|boolean|integer|float|string|array" but found "Enum|null|bool|int|float|string|array" for parameter type
Loading history...
introduced by
Parameter comment must end with a full stop
Loading history...
172
     * @return void
173
     * @throws InvalidArgumentException On an invalid given enumerator
0 ignored issues
show
introduced by
@throws tag comment must end with a full stop
Loading history...
174
     * @see remove()
175
     * @deprecated Will trigger deprecation warning in last 4.x and removed in 5.x
176
     */
177 1
    public function detach($enumerator): void
178
    {
179 1
        $this->remove($enumerator);
180 1
    }
181
182
    /**
183
     * Removes all enumerator objects or values of the given iterable
184
     * @param iterable $enumerators Iterable list of enumerator objects or values
1 ignored issue
show
introduced by
Parameter comment must end with a full stop
Loading history...
185
     * @return void
186
     * @throws InvalidArgumentException On an invalid given enumerator
0 ignored issues
show
introduced by
@throws tag comment must end with a full stop
Loading history...
187
     */
188 6
    public function removeIterable(iterable $enumerators): void
189
    {
190 6
        $bitset = $this->bitset;
191
192
        try {
193 6
            foreach ($enumerators as $enumerator) {
194 6
                $this->{$this->fnDoUnsetBit}(($this->enumeration)::get($enumerator)->getOrdinal());
195
            }
196 1
        } catch (\Throwable $e) {
197
            // reset all changes until error happened
198 1
            $this->bitset = $bitset;
199 1
            throw $e;
200
        }
201 5
    }
202
203
    /**
204
     * Modify this set from both this and other (this | other)
205
     *
206
     * @param EnumSet $other EnumSet of the same enumeration to produce the union
1 ignored issue
show
introduced by
Parameter comment must end with a full stop
Loading history...
207
     * @return void
208
     * @throws InvalidArgumentException If $other doesn't match the enumeration
0 ignored issues
show
introduced by
@throws tag comment must end with a full stop
Loading history...
209
     */
210 3
    public function setUnion(EnumSet $other): void
211
    {
212 3
        if ($this->enumeration !== $other->enumeration) {
213 1
            throw new InvalidArgumentException(\sprintf(
214 1
                'Other should be of the same enumeration as this %s',
215 1
                $this->enumeration
216
            ));
217
        }
218
219 2
        $this->bitset = $this->bitset | $other->bitset;
220 2
    }
221
222
    /**
223
     * Modify this set with enumerators common to both this and other (this & other)
224
     *
225
     * @param EnumSet $other EnumSet of the same enumeration to produce the intersect
1 ignored issue
show
introduced by
Parameter comment must end with a full stop
Loading history...
226
     * @return void
227
     * @throws InvalidArgumentException If $other doesn't match the enumeration
0 ignored issues
show
introduced by
@throws tag comment must end with a full stop
Loading history...
228
     */
229 3
    public function setIntersect(EnumSet $other): void
230
    {
231 3
        if ($this->enumeration !== $other->enumeration) {
232 1
            throw new InvalidArgumentException(\sprintf(
233 1
                'Other should be of the same enumeration as this %s',
234 1
                $this->enumeration
235
            ));
236
        }
237
238 2
        $this->bitset = $this->bitset & $other->bitset;
239 2
    }
240
241
    /**
242
     * Modify this set with enumerators in this but not in other (this - other)
243
     *
244
     * @param EnumSet $other EnumSet of the same enumeration to produce the diff
1 ignored issue
show
introduced by
Parameter comment must end with a full stop
Loading history...
245
     * @return void
246
     * @throws InvalidArgumentException If $other doesn't match the enumeration
0 ignored issues
show
introduced by
@throws tag comment must end with a full stop
Loading history...
247
     */
248 3
    public function setDiff(EnumSet $other): void
249
    {
250 3
        if ($this->enumeration !== $other->enumeration) {
251 1
            throw new InvalidArgumentException(\sprintf(
252 1
                'Other should be of the same enumeration as this %s',
253 1
                $this->enumeration
254
            ));
255
        }
256
257 2
        $this->bitset = $this->bitset & ~$other->bitset;
258 2
    }
259
260
    /**
261
     * Modify this set with enumerators in either this and other but not in both (this ^ other)
262
     *
263
     * @param EnumSet $other EnumSet of the same enumeration to produce the symmetric difference
1 ignored issue
show
introduced by
Parameter comment must end with a full stop
Loading history...
264
     * @return void
265
     * @throws InvalidArgumentException If $other doesn't match the enumeration
0 ignored issues
show
introduced by
@throws tag comment must end with a full stop
Loading history...
266
     */
267 3
    public function setSymDiff(EnumSet $other): void
268
    {
269 3
        if ($this->enumeration !== $other->enumeration) {
270 1
            throw new InvalidArgumentException(\sprintf(
271 1
                'Other should be of the same enumeration as this %s',
272 1
                $this->enumeration
273
            ));
274
        }
275
276 2
        $this->bitset = $this->bitset ^ $other->bitset;
277 2
    }
278
279
    /**
280
     * Set the given binary bitset in little-endian order
281
     *
282
     * @param string $bitset
0 ignored issues
show
Documentation introduced by
Missing parameter comment
Loading history...
283
     * @return void
284
     * @throws InvalidArgumentException On out-of-range bits given as input bitset
0 ignored issues
show
introduced by
@throws tag comment must end with a full stop
Loading history...
285
     * @uses doSetBinaryBitsetLeBin()
286
     * @uses doSetBinaryBitsetLeInt()
287
     */
288 1
    public function setBinaryBitsetLe(string $bitset): void
289
    {
290 1
        $this->{$this->fnDoSetBinaryBitsetLe}($bitset);
291 1
    }
292
293
    /**
294
     * Set binary bitset in little-endian order
295
     *
296
     * @param string $bitset
0 ignored issues
show
Documentation introduced by
Missing parameter comment
Loading history...
297
     * @return void
298
     * @throws InvalidArgumentException On out-of-range bits given as input bitset
0 ignored issues
show
introduced by
@throws tag comment must end with a full stop
Loading history...
299
     * @see setBinaryBitsetLeBin()
300
     * @see doSetBinaryBitsetLeInt()
301
     */
302 9
    private function doSetBinaryBitsetLeBin($bitset): void
0 ignored issues
show
Coding Style introduced by
Type hint "string" missing for $bitset
Loading history...
303
    {
304 9
        $size   = \strlen($this->bitset);
305 9
        $sizeIn = \strlen($bitset);
306
307 9
        if ($sizeIn < $size) {
308
            // add "\0" if the given bitset is not long enough
309 1
            $bitset .= \str_repeat("\0", $size - $sizeIn);
310 8
        } elseif ($sizeIn > $size) {
311 2
            if (\ltrim(\substr($bitset, $size), "\0") !== '') {
312 1
                throw new InvalidArgumentException('out-of-range bits detected');
313
            }
314 1
            $bitset = \substr($bitset, 0, $size);
315
        }
316
317
        // truncate out-of-range bits of last byte
318 8
        $lastByteMaxOrd = $this->enumerationCount % 8;
319 8
        if ($lastByteMaxOrd !== 0) {
320 8
            $lastByte         = $bitset[-1];
321 8
            $lastByteExpected = \chr((1 << $lastByteMaxOrd) - 1) & $lastByte;
322 8
            if ($lastByte !== $lastByteExpected) {
323 2
                throw new InvalidArgumentException('out-of-range bits detected');
324
            }
325
326 6
            $this->bitset = \substr($bitset, 0, -1) . $lastByteExpected;
327
        }
328
329 6
        $this->bitset = $bitset;
330 6
    }
331
332
    /**
333
     * Set binary bitset in little-endian order
334
     *
335
     * @param string $bitset
0 ignored issues
show
Documentation introduced by
Missing parameter comment
Loading history...
336
     * @return void
337
     * @throws InvalidArgumentException On out-of-range bits given as input bitset
0 ignored issues
show
introduced by
@throws tag comment must end with a full stop
Loading history...
338
     * @see setBinaryBitsetLeBin()
339
     * @see doSetBinaryBitsetLeBin()
340
     */
341 5
    private function doSetBinaryBitsetLeInt($bitset): void
0 ignored issues
show
Coding Style introduced by
Type hint "string" missing for $bitset
Loading history...
342
    {
343 5
        $len = \strlen($bitset);
344 5
        $int = 0;
345 5
        for ($i = 0; $i < $len; ++$i) {
346 5
            $ord = \ord($bitset[$i]);
347
348 5
            if ($ord && $i > \PHP_INT_SIZE - 1) {
349 1
                throw new InvalidArgumentException('out-of-range bits detected');
350
            }
351
352 5
            $int |= $ord << (8 * $i);
353
        }
354
355 4
        if ($int & (~0 << $this->enumerationCount)) {
356 2
            throw new InvalidArgumentException('out-of-range bits detected');
357
        }
358
359 2
        $this->bitset = $int;
360 2
    }
361
362
    /**
363
     * Set the given binary bitset in big-endian order
364
     *
365
     * @param string $bitset
0 ignored issues
show
Documentation introduced by
Missing parameter comment
Loading history...
366
     * @return void
367
     * @throws InvalidArgumentException On out-of-range bits given as input bitset
0 ignored issues
show
introduced by
@throws tag comment must end with a full stop
Loading history...
368
     */
369 1
    public function setBinaryBitsetBe(string $bitset): void
370
    {
371 1
        $this->{$this->fnDoSetBinaryBitsetLe}(\strrev($bitset));
372 1
    }
373
374
    /**
375
     * Set a bit at the given ordinal number
376
     *
377
     * @param int $ordinal Ordinal number of bit to set
2 ignored issues
show
Coding Style introduced by
Expected "integer" but found "int" for parameter type
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
introduced by
Parameter comment must end with a full stop
Loading history...
378
     * @param bool $bit    The bit to set
2 ignored issues
show
Coding Style introduced by
Expected "boolean" but found "bool" for parameter type
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter name; 4 found
Loading history...
introduced by
Parameter comment must end with a full stop
Loading history...
379
     * @return void
380
     * @throws InvalidArgumentException If the given ordinal number is out-of-range
0 ignored issues
show
introduced by
@throws tag comment must end with a full stop
Loading history...
381
     * @uses doSetBitBin()
382
     * @uses doSetBitInt()
383
     * @uses doUnsetBitBin()
384
     * @uses doUnsetBitInt()
385
     */
386 3
    public function setBit(int $ordinal, bool $bit): void
387
    {
388 3
        if ($ordinal < 0 || $ordinal > $this->enumerationCount) {
389 1
            throw new InvalidArgumentException("Ordinal number must be between 0 and {$this->enumerationCount}");
1 ignored issue
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $this instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 113 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
390
        }
391
392 2
        if ($bit) {
393 2
            $this->{$this->fnDoSetBit}($ordinal);
394
        } else {
395 2
            $this->{$this->fnDoUnsetBit}($ordinal);
396
        }
397 2
    }
398
399
    /**
400
     * Set a bit at the given ordinal number.
401
     *
402
     * This is the binary bitset implementation.
403
     *
404
     * @param int $ordinal Ordinal number of bit to set
2 ignored issues
show
Coding Style introduced by
Expected "integer" but found "int" for parameter type
Loading history...
introduced by
Parameter comment must end with a full stop
Loading history...
405
     * @return void
406
     * @see setBit()
407
     * @see doSetBitInt()
408
     */
409 14
    private function doSetBitBin($ordinal): void
0 ignored issues
show
Coding Style introduced by
Type hint "int" missing for $ordinal
Loading history...
410
    {
411 14
        $byte = (int) ($ordinal / 8);
412 14
        $this->bitset[$byte] = $this->bitset[$byte] | \chr(1 << ($ordinal % 8));
413 14
    }
414
415
    /**
416
     * Set a bit at the given ordinal number.
417
     *
418
     * This is the binary bitset implementation.
419
     *
420
     * @param int $ordinal Ordinal number of bit to set
2 ignored issues
show
Coding Style introduced by
Expected "integer" but found "int" for parameter type
Loading history...
introduced by
Parameter comment must end with a full stop
Loading history...
421
     * @return void
422
     * @see setBit()
423
     * @see doSetBitBin()
424
     */
425 56
    private function doSetBitInt($ordinal): void
0 ignored issues
show
Coding Style introduced by
Type hint "int" missing for $ordinal
Loading history...
426
    {
427 56
        $this->bitset = $this->bitset | (1 << $ordinal);
428 56
    }
429
430
    /**
431
     * Unset a bit at the given ordinal number.
432
     *
433
     * This is the binary bitset implementation.
434
     *
435
     * @param int $ordinal Ordinal number of bit to unset
2 ignored issues
show
Coding Style introduced by
Expected "integer" but found "int" for parameter type
Loading history...
introduced by
Parameter comment must end with a full stop
Loading history...
436
     * @return void
437
     * @see setBit()
438
     * @see doUnsetBitInt()
439
     */
440 10
    private function doUnsetBitBin($ordinal): void
0 ignored issues
show
Coding Style introduced by
Type hint "int" missing for $ordinal
Loading history...
441
    {
442 10
        $byte = (int) ($ordinal / 8);
443 10
        $this->bitset[$byte] = $this->bitset[$byte] & \chr(~(1 << ($ordinal % 8)));
444 10
    }
445
446
    /**
447
     * Unset a bit at the given ordinal number.
448
     *
449
     * This is the integer bitset implementation.
450
     *
451
     * @param int $ordinal Ordinal number of bit to unset
2 ignored issues
show
Coding Style introduced by
Expected "integer" but found "int" for parameter type
Loading history...
introduced by
Parameter comment must end with a full stop
Loading history...
452
     * @return void
453
     * @see setBit()
454
     * @see doUnsetBitBin()
455
     */
456 20
    private function doUnsetBitInt($ordinal): void
0 ignored issues
show
Coding Style introduced by
Type hint "int" missing for $ordinal
Loading history...
457
    {
458 20
        $this->bitset = $this->bitset & ~(1 << $ordinal);
459 20
    }
460
461
    /* write access (immutable) */
462
463
    /**
464
     * Creates a new set with the given enumerator object or value added
465
     * @param Enum|null|bool|int|float|string|array $enumerator Enumerator object or value
2 ignored issues
show
Coding Style introduced by
Expected "Enum|null|boolean|integer|float|string|array" but found "Enum|null|bool|int|float|string|array" for parameter type
Loading history...
introduced by
Parameter comment must end with a full stop
Loading history...
466
     * @return static
467
     * @throws InvalidArgumentException On an invalid given enumerator
0 ignored issues
show
introduced by
@throws tag comment must end with a full stop
Loading history...
468
     */
469 33
    public function with($enumerator): self
470
    {
471 33
        $clone = clone $this;
472 33
        $clone->{$this->fnDoSetBit}(($this->enumeration)::get($enumerator)->getOrdinal());
473 33
        return $clone;
474
    }
475
476
    /**
477
     * Creates a new set with the given enumeration objects or values added
478
     * @param iterable $enumerators Iterable list of enumerator objects or values
1 ignored issue
show
introduced by
Parameter comment must end with a full stop
Loading history...
479
     * @return static
480
     * @throws InvalidArgumentException On an invalid given enumerator
0 ignored issues
show
introduced by
@throws tag comment must end with a full stop
Loading history...
481
     */
482 5
    public function withIterable(iterable $enumerators): self
483
    {
484 5
        $clone = clone $this;
485 5
        foreach ($enumerators as $enumerator) {
486 5
            $clone->{$this->fnDoSetBit}(($this->enumeration)::get($enumerator)->getOrdinal());
487
        }
488 5
        return $clone;
489
    }
490
491
    /**
492
     * Create a new set with the given enumerator object or value removed
493
     * @param Enum|null|bool|int|float|string|array $enumerator Enumerator object or value
2 ignored issues
show
Coding Style introduced by
Expected "Enum|null|boolean|integer|float|string|array" but found "Enum|null|bool|int|float|string|array" for parameter type
Loading history...
introduced by
Parameter comment must end with a full stop
Loading history...
494
     * @return static
495
     * @throws InvalidArgumentException On an invalid given enumerator
0 ignored issues
show
introduced by
@throws tag comment must end with a full stop
Loading history...
496
     */
497 9
    public function without($enumerator): self
498
    {
499 9
        $clone = clone $this;
500 9
        $clone->{$this->fnDoUnsetBit}(($this->enumeration)::get($enumerator)->getOrdinal());
501 9
        return $clone;
502
    }
503
504
    /**
505
     * Creates a new set with the given enumeration objects or values removed
506
     * @param iterable $enumerators Iterable list of enumerator objects or values
1 ignored issue
show
introduced by
Parameter comment must end with a full stop
Loading history...
507
     * @return static
508
     * @throws InvalidArgumentException On an invalid given enumerator
0 ignored issues
show
introduced by
@throws tag comment must end with a full stop
Loading history...
509
     */
510 5
    public function withoutIterable(iterable $enumerators): self
511
    {
512 5
        $clone = clone $this;
513 5
        foreach ($enumerators as $enumerator) {
514 5
            $clone->{$this->fnDoUnsetBit}(($this->enumeration)::get($enumerator)->getOrdinal());
515
        }
516 5
        return $clone;
517
    }
518
519
    /**
520
     * Create a new set with enumerators from both this and other (this | other)
521
     *
522
     * @param EnumSet $other EnumSet of the same enumeration to produce the union
1 ignored issue
show
introduced by
Parameter comment must end with a full stop
Loading history...
523
     * @return static
524
     * @throws InvalidArgumentException If $other doesn't match the enumeration
0 ignored issues
show
introduced by
@throws tag comment must end with a full stop
Loading history...
525
     */
526 1
    public function withUnion(EnumSet $other): self
527
    {
528 1
        $clone = clone $this;
529 1
        $clone->setUnion($other);
530 1
        return $clone;
531
    }
532
533
    /**
534
     * Create a new set with enumerators common to both this and other (this & other)
535
     *
536
     * @param EnumSet $other EnumSet of the same enumeration to produce the intersect
1 ignored issue
show
introduced by
Parameter comment must end with a full stop
Loading history...
537
     * @return static
538
     * @throws InvalidArgumentException If $other doesn't match the enumeration
0 ignored issues
show
introduced by
@throws tag comment must end with a full stop
Loading history...
539
     */
540 1
    public function withIntersect(EnumSet $other): self
541
    {
542 1
        $clone = clone $this;
543 1
        $clone->setIntersect($other);
544 1
        return $clone;
545
    }
546
547
    /**
548
     * Modify this set with enumerators in this but not in other (this - other)
549
     *
550
     * @param EnumSet $other EnumSet of the same enumeration to produce the diff
1 ignored issue
show
introduced by
Parameter comment must end with a full stop
Loading history...
551
     * @return static
552
     * @throws InvalidArgumentException If $other doesn't match the enumeration
0 ignored issues
show
introduced by
@throws tag comment must end with a full stop
Loading history...
553
     */
554 1
    public function withDiff(EnumSet $other): self
555
    {
556 1
        $clone = clone $this;
557 1
        $clone->setDiff($other);
558 1
        return $clone;
559
    }
560
561
    /**
562
     * Create a new set with enumerators in either this and other but not in both (this ^ other)
563
     *
564
     * @param EnumSet $other EnumSet of the same enumeration to produce the symmetric difference
1 ignored issue
show
introduced by
Parameter comment must end with a full stop
Loading history...
565
     * @return static
566
     * @throws InvalidArgumentException If $other doesn't match the enumeration
0 ignored issues
show
introduced by
@throws tag comment must end with a full stop
Loading history...
567
     */
568 1
    public function withSymDiff(EnumSet $other): self
569
    {
570 1
        $clone = clone $this;
571 1
        $clone->setSymDiff($other);
572 1
        return $clone;
573
    }
574
575
    /**
576
     * Create a new set with the given binary bitset in little-endian order
577
     *
578
     * @param string $bitset
0 ignored issues
show
Documentation introduced by
Missing parameter comment
Loading history...
579
     * @return static
580
     * @throws InvalidArgumentException On out-of-range bits given as input bitset
0 ignored issues
show
introduced by
@throws tag comment must end with a full stop
Loading history...
581
     * @uses doSetBinaryBitsetLeBin()
582
     * @uses doSetBinaryBitsetLeInt()
583
     */
584 11
    public function withBinaryBitsetLe(string $bitset): self
585
    {
586 11
        $clone = clone $this;
587 11
        $clone->{$this->fnDoSetBinaryBitsetLe}($bitset);
588 5
        return $clone;
589
    }
590
591
    /**
592
     * Create a new set with the given binary bitset in big-endian order
593
     *
594
     * @param string $bitset
0 ignored issues
show
Documentation introduced by
Missing parameter comment
Loading history...
595
     * @return static
596
     * @throws InvalidArgumentException On out-of-range bits given as input bitset
0 ignored issues
show
introduced by
@throws tag comment must end with a full stop
Loading history...
597
     */
598 1
    public function withBinaryBitsetBe(string $bitset): self
599
    {
600 1
        $clone = $this;
601 1
        $clone->{$this->fnDoSetBinaryBitsetLe}(\strrev($bitset));
602 1
        return $clone;
603
    }
604
605
    /**
606
     * Create a new set with the bit at the given ordinal number set
607
     *
608
     * @param int $ordinal Ordinal number of bit to set
2 ignored issues
show
Coding Style introduced by
Expected "integer" but found "int" for parameter type
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
introduced by
Parameter comment must end with a full stop
Loading history...
609
     * @param bool $bit    The bit to set
2 ignored issues
show
Coding Style introduced by
Expected "boolean" but found "bool" for parameter type
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter name; 4 found
Loading history...
introduced by
Parameter comment must end with a full stop
Loading history...
610
     * @return static
611
     * @throws InvalidArgumentException If the given ordinal number is out-of-range
0 ignored issues
show
introduced by
@throws tag comment must end with a full stop
Loading history...
612
     * @uses doSetBitBin()
613
     * @uses doSetBitInt()
614
     * @uses doUnsetBitBin()
615
     * @uses doUnsetBitInt()
616
     */
617 1
    public function withBit(int $ordinal, bool $bit): self
618
    {
619 1
        $clone = clone $this;
620 1
        $clone->setBit($ordinal, $bit);
621 1
        return $clone;
622
    }
623
624
    /* read access */
625
626
    /**
627
     * Test if the given enumerator exists
628
     * @param Enum|null|bool|int|float|string|array $enumerator
1 ignored issue
show
Documentation introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected "Enum|null|boolean|integer|float|string|array" but found "Enum|null|bool|int|float|string|array" for parameter type
Loading history...
629
     * @return bool
1 ignored issue
show
Coding Style introduced by
Expected "boolean" but found "bool" for function return type
Loading history...
630
     */
631 24
    public function has($enumerator): bool
632
    {
633 24
        return $this->{$this->fnDoGetBit}(($this->enumeration)::get($enumerator)->getOrdinal());
634
    }
635
636
    /**
637
     * Test if the given enumerator exists
638
     * @param Enum|null|bool|int|float|string|array $enumerator
1 ignored issue
show
Documentation introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected "Enum|null|boolean|integer|float|string|array" but found "Enum|null|bool|int|float|string|array" for parameter type
Loading history...
639
     * @return bool
1 ignored issue
show
Coding Style introduced by
Expected "boolean" but found "bool" for function return type
Loading history...
640
     * @see has()
641
     * @deprecated Will trigger deprecation warning in last 4.x and removed in 5.x
642
     */
643 1
    public function contains($enumerator): bool
644
    {
645 1
        return $this->has($enumerator);
646
    }
647
648
    /* IteratorAggregate */
649
650
    /**
651
     * Get a new iterator
652
     * @return Iterator
653
     * @uses doGetIteratorInt()
654
     * @uses doGetIteratorBin()
655
     */
656 13
    public function getIterator(): Iterator
657
    {
658 13
        return $this->{$this->fnDoGetIterator}();
659
    }
660
661
    /**
662
     * Get a new Iterator.
663
     *
664
     * This is the binary bitset implementation.
665
     *
666
     * @return Iterator
667
     * @see getIterator()
668
     * @see goGetIteratorInt()
669
     */
670 4
    private function doGetIteratorBin()
671
    {
672 4
        $bitset   = $this->bitset;
673 4
        $byteLen  = \strlen($bitset);
674 4
        for ($bytePos = 0; $bytePos < $byteLen; ++$bytePos) {
675 4
            if ($bitset[$bytePos] === "\0") {
676
                // fast skip null byte
677 4
                continue;
678
            }
679
680 4
            $ord = \ord($bitset[$bytePos]);
681 4
            for ($bitPos = 0; $bitPos < 8; ++$bitPos) {
682 4
                if ($ord & (1 << $bitPos)) {
683 4
                    $ordinal = $bytePos * 8 + $bitPos;
684 4
                    yield $ordinal => ($this->enumeration)::byOrdinal($ordinal);
685
                }
686
            }
687
        }
688 4
    }
689
690
    /**
691
     * Get a new Iterator.
692
     *
693
     * This is the integer bitset implementation.
694
     *
695
     * @return Iterator
696
     * @see getIterator()
697
     * @see doGetIteratorBin()
698
     */
699 9
    private function doGetIteratorInt()
700
    {
701 9
        $count  = $this->enumerationCount;
702 9
        $bitset = $this->bitset;
703 9
        for ($ordinal = 0; $ordinal < $count; ++$ordinal) {
704 9
            if ($bitset & (1 << $ordinal)) {
705 7
                yield $ordinal => ($this->enumeration)::byOrdinal($ordinal);
706
            }
707
        }
708 7
    }
709
710
    /* Countable */
711
712
    /**
713
     * Count the number of elements
714
     *
715
     * @return int
1 ignored issue
show
Coding Style introduced by
Expected "integer" but found "int" for function return type
Loading history...
716
     * @uses doCountBin()
717
     * @uses doCountInt()
718
     */
719 32
    public function count(): int
720
    {
721 32
        return $this->{$this->fnDoCount}();
722
    }
723
724
    /**
725
     * Count the number of elements.
726
     *
727
     * This is the binary bitset implementation.
728
     *
729
     * @return int
1 ignored issue
show
Coding Style introduced by
Expected "integer" but found "int" for function return type
Loading history...
730
     * @see count()
731
     * @see doCountInt()
732
     */
733 15
    private function doCountBin()
734
    {
735 15
        $count   = 0;
736 15
        $bitset  = $this->bitset;
737 15
        $byteLen = \strlen($bitset);
738 15
        for ($bytePos = 0; $bytePos < $byteLen; ++$bytePos) {
739 15
            if ($bitset[$bytePos] === "\0") {
740
                // fast skip null byte
741 15
                continue;
742
            }
743
744 15
            $ord = \ord($bitset[$bytePos]);
745 15
            if ($ord & 0b00000001) ++$count;
1 ignored issue
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
746 15
            if ($ord & 0b00000010) ++$count;
1 ignored issue
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
747 15
            if ($ord & 0b00000100) ++$count;
1 ignored issue
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
748 15
            if ($ord & 0b00001000) ++$count;
1 ignored issue
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
749 15
            if ($ord & 0b00010000) ++$count;
1 ignored issue
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
750 15
            if ($ord & 0b00100000) ++$count;
1 ignored issue
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
751 15
            if ($ord & 0b01000000) ++$count;
1 ignored issue
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
752 15
            if ($ord & 0b10000000) ++$count;
1 ignored issue
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
753
        }
754 15
        return $count;
755
    }
756
757
    /**
758
     * Count the number of elements.
759
     *
760
     * This is the integer bitset implementation.
761
     *
762
     * @return int
1 ignored issue
show
Coding Style introduced by
Expected "integer" but found "int" for function return type
Loading history...
763
     * @see count()
764
     * @see doCountBin()
765
     */
766 17
    private function doCountInt()
767
    {
768 17
        $count  = 0;
769 17
        $bitset = $this->bitset;
770
771
        // PHP does not support right shift unsigned
772 17
        if ($bitset < 0) {
773 5
            $count  = 1;
774 5
            $bitset = $bitset & \PHP_INT_MAX;
775
        }
776
777
        // iterate byte by byte and count set bits
778 17
        $phpIntBitSize = \PHP_INT_SIZE * 8;
779 17
        for ($bitPos = 0; $bitPos < $phpIntBitSize; $bitPos += 8) {
780 17
            $bitChk = 0xff << $bitPos;
781 17
            $byte = $bitset & $bitChk;
782 17
            if ($byte) {
783 16
                $byte = $byte >> $bitPos;
784 16
                if ($byte & 0b00000001) ++$count;
1 ignored issue
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
785 16
                if ($byte & 0b00000010) ++$count;
1 ignored issue
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
786 16
                if ($byte & 0b00000100) ++$count;
1 ignored issue
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
787 16
                if ($byte & 0b00001000) ++$count;
1 ignored issue
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
788 16
                if ($byte & 0b00010000) ++$count;
1 ignored issue
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
789 16
                if ($byte & 0b00100000) ++$count;
1 ignored issue
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
790 16
                if ($byte & 0b01000000) ++$count;
1 ignored issue
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
791 16
                if ($byte & 0b10000000) ++$count;
1 ignored issue
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
792
            }
793
794 17
            if ($bitset <= $bitChk) {
795 17
                break;
796
            }
797
        }
798
799 17
        return $count;
800
    }
801
802
    /**
803
     * Check if this EnumSet is the same as other
804
     * @param EnumSet $other
0 ignored issues
show
Documentation introduced by
Missing parameter comment
Loading history...
805
     * @return bool
1 ignored issue
show
Coding Style introduced by
Expected "boolean" but found "bool" for function return type
Loading history...
806
     */
807 3
    public function isEqual(EnumSet $other): bool
808
    {
809 3
        return $this->enumeration === $other->enumeration
810 3
            && $this->bitset === $other->bitset;
811
    }
812
813
    /**
814
     * Check if this EnumSet is a subset of other
815
     * @param EnumSet $other
0 ignored issues
show
Documentation introduced by
Missing parameter comment
Loading history...
816
     * @return bool
1 ignored issue
show
Coding Style introduced by
Expected "boolean" but found "bool" for function return type
Loading history...
817
     */
818 4
    public function isSubset(EnumSet $other): bool
819
    {
820 4
        return $this->enumeration === $other->enumeration
821 4
            && ($this->bitset & $other->bitset) === $this->bitset;
822
    }
823
824
    /**
825
     * Check if this EnumSet is a superset of other
826
     * @param EnumSet $other
0 ignored issues
show
Documentation introduced by
Missing parameter comment
Loading history...
827
     * @return bool
1 ignored issue
show
Coding Style introduced by
Expected "boolean" but found "bool" for function return type
Loading history...
828
     */
829 4
    public function isSuperset(EnumSet $other): bool
830
    {
831 4
        return $this->enumeration === $other->enumeration
832 4
            && ($this->bitset | $other->bitset) === $this->bitset;
833
    }
834
835
    /**
836
     * Get ordinal numbers of the defined enumerators as array
837
     * @return int[]
838
     * @uses  doGetOrdinalsBin()
839
     * @uses  doGetOrdinalsInt()
840
     */
841 14
    public function getOrdinals(): array
842
    {
843 14
        return $this->{$this->fnDoGetOrdinals}();
844
    }
845
846
    /**
847
     * Get ordinal numbers of the defined enumerators as array.
848
     *
849
     * This is the binary bitset implementation.
850
     *
851
     * @return int[]
852
     * @see getOrdinals()
853
     * @see goGetOrdinalsInt()
854
     */
855 1
    private function doGetOrdinalsBin()
856
    {
857 1
        $ordinals = [];
858 1
        $bitset   = $this->bitset;
859 1
        $byteLen  = \strlen($bitset);
860 1
        for ($bytePos = 0; $bytePos < $byteLen; ++$bytePos) {
861 1
            if ($bitset[$bytePos] === "\0") {
862
                // fast skip null byte
863 1
                continue;
864
            }
865
866 1
            $ord = \ord($bitset[$bytePos]);
867 1
            for ($bitPos = 0; $bitPos < 8; ++$bitPos) {
868 1
                if ($ord & (1 << $bitPos)) {
869 1
                    $ordinals[] = $bytePos * 8 + $bitPos;
870
                }
871
            }
872
        }
873 1
        return $ordinals;
874
    }
875
876
    /**
877
     * Get ordinal numbers of the defined enumerators as array.
878
     *
879
     * This is the integer bitset implementation.
880
     *
881
     * @return int[]
882
     * @see getOrdinals()
883
     * @see doGetOrdinalsBin()
884
     */
885 13
    private function doGetOrdinalsInt()
886
    {
887 13
        $ordinals = [];
888 13
        $count    = $this->enumerationCount;
889 13
        $bitset   = $this->bitset;
890 13
        for ($ordinal = 0; $ordinal < $count; ++$ordinal) {
891 13
            if ($bitset & (1 << $ordinal)) {
892 13
                $ordinals[] = $ordinal;
893
            }
894
        }
895 13
        return $ordinals;
896
    }
897
898
    /**
899
     * Get values of the defined enumerators as array
900
     * @return mixed[]
901
     */
902 9
    public function getValues(): array
903
    {
904 9
        $enumeration = $this->enumeration;
905 9
        $values      = [];
906 9
        foreach ($this->getOrdinals() as $ord) {
907 9
            $values[] = $enumeration::byOrdinal($ord)->getValue();
908
        }
909 9
        return $values;
910
    }
911
912
    /**
913
     * Get names of the defined enumerators as array
914
     * @return string[]
915
     */
916 1
    public function getNames(): array
917
    {
918 1
        $enumeration = $this->enumeration;
919 1
        $names       = [];
920 1
        foreach ($this->getOrdinals() as $ord) {
921 1
            $names[] = $enumeration::byOrdinal($ord)->getName();
922
        }
923 1
        return $names;
924
    }
925
926
    /**
927
     * Get the defined enumerators as array
928
     * @return Enum[]
929
     */
930 2
    public function getEnumerators(): array
931
    {
932 2
        $enumeration = $this->enumeration;
933 2
        $enumerators = [];
934 2
        foreach ($this->getOrdinals() as $ord) {
935 2
            $enumerators[] = $enumeration::byOrdinal($ord);
936
        }
937 2
        return $enumerators;
938
    }
939
940
    /**
941
     * Get binary bitset in little-endian order
942
     * 
943
     * @return string
944
     * @uses doGetBinaryBitsetLeBin()
945
     * @uses doGetBinaryBitsetLeInt()
946
     */
947 6
    public function getBinaryBitsetLe(): string
948
    {
949 6
        return $this->{$this->fnDoGetBinaryBitsetLe}();
950
    }
951
952
    /**
953
     * Get binary bitset in little-endian order.
954
     *
955
     * This is the binary bitset implementation.
956
     *
957
     * @return string
958
     * @see getBinaryBitsetLe()
959
     * @see doGetBinaryBitsetLeInt()
960
     */
961 4
    private function doGetBinaryBitsetLeBin()
962
    {
963 4
        return $this->bitset;
964
    }
965
966
    /**
967
     * Get binary bitset in little-endian order.
968
     *
969
     * This is the integer bitset implementation.
970
     *
971
     * @return string
972
     * @see getBinaryBitsetLe()
973
     * @see doGetBinaryBitsetLeBin()
974
     */
975 2
    private function doGetBinaryBitsetLeInt()
976
    {
977 2
        $bin = \pack(\PHP_INT_SIZE === 8 ? 'P' : 'V', $this->bitset);
978 2
        return \substr($bin, 0, (int)\ceil($this->enumerationCount / 8));
979
    }
980
981
    /**
982
     * Get binary bitset in big-endian order
983
     * 
984
     * @return string
985
     */
986 1
    public function getBinaryBitsetBe(): string
987
    {
988 1
        return \strrev($this->bitset);
989
    }
990
991
    /**
992
     * Get a bit at the given ordinal number
993
     *
994
     * @param int $ordinal Ordinal number of bit to get
2 ignored issues
show
Coding Style introduced by
Expected "integer" but found "int" for parameter type
Loading history...
introduced by
Parameter comment must end with a full stop
Loading history...
995
     * @return bool
1 ignored issue
show
Coding Style introduced by
Expected "boolean" but found "bool" for function return type
Loading history...
996
     * @throws InvalidArgumentException If the given ordinal number is out-of-range
0 ignored issues
show
introduced by
@throws tag comment must end with a full stop
Loading history...
997
     * @uses doGetBitBin()
998
     * @uses doGetBitInt()
999
     */
1000 4
    public function getBit(int $ordinal): bool
1001
    {
1002 4
        if ($ordinal < 0 || $ordinal > $this->enumerationCount) {
1003 1
            throw new InvalidArgumentException("Ordinal number must be between 0 and {$this->enumerationCount}");
1 ignored issue
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $this instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 113 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
1004
        }
1005
1006 3
        return $this->{$this->fnDoGetBit}($ordinal);
1007
    }
1008
1009
    /**
1010
     * Get a bit at the given ordinal number.
1011
     *
1012
     * This is the binary bitset implementation.
1013
     *
1014
     * @param int $ordinal Ordinal number of bit to get
2 ignored issues
show
Coding Style introduced by
Expected "integer" but found "int" for parameter type
Loading history...
introduced by
Parameter comment must end with a full stop
Loading history...
1015
     * @return bool
1 ignored issue
show
Coding Style introduced by
Expected "boolean" but found "bool" for function return type
Loading history...
1016
     * @see getBit()
1017
     * @see doGetBitInt()
1018
     */
1019 8
    private function doGetBitBin($ordinal)
0 ignored issues
show
Coding Style introduced by
Type hint "int" missing for $ordinal
Loading history...
1020
    {
1021 8
        return (\ord($this->bitset[(int) ($ordinal / 8)]) & 1 << ($ordinal % 8)) !== 0;
1022
    }
1023
1024
    /**
1025
     * Get a bit at the given ordinal number.
1026
     *
1027
     * This is the integer bitset implementation.
1028
     * 
1029
     * @param int $ordinal Ordinal number of bit to get
2 ignored issues
show
Coding Style introduced by
Expected "integer" but found "int" for parameter type
Loading history...
introduced by
Parameter comment must end with a full stop
Loading history...
1030
     * @return bool
1 ignored issue
show
Coding Style introduced by
Expected "boolean" but found "bool" for function return type
Loading history...
1031
     * @see getBit()
1032
     * @see doGetBitBin()
1033
     */
1034 18
    private function doGetBitInt($ordinal)
0 ignored issues
show
Coding Style introduced by
Type hint "int" missing for $ordinal
Loading history...
1035
    {
1036 18
        return (bool)($this->bitset & (1 << $ordinal));
1037
    }
1038
}
1039