Completed
Pull Request — master (#20)
by Eugene
06:27
created

UnpackOptions::fromBitmask()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 2
eloc 8
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the rybakit/msgpack.php package.
5
 *
6
 * (c) Eugene Leonovich <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace MessagePack;
13
14
use MessagePack\Exception\InvalidOptionException;
15
16
final class UnpackOptions
17
{
18
    const BIGINT_AS_STR       = 0b001;
19
    const BIGINT_AS_GMP       = 0b010;
20
    const BIGINT_AS_EXCEPTION = 0b100;
21
22
    private $bigIntMode;
23
24
    /**
25
     * @codeCoverageIgnore
26
     */
27
    private function __construct()
28
    {
29
    }
30
31
    public static function fromBitmask($options)
32
    {
33
        $self = new self();
34
35
        $self->bigIntMode = self::getSingleOption('bigint', $options,
36
            self::BIGINT_AS_STR |
37
            self::BIGINT_AS_GMP |
38
            self::BIGINT_AS_EXCEPTION
39
        ) ?: self::BIGINT_AS_STR;
40
41
        return $self;
42
    }
43
44
    public function isBigIntAsStrMode()
45
    {
46
        return self::BIGINT_AS_STR === $this->bigIntMode;
47
    }
48
49
    public function isBigIntAsGmpMode()
50
    {
51
        return self::BIGINT_AS_GMP === $this->bigIntMode;
52
    }
53
54
    private static function getSingleOption($name, $options, $mask)
55
    {
56
        $option = $options & $mask;
57
        if ($option === ($option & -$option)) {
58
            return $option;
59
        }
60
61
        static $map = [
62
            self::BIGINT_AS_STR => 'BIGINT_AS_STR',
63
            self::BIGINT_AS_GMP => 'BIGINT_AS_GMP',
64
            self::BIGINT_AS_EXCEPTION => 'BIGINT_AS_EXCEPTION',
65
        ];
66
67
        $validOptions = [];
68 View Code Duplication
        for ($i = $mask & -$mask; $i <= $mask; $i <<= 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
            $validOptions[] = __CLASS__.'::'.$map[$i];
70
        }
71
72
        throw InvalidOptionException::fromValidOptions($name, $validOptions);
73
    }
74
}
75