Completed
Push — master ( 0d7a8a...2e892a )
by Chris
03:11
created

validate_int16()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 2
dl 0
loc 9
ccs 4
cts 4
cp 1
crap 3
rs 9.6666
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace DaveRandom\LibLifxLan;
4
5
use DaveRandom\LibLifxLan\Exceptions\InvalidValueException;
6
7
// @codeCoverageIgnoreStart
8
foreach (['g', 'f', 'e'] as $format) {
9
    if (\pack($format, -0.0) === "\x00\x00\x00\x80") {
10
        \define(__NAMESPACE__ . '\\FLOAT32_CODE', $format);
11
        break;
12
    }
13
}
14
15
if (!\defined(__NAMESPACE__ . '\\FLOAT32_CODE')) {
16
    /** @noinspection PhpUnhandledExceptionInspection */
17
    throw new \Error('Cannot pack()/unpack() floating point numbers to a 32-bit little-endian representation');
18
}
19
// @codeCoverageIgnoreEnd
20
21
/**
22
 * @param \DateTimeInterface $dateTime
23
 * @return \DateTimeImmutable
24
 */
25
function datetimeinterface_to_datetimeimmutable(\DateTimeInterface $dateTime): \DateTimeImmutable
26
{
27 44
    if ($dateTime instanceof \DateTimeImmutable) {
28 19
        return $dateTime;
29
    }
30
31 25
    \assert($dateTime instanceof \DateTime, new \Error('DateTimeInterface is not DateTimeImmutable or DateTime???'));
32
33 25
    return \DateTimeImmutable::createFromMutable($dateTime);
34
}
35
36
/**
37
 * @param int $timestamp
38
 * @return \DateTimeImmutable
39
 */
40
function nanotime_to_datetimeimmutable(int $timestamp): \DateTimeImmutable
41
{
42 1
    static $utcTimeZone;
43
44 1
    $usecs = \abs((int)(($timestamp % 1000000000) / 1000));
45 1
    $secs = (int)($timestamp / 1000000000);
46
47 1
    $result = \DateTimeImmutable::createFromFormat(
48 1
        'u U',
49 1
        \sprintf("%06d %d", $usecs, $secs),
50 1
        $utcTimeZone ?? ($utcTimeZone = new \DateTimeZone('UTC'))
51
    );
52
53 1
    \assert($result !== false, new \Error("Could not convert nanotime to DateTimeImmutable instance"));
54
55 1
    return $result;
56
}
57
58
function datetimeinterface_to_nanotime(\DateTimeInterface $dateTime): int
59
{
60 2
    return (int)$dateTime->format('Uu000');
61
}
62
63
function int16_to_uint16(int $signed): int
64
{
65 2
    return $signed < 0
66 2
        ? ($signed & 0x7fff) + ($signed & 0x8000)
67 2
        : $signed & 0x7fff;
68
}
69
70
function uint16_to_int16(int $unsigned): int
71
{
72 2
    return $unsigned >= 0x8000
73 2
        ? ($unsigned & 0x7fff) - ($unsigned & 0x8000)
74 2
        : $unsigned & 0x7fff;
75
}
76
77
/**
78
 * @param string $description
79
 * @param int $int
80
 * @param int $min
81
 * @param int $max
82
 * @return int
83
 * @throws InvalidValueException
84
 */
85
function validate_int_range(string $description, int $int, int $min, int $max): int
86
{
87 101
    if ($int < $min || $int > $max) {
88 7
        throw new InvalidValueException(
89 7
            "{$description} '{$int}' outside allowable range of {$min} - {$max}"
90
        );
91
    }
92
93 96
    return $int;
94
}
95
96
/**
97
 * @param string $description
98
 * @param int $int
99
 * @return int
100
 * @throws InvalidValueException
101
 */
102
function validate_uint8(string $description, int $int): int
103
{
104 67
    if ($int < 0 || $int > 0xff) {
105 7
        throw new InvalidValueException(
106 7
            "{$description} '{$int}' outside allowable range of " . -0x8000 . " - " . 0xff
107
        );
108
    }
109
110 60
    return $int;
111
}
112
113
/**
114
 * @param string $description
115
 * @param int $int
116
 * @return int
117
 * @throws InvalidValueException
118
 */
119
function validate_int16(string $description, int $int): int
120
{
121 20
    if ($int < -0x8000 || $int > 0x7fff) {
122 3
        throw new InvalidValueException(
123 3
            "{$description} '{$int}' outside allowable range of " . -0x8000 . " - " . 0x7fff
124
        );
125
    }
126
127 17
    return $int;
128
}
129
130
/**
131
 * @param string $description
132
 * @param int $int
133
 * @return int
134
 * @throws InvalidValueException
135
 */
136
function validate_uint16(string $description, int $int): int
137
{
138 151
    if ($int < 0 || $int > 0xffff) {
139 25
        throw new InvalidValueException(
140 25
            "{$description} '{$int}' outside allowable range of " . 0 . " - " . 0xffff
141
        );
142
    }
143
144 132
    return $int;
145
}
146
147
/**
148
 * @param string $description
149
 * @param int $int
150
 * @return int
151
 * @throws InvalidValueException
152
 */
153
function validate_uint32(string $description, int $int): int
154
{
155 162
    if ($int < 0 || $int > 0xffffffff) {
156 35
        throw new InvalidValueException(
157 35
            "{$description} '{$int}' outside allowable range of " . 0 . " - " . 0xffffffff
158
        );
159
    }
160
161 137
    return $int;
162
}
163