Passed
Pull Request — master (#63)
by Eugene
02:59
created

DecimalExtension::getType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.037

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
ccs 2
cts 3
cp 0.6667
crap 1.037
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 13
    public function getType() : int
27
    {
28 13
        return self::TYPE;
29
    }
30
31 13
    public function pack(Packer $packer, $value) : ?string
32
    {
33 13
        if (!$value instanceof Decimal) {
34
            return null;
35
        }
36
37
        // @see https://github.com/php-decimal/ext-decimal/issues/22#issuecomment-512364914
38 13
        $data = $value->toFixed(self::PRECISION);
39
40 13
        if ('-' === $data[0]) {
41 2
            $nibble = 'd';
42 2
            $data = \substr($data, 1);
43
        } else {
44 11
            $nibble = 'c';
45
        }
46
47 13
        if ($scale = \strpos($data, '.')) {
48 13
            $data = \substr_replace($data, '', $scale, 1);
49
        }
50
51 13
        $data = (0 === \strlen($data) % 2) ? "0{$data}{$nibble}" : "{$data}{$nibble}";
52
53 13
        return $packer->packExt(self::TYPE,
54 13
            $packer->packInt($scale).\hex2bin($data)
55
        );
56
    }
57
58 13
    public function unpackExt(BufferUnpacker $unpacker, int $extLength)
59
    {
60 13
        $scale = $unpacker->unpackInt();
61 13
        $data = $unpacker->read($extLength - 1);
62 13
        $data = \bin2hex($data);
63
64 13
        $sign = 'd' === $data[-1] ? '-' : '';
65 13
        $dec = \substr($data, 0, -1);
66
67 13
        if (0 !== $scale) {
68 9
            $length = \strlen($dec);
69 9
            $dec = ($length <= $scale)
70 4
                ? \substr_replace($dec, '0.'.\str_repeat('0', $scale - $length), -$scale, 0)
71 9
                : \substr_replace($dec, '.', -$scale, 0);
72
        }
73
74 13
        return new Decimal($sign.$dec, self::PRECISION);
75
    }
76
}
77