Completed
Push — master ( 0770f8...218e56 )
by Ryosuke
21:28
created

CoOption::offsetSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
namespace mpyw\Co\Internal;
4
5
class CoOption implements \ArrayAccess
6
{
7
    /**
8
     * Field types.
9
     */
10
    const TYPES = [
11
        'throw' => 'Bool', // Throw CURLExceptions?
12
        'pipeline' => 'Bool', // Use HTTP/1.1 pipelining?
13
        'multiplex' => 'Bool', // Use HTTP/2 multiplexing?
14
        'interval' => 'NaturalFloat', // curl_multi_select() timeout
15
        'concurrency' => 'NaturalInt', // Limit of TCP connections
16
    ];
17
18
    /**
19
     * Default values.
20
     * @var array
21
     */
22
    private static $defaults = [
23
        'throw' => true,
24
        'pipeline' => false,
25
        'multiplex' => true,
26
        'interval' => 0.5,
27
        'concurrency' => 6,
28
    ];
29
30
    /**
31
     * Actual values.
32
     * @var array
33
     */
34
    private $options;
35
36
    /**
37
     * Set default options.
38
     * @param array $options
39
     */
40 7
    public static function setDefault(array $options)
41 7
    {
42 7
        self::$defaults = self::validateOptions($options) + self::$defaults;
43 7
    }
44
45
    /**
46
     * Get default options.
47
     * @param array $options
0 ignored issues
show
Bug introduced by
There is no parameter named $options. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
48
     */
49 7
    public static function getDefault()
50 7
    {
51 7
        return self::$defaults;
52
    }
53
54
    /**
55
     * Constructor.
56
     * @param array $options
57
     */
58 18
    public function __construct(array $options = [])
59 18
    {
60 18
        $this->options = self::validateOptions($options) + self::$defaults;
61 18
    }
62
63
    /**
64
     * Reconfigure to get new instance.
65
     * @return CoOption
66
     */
67 3
    public function reconfigure(array $options)
68 3
    {
69 3
        return new self($options + $this->options);
70
    }
71
72
    /**
73
     * Implemention of ArrayAccess.
74
     * @param  mixed $offset
75
     * @return bool
76
     */
77 1
    public function offsetExists($offset)
78 1
    {
79 1
        return isset($this->options[$offset]);
80
    }
81
82
    /**
83
     * Implemention of ArrayAccess.
84
     * @param  mixed $offset
85
     * @return mixed
86
     */
87 9
    public function offsetGet($offset)
88 9
    {
89 9
        if (!isset($this->options[$offset])) {
90 1
            throw new \DomainException('Undefined field: ' + $offset);
91
        }
92 8
        return $this->options[$offset];
93
    }
94
95
    /**
96
     * Implemention of ArrayAccess.
97
     * @param  mixed $offset
98
     * @param  mixed $value
99
     * @throws BadMethodCallException
100
     */
101 1
    public function offsetSet($offset, $value)
102 1
    {
103 1
        throw new \BadMethodCallException('The instance of CoOptions is immutable.');
104
    }
105
106
    /**
107
     * Implemention of ArrayAccess.
108
     * @param  mixed $offset
109
     * @throws BadMethodCallException
110
     */
111 1
    public function offsetUnset($offset)
112 1
    {
113 1
        throw new \BadMethodCallException('The instance of CoOptions is immutable.');
114
    }
115
116
    /**
117
     * Validate options.
118
     * @param  array $options
119
     * @return array
120
     */
121 22
    private static function validateOptions(array $options)
122 22
    {
123 22
        foreach ($options as $key => $value) {
124 11
            if (!isset(self::TYPES[$key])) {
125 1
                throw new \InvalidArgumentException("Unknown option: $key");
126
            }
127 11
            $validator = [__CLASS__, 'validate' . self::TYPES[$key]];
128 11
            $options[$key] = $validator($key, $value);
129
        }
130 22
        return $options;
131
    }
132
133
    /**
134
     * Validate bool value.
135
     * @param  string $key
136
     * @param  mixed  $value
137
     * @throws InvalidArgumentException
138
     * @return bool
139
     */
140 11
    private static function validateBool($key, $value)
1 ignored issue
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
141 11
    {
142 11
        $value = filter_var($value, FILTER_VALIDATE_BOOLEAN, [
143 11
            'flags' => FILTER_NULL_ON_FAILURE,
144
        ]);
145 11
        if ($value === null) {
146
            throw new \InvalidArgumentException("Option[$key] must be boolean.");
147
        }
148 11
        return $value;
149
    }
150
151
    /**
152
     * Validate natural float value.
153
     * @param  string $key
154
     * @param  mixed  $value
155
     * @throws InvalidArgumentException
156
     * @return float
157
     */
158 9 View Code Duplication
    private static function validateNaturalFloat($key, $value)
2 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
Duplication introduced by
This method seems to be duplicated in 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...
159 9
    {
160 9
        $value = filter_var($value, FILTER_VALIDATE_FLOAT);
161 9
        if ($value === false || $value < 0.0) {
162 1
            throw new \InvalidArgumentException("Option[$key] must be positive float or zero.");
163
        }
164 9
        return $value;
165
    }
166
167
    /**
168
     * Validate natural int value.
169
     * @param  string $key
170
     * @param  mixed  $value
171
     * @throws InvalidArgumentException
172
     * @return int
173
     */
174 9 View Code Duplication
    private static function validateNaturalInt($key, $value)
2 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
Duplication introduced by
This method seems to be duplicated in 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...
175 9
    {
176 9
        $value = filter_var($value, FILTER_VALIDATE_INT);
177 9
        if ($value === false || $value < 0) {
178 1
            throw new \InvalidArgumentException("Option[$key] must be positive integer or zero.");
179
        }
180 9
        return $value;
181
    }
182
}
183