SimpleTypeValueEncoder::encodeType()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Knik\Binn\Encoder;
4
5
use Knik\Binn\Binn;
6
use Knik\Binn\Contracts\BinnValueEncoder;
7
8
class SimpleTypeValueEncoder implements BinnValueEncoder
9
{
10
    public function encode($value): string
11
    {
12
        $type = $this->detectType($value);
13
14
        return $this->encodeType($type) . $this->encodeValue($type, $value);
0 ignored issues
show
Bug introduced by
It seems like $type can also be of type null; however, parameter $type of Knik\Binn\Encoder\Simple...eEncoder::encodeValue() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

14
        return $this->encodeType($type) . $this->encodeValue(/** @scrutinizer ignore-type */ $type, $value);
Loading history...
Bug introduced by
It seems like $type can also be of type null; however, parameter $type of Knik\Binn\Encoder\Simple...ueEncoder::encodeType() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

14
        return $this->encodeType(/** @scrutinizer ignore-type */ $type) . $this->encodeValue($type, $value);
Loading history...
15
    }
16
17
    public function encodeValue(int $type, $value = null): ?string
18
    {
19
        if ($type === Binn::BINN_NULL) {
20
            return '';
21
        }
22
23
        if ($type === Binn::BINN_TRUE) {
24
            return '';
25
        }
26
27
        if ($type === Binn::BINN_FALSE) {
28
            return '';
29
        }
30
31
        if ($type === Binn::BINN_UINT64) {
32
            return Packer::packUint64($value);
33
        }
34
35
        if ($type === Binn::BINN_UINT32) {
36
            return Packer::packUint32($value);
37
        }
38
39
        if ($type === Binn::BINN_UINT16) {
40
            return Packer::packUint16($value);
41
        }
42
43
        if ($type === Binn::BINN_UINT8) {
44
            return Packer::packUint8($value);
45
        }
46
47
        if ($type === Binn::BINN_INT8) {
48
            return Packer::packInt8($value);
49
        }
50
51
        if ($type === Binn::BINN_INT16) {
52
            return Packer::packInt16($value);
53
        }
54
55
        if ($type === Binn::BINN_INT32) {
56
            return Packer::packInt32($value);
57
        }
58
59
        if ($type === Binn::BINN_INT64) {
60
            return Packer::packInt64($value);
61
        }
62
63
        if ($type === Binn::BINN_FLOAT32) {
64
            return Packer::packFloat32($value);
65
        }
66
67
        if ($type === Binn::BINN_FLOAT64) {
68
            return Packer::packFloat64($value);
69
        }
70
71
        if ($type === Binn::BINN_STRING) {
72
            return Packer::packSize(strlen($value)) . Packer::packString($value) . "\x00";
73
        }
74
75
        return null;
76
    }
77
78
    public function supportsEncoding($value): bool
79
    {
80
        return $this->detectType($value) !== null;
81
    }
82
83
    public function encodeType(int $type): ?string
84
    {
85
        return $this->encodeValue(Binn::BINN_UINT8, $type);
86
    }
87
88
    private function detectType($value): ?int
89
    {
90
        if (is_bool($value)) {
91
            return $value ? Binn::BINN_TRUE : Binn::BINN_FALSE;
92
        }
93
94
        if (is_string($value)) {
95
            return Binn::BINN_STRING;
96
        }
97
98
        if (is_int($value)) {
99
            return $this->detectInt($value);
100
        }
101
102
        if (is_float($value)) {
103
            if (strlen($value) > 4) {
104
                return Binn::BINN_FLOAT64;
105
            }
106
107
            return Binn::BINN_FLOAT32;
108
        }
109
110
        if (is_null($value)) {
111
            return Binn::BINN_NULL;
112
        }
113
114
        return null;
115
    }
116
117
    public function detectInt($value): int
118
    {
119
        if ($value < 0) {
120
            // int
121
            if ($value >= Binn::INT8_MIN) {
122
                return Binn::BINN_INT8;
123
            }
124
125
            if ($value >= Binn::INT16_MIN) {
126
                return Binn::BINN_INT16;
127
            }
128
129
            if ($value >= Binn::INT32_MIN) {
130
                return Binn::BINN_INT32;
131
            }
132
133
            return Binn::BINN_INT64;
134
        }
135
136
        // uint
137
        if ($value <= Binn::UINT8_MAX) {
138
            return Binn::BINN_UINT8;
139
        }
140
141
        if ($value <= Binn::UINT16_MAX) {
142
            return Binn::BINN_UINT16;
143
        }
144
145
        if ($value <= Binn::UINT32_MAX) {
146
            return Binn::BINN_UINT32;
147
        }
148
149
        return Binn::BINN_UINT64;
150
    }
151
}
152