Passed
Push — master ( 7df6d1...07ff3c )
by Eugene
05:26
created

DecimalExtension::unpackExt()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4.0466

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 11
c 1
b 0
f 0
nc 6
nop 2
dl 0
loc 17
rs 9.9
ccs 12
cts 14
cp 0.8571
crap 4.0466
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\Extension;
15
16
use Decimal\Decimal;
0 ignored issues
show
Bug introduced by
The type Decimal\Decimal was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use MessagePack\BufferUnpacker;
18
use MessagePack\Packer;
19
use MessagePack\TypeTransformer\Extension;
20
21
class DecimalExtension implements Extension
22
{
23
    private const TYPE = 1;
24
    private const PRECISION = 38;
25
26 17
    public function getType() : int
27
    {
28 17
        return self::TYPE;
29
    }
30
31 16
    public function pack(Packer $packer, $value) : ?string
32
    {
33 16
        if (!$value instanceof Decimal) {
34
            return null;
35
        }
36
37
        // @see https://github.com/php-decimal/ext-decimal/issues/22#issuecomment-512364914
38 16
        $data = $value->toFixed(self::PRECISION);
39
40 16
        if ('-' === $data[0]) {
41 3
            $nibble = 'd';
42 3
            $data = \substr($data, 1);
43
        } else {
44 13
            $nibble = 'c';
45
        }
46
47 16
        $pieces = \explode('.', $data, 2);
48 16
        $pieces[1] = \rtrim($pieces[1], '0');
49
50 16
        $data = "{$pieces[0]}{$pieces[1]}{$nibble}";
51 16
        if (0 !== \strlen($data) % 2) {
52 9
            $data = '0'.$data;
53
        }
54
55 16
        return $packer->packExt(self::TYPE,
56 16
            $packer->packInt(empty($pieces[1]) ? 0 : \strlen($pieces[1])).\hex2bin($data)
57
        );
58
    }
59
60 16
    public function unpackExt(BufferUnpacker $unpacker, int $extLength)
61
    {
62 16
        $scale = $unpacker->unpackInt();
63 16
        $data = $unpacker->read($extLength - 1);
64 16
        $data = \bin2hex($data);
65
66 16
        $sign = 'd' === $data[-1] ? '-' : '';
67 16
        $dec = \substr($data, 0, -1);
68
69 16
        if (0 !== $scale) {
70 11
            $length = \strlen($dec);
71 11
            $dec = ($length <= $scale)
72 5
                ? \substr_replace($dec, '0.'.\str_repeat('0', $scale - $length), -$scale, 0)
73 11
                : \substr_replace($dec, '.', -$scale, 0);
74
        }
75
76 16
        return new Decimal($sign.$dec, self::PRECISION);
77
    }
78
}
79