Code Duplication    Length = 36-36 lines in 2 locations

src/AbstractEnumSet.php 1 location

@@ 413-448 (lines=36) @@
410
     * @return void
411
     * @throws InvalidArgumentException On a non string is given as Parameter
412
     */
413
    public function setBinaryBitsetLe($bitset)
414
    {
415
        if (!is_string($bitset)) {
416
            throw new InvalidArgumentException('Bitset must be a string');
417
        }
418
419
        $size   = strlen($this->bitset);
420
        $sizeIn = strlen($bitset);
421
422
        if ($sizeIn < $size) {
423
            // add "\0" if the given bitset is not long enough
424
            $bitset .= str_repeat("\0", $size - $sizeIn);
425
        } elseif ($sizeIn > $size) {
426
            if (trim(substr($bitset, $size), "\0") !== '') {
427
                throw new InvalidArgumentException('Out-Of-Range bits detected');
428
            }
429
            $bitset = substr($bitset, 0, $size);
430
        }
431
432
        // truncate out-of-range bits of last byte
433
        $lastByteMaxOrd = $this->ordinalMax % 8;
434
        if ($lastByteMaxOrd !== 0) {
435
            $lastByte         = $bitset[$size - 1];
436
            $lastByteExpected = chr((1 << $lastByteMaxOrd) - 1) & $lastByte;
437
            if ($lastByte !== $lastByteExpected) {
438
                throw new InvalidArgumentException('Out-Of-Range bits detected');
439
            }
440
441
            $this->bitset = substr($bitset, 0, -1) . $lastByteExpected;
442
        }
443
444
        $this->bitset = $bitset;
445
446
        // reset the iterator position
447
        $this->rewind();
448
    }
449
450
    /**
451
     * Get binary bitset in big-endian order

src/BinaryEnumSet.php 1 location

@@ 395-430 (lines=36) @@
392
     * @return void
393
     * @throws InvalidArgumentException On a non string is given as Parameter
394
     */
395
    public function setBinaryBitsetLe($bitset)
396
    {
397
        if (!is_string($bitset)) {
398
            throw new InvalidArgumentException('Bitset must be a string');
399
        }
400
401
        $size   = strlen($this->bitset);
402
        $sizeIn = strlen($bitset);
403
404
        if ($sizeIn < $size) {
405
            // add "\0" if the given bitset is not long enough
406
            $bitset .= str_repeat("\0", $size - $sizeIn);
407
        } elseif ($sizeIn > $size) {
408
            if (trim(substr($bitset, $size), "\0") !== '') {
409
                throw new InvalidArgumentException('Out-Of-Range bits detected');
410
            }
411
            $bitset = substr($bitset, 0, $size);
412
        }
413
414
        // truncate out-of-range bits of last byte
415
        $lastByteMaxOrd = $this->ordinalMax % 8;
416
        if ($lastByteMaxOrd !== 0) {
417
            $lastByte         = $bitset[$size - 1];
418
            $lastByteExpected = chr((1 << $lastByteMaxOrd) - 1) & $lastByte;
419
            if ($lastByte !== $lastByteExpected) {
420
                throw new InvalidArgumentException('Out-Of-Range bits detected');
421
            }
422
423
            $this->bitset = substr($bitset, 0, -1) . $lastByteExpected;
424
        }
425
426
        $this->bitset = $bitset;
427
428
        // reset the iterator position
429
        $this->rewind();
430
    }
431
432
    /**
433
     * Get binary bitset in big-endian order