Passed
Push — master ( 7cf5b4...231b8d )
by Eugene
05:38
created

PacketLength   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Test Coverage

Coverage 81.82%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 23
ccs 9
cts 11
cp 0.8182
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A pack() 0 3 1
A __construct() 0 2 1
A unpack() 0 10 3
1
<?php
2
3
/**
4
 * This file is part of the Tarantool Client 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
declare(strict_types=1);
13
14
namespace Tarantool\Client\Packer;
15
16
final class PacketLength
17
{
18
    public const SIZE_BYTES = 5;
19
20
    private function __construct()
21
    {
22
    }
23
24 262
    public static function pack(int $length) : string
25
    {
26 262
        return \pack('CN', 0xce, $length);
27
    }
28
29 228
    public static function unpack(string $data) : int
30
    {
31 228
        if (!isset($data[4]) || "\xce" !== $data[0]) {
32 2
            throw new \RuntimeException('Unable to unpack packet length.');
33
        }
34
35 226
        return \ord($data[1]) << 24
36 226
            | \ord($data[2]) << 16
37 226
            | \ord($data[3]) << 8
38 226
            | \ord($data[4]);
39
    }
40
}
41