Completed
Push — master ( a0d5af...253710 )
by Eugene
04:42
created

Packer::packMap()   D

Complexity

Conditions 10
Paths 11

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 10

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 18
cts 18
cp 1
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 18
nc 11
nop 1
crap 10

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
use MessagePack\Exception\PackingFailedException;
16
use MessagePack\TypeTransformer\Packable;
17
18
class Packer
19
{
20
    private const UTF8_REGEX = '/\A(?:
21
          [\x00-\x7F]++                      # ASCII
22
        | [\xC2-\xDF][\x80-\xBF]             # non-overlong 2-byte
23
        |  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
24
        | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
25
        |  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
26
        |  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
27
        | [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
28
        |  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
29
        )*+\z/x';
30
31
    private $isDetectStrBin;
32
    private $isForceStr;
33
    private $isDetectArrMap;
34
    private $isForceArr;
35
    private $isForceFloat32;
36
37
    /**
38
     * @var Packable[]|null
39
     */
40
    private $transformers;
41
42
    /**
43
     * @param PackOptions|int|null $options
44
     *
45
     * @throws InvalidOptionException
46
     */
47 182
    public function __construct($options = null)
48
    {
49 182
        if (null === $options) {
50 181
            $options = PackOptions::fromDefaults();
51 20
        } elseif (!$options instanceof PackOptions) {
52 20
            $options = PackOptions::fromBitmask($options);
53
        }
54
55 182
        $this->isDetectStrBin = $options->isDetectStrBinMode();
56 182
        $this->isForceStr = $options->isForceStrMode();
57 182
        $this->isDetectArrMap = $options->isDetectArrMapMode();
58 182
        $this->isForceArr = $options->isForceArrMode();
59 182
        $this->isForceFloat32 = $options->isForceFloat32Mode();
60 182
    }
61
62 2
    public function registerTransformer(Packable $transformer) : self
63
    {
64 2
        $this->transformers[] = $transformer;
65
66 2
        return $this;
67
    }
68
69 114
    public function pack($value)
70
    {
71 114
        if (\is_int($value)) {
72 63
            return $this->packInt($value);
73
        }
74 74
        if (\is_string($value)) {
75 35
            if ($this->isForceStr) {
76 3
                return $this->packStr($value);
77
            }
78 32
            if ($this->isDetectStrBin) {
79 29
                return \preg_match(self::UTF8_REGEX, $value)
80 22
                    ? $this->packStr($value)
81 29
                    : $this->packBin($value);
82
            }
83
84 3
            return $this->packBin($value);
85
        }
86 51
        if (\is_array($value)) {
87 28
            if ($this->isDetectArrMap) {
88 21
                return \array_values($value) === $value
89 12
                    ? $this->packArray($value)
90 21
                    : $this->packMap($value);
91
            }
92
93 7
            return $this->isForceArr ? $this->packArray($value) : $this->packMap($value);
94
        }
95 29
        if (null === $value) {
96 5
            return "\xc0";
97
        }
98 28
        if (\is_bool($value)) {
99 10
            return $value ? "\xc3" : "\xc2";
100
        }
101 20
        if (\is_float($value)) {
102 6
            return $this->packFloat($value);
103
        }
104 14
        if ($value instanceof Ext) {
105 10
            return $this->packExt($value->type, $value->data);
106
        }
107 4
        if ($this->transformers) {
108 2
            foreach ($this->transformers as $transformer) {
109 2
                if (null !== $packed = $transformer->pack($this, $value)) {
110 2
                    return $packed;
111
                }
112
            }
113
        }
114
115 3
        throw PackingFailedException::unsupportedType($value);
116
    }
117
118 1
    public function packNil()
119
    {
120 1
        return "\xc0";
121
    }
122
123 2
    public function packBool($bool)
124
    {
125 2
        return $bool ? "\xc3" : "\xc2";
126
    }
127
128 98
    public function packInt($int)
129
    {
130 98
        if ($int >= 0) {
131 66
            if ($int <= 0x7f) {
132 40
                return \chr($int);
133
            }
134 34
            if ($int <= 0xff) {
135 12
                return "\xcc".\chr($int);
136
            }
137 26
            if ($int <= 0xffff) {
138 12
                return "\xcd".\chr($int >> 8).\chr($int);
139
            }
140 16
            if ($int <= 0xffffffff) {
141 10
                return \pack('CN', 0xce, $int);
142
            }
143
144 8
            return \pack('CJ', 0xcf, $int);
145
        }
146
147 34
        if ($int >= -0x20) {
148 8
            return \chr(0xe0 | $int);
149
        }
150 26
        if ($int >= -0x80) {
151 6
            return "\xd0".\chr($int);
152
        }
153 20
        if ($int >= -0x8000) {
154 6
            return "\xd1".\chr($int >> 8).\chr($int);
155
        }
156 14
        if ($int >= -0x80000000) {
157 6
            return \pack('CN', 0xd2, $int);
158
        }
159
160 8
        return \pack('CJ', 0xd3, $int);
161
    }
162
163 9
    public function packFloat($float)
164
    {
165 9
        return $this->isForceFloat32
166 1
            ? "\xca".\pack('G', $float)
167 9
            : "\xcb".\pack('E', $float);
168
    }
169
170 39
    public function packStr($str)
171
    {
172 39
        $length = \strlen($str);
173
174 39
        if ($length < 32) {
175 25
            return \chr(0xa0 | $length).$str;
176
        }
177 14
        if ($length <= 0xff) {
178 8
            return "\xd9".\chr($length).$str;
179
        }
180 6
        if ($length <= 0xffff) {
181 4
            return "\xda".\chr($length >> 8).\chr($length).$str;
182
        }
183
184 2
        return \pack('CN', 0xdb, $length).$str;
185
    }
186
187 21
    public function packBin($str)
188
    {
189 21
        $length = \strlen($str);
190
191 21
        if ($length <= 0xff) {
192 17
            return "\xc4".\chr($length).$str;
193
        }
194 4
        if ($length <= 0xffff) {
195 2
            return "\xc5".\chr($length >> 8).\chr($length).$str;
196
        }
197
198 2
        return \pack('CN', 0xc6, $length).$str;
199
    }
200
201 20
    public function packArray(array $array)
202
    {
203 20
        $data = $this->packArrayHeader(\count($array));
204
205 20
        foreach ($array as $val) {
206 18
            $data .= $this->pack($val);
207
        }
208
209 20
        return $data;
210
    }
211
212 20
    public function packArrayHeader($size)
213
    {
214 20
        if ($size <= 0xf) {
215 14
            return \chr(0x90 | $size);
216
        }
217 6
        if ($size <= 0xffff) {
218 4
            return "\xdc".\chr($size >> 8).\chr($size);
219
        }
220
221 2
        return \pack('CN', 0xdd, $size);
222
    }
223
224 24
    public function packMap(array $map)
225
    {
226 24
        $data = $this->packMapHeader(\count($map));
227
228 24
        if ($this->isForceStr) {
229 1
            foreach ($map as $key => $val) {
230 1
                $data .= \is_string($key) ? $this->packStr($key) : $this->packInt($key);
231 1
                $data .= $this->pack($val);
232
            }
233
234 1
            return $data;
235
        }
236
237 23
        if ($this->isDetectStrBin) {
238 22
            foreach ($map as $key => $val) {
239 22
                $data .= \is_string($key)
240 4
                    ? (\preg_match(self::UTF8_REGEX, $key) ? $this->packStr($key) : $this->packBin($key))
241 22
                    : $this->packInt($key);
242 22
                $data .= $this->pack($val);
243
            }
244
245 22
            return $data;
246
        }
247
248 1
        foreach ($map as $key => $val) {
249 1
            $data .= \is_string($key) ? $this->packBin($key) : $this->packInt($key);
250 1
            $data .= $this->pack($val);
251
        }
252
253 1
        return $data;
254
    }
255
256 24
    public function packMapHeader($size)
257
    {
258 24
        if ($size <= 0xf) {
259 18
            return \chr(0x80 | $size);
260
        }
261 6
        if ($size <= 0xffff) {
262 4
            return "\xde".\chr($size >> 8).\chr($size);
263
        }
264
265 2
        return \pack('CN', 0xdf, $size);
266
    }
267
268 18
    public function packExt($type, $data)
269
    {
270 18
        $length = \strlen($data);
271
272 18
        switch ($length) {
273 18
            case 1: return "\xd4".\chr($type).$data;
274 16
            case 2: return "\xd5".\chr($type).$data;
275 14
            case 4: return "\xd6".\chr($type).$data;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
276 12
            case 8: return "\xd7".\chr($type).$data;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
277 10
            case 16: return "\xd8".\chr($type).$data;
278
        }
279
280 8
        if ($length <= 0xff) {
281 4
            return "\xc7".\chr($length).\chr($type).$data;
282
        }
283 4
        if ($length <= 0xffff) {
284 2
            return \pack('CnC', 0xc8, $length, $type).$data;
285
        }
286
287 2
        return \pack('CNC', 0xc9, $length, $type).$data;
288
    }
289
}
290