Passed
Push — master ( c493bc...401b4d )
by Nikita
03:14 queued 11s
created

SimpleStorageValueDecoder::decode()   C

Complexity

Conditions 16
Paths 16

Size

Total Lines 70
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 42
c 1
b 0
f 0
dl 0
loc 70
rs 5.5666
cc 16
nc 16
nop 1

How to fix   Long Method    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
namespace Knik\Binn\Decoder;
4
5
use Knik\Binn\Binn;
6
use Knik\Binn\Contracts\BinnValueDecoder;
7
8
class SimpleStorageValueDecoder extends Decoder implements BinnValueDecoder
9
{
10
    public function decode(string $bytes)
11
    {
12
        $type = $this->detectType($bytes);
13
14
        switch ($type) {
15
            case Binn::BINN_NULL:
16
                return null;
17
18
            case Binn::BINN_TRUE:
19
                return true;
20
21
            case Binn::BINN_FALSE:
22
                return false;
23
24
            case Binn::BINN_FLOAT32:
25
                return Unpacker::unpackFloat32(
26
                    substr($bytes, 1, 4)
27
                );
28
29
            case Binn::BINN_FLOAT64:
30
                return Unpacker::unpackFloat64(
31
                    substr($bytes, 1, 8)
32
                );
33
34
            case Binn::BINN_INT64:
35
                return Unpacker::unpackInt64(
36
                    substr($bytes, 1, 8)
37
                );
38
39
            case Binn::BINN_INT32:
40
                return Unpacker::unpackInt32(
41
                    substr($bytes, 1, 4)
42
                );
43
44
            case Binn::BINN_INT16:
45
                return Unpacker::unpackInt16(
46
                    substr($bytes, 1, 2)
47
                );
48
49
            case Binn::BINN_INT8:
50
                return Unpacker::unpackInt8(
51
                    $bytes[1]
52
                );
53
54
            case Binn::BINN_UINT64:
55
                return Unpacker::unpackUint64(
56
                    substr($bytes, 1, 8)
57
                );
58
59
            case Binn::BINN_UINT32:
60
                return Unpacker::unpackUint32(
61
                    substr($bytes, 1, 4)
62
                );
63
64
            case Binn::BINN_UINT16:
65
                return Unpacker::unpackUint16(
66
                    substr($bytes, 1, 2)
67
                );
68
69
            case Binn::BINN_UINT8:
70
                return Unpacker::unpackUint8($bytes[1]);
71
72
            case Binn::BINN_STRING:
73
                return $this->decodeString($bytes);
74
75
            case Binn::BINN_STORAGE_BLOB:
76
                return $this->decodeString($bytes);
77
        }
78
79
        return null;
80
    }
81
82
    public function supportsDecoding(string $bytes): bool
83
    {
84
        $type = $this->detectType($bytes);
85
86
        return in_array($type, [
87
            Binn::BINN_NULL,
88
            Binn::BINN_TRUE,
89
            Binn::BINN_FALSE,
90
            Binn::BINN_FLOAT32,
91
            Binn::BINN_FLOAT64,
92
            Binn::BINN_INT64,
93
            Binn::BINN_INT32,
94
            Binn::BINN_INT16,
95
            Binn::BINN_INT8,
96
            Binn::BINN_UINT64,
97
            Binn::BINN_UINT32,
98
            Binn::BINN_UINT16,
99
            Binn::BINN_UINT8,
100
            Binn::BINN_STRING,
101
            Binn::BINN_STORAGE_BLOB,
102
        ], true);
103
    }
104
105
    private function decodeString(string $bytes): string
106
    {
107
        $size = Unpacker::unpackSize8($bytes[1]);
108
        $offset = 2;
109
110
        if ($size > Binn::BINN_MAX_ONE_BYTE_SIZE) {
111
            $sizeBytes = substr($bytes, 1, 4);
112
            $size = Unpacker::unpackSize32($sizeBytes);
113
            $offset = 5;
114
        }
115
116
        $stringBytes = substr($bytes, $offset, $size);
117
118
        return Unpacker::unpackString($stringBytes);
119
    }
120
}
121