Passed
Push — master ( ec031f...0f6d00 )
by Chris
03:11
created

datetimeinterface_to_datetimeimmutable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
ccs 4
cts 4
cp 1
crap 2
rs 9.6666
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace DaveRandom\LibLifxLan;
4
5
// @codeCoverageIgnoreStart
6
if (\strlen(\pack('e', 0.1)) === 4) {
7
    \define(__NAMESPACE__ . '\\FLOAT32_CODE', 'e');
8
} else if (\strlen(\pack('g', 0.1)) === 4) {
9
    \define(__NAMESPACE__ . '\\FLOAT32_CODE', 'g');
10
} else {
11
    /** @noinspection PhpUnhandledExceptionInspection */
12
    throw new \Error('Cannot pack()/unpack() floating point numbers to a 32-bit little-endian representation');
13
}
14
15
const UINT32_MIN = \PHP_INT_SIZE === 4 ? \PHP_INT_MIN : 0;
16
const UINT32_MAX = \PHP_INT_SIZE === 4 ? \PHP_INT_MIN : 0xffffffff;
17
// @codeCoverageIgnoreEnd
18
19
/**
20
 * @param \DateTimeInterface $dateTime
21
 * @return \DateTimeImmutable
22
 */
23
function datetimeinterface_to_datetimeimmutable(\DateTimeInterface $dateTime): \DateTimeImmutable
24
{
25 23
    if ($dateTime instanceof \DateTimeImmutable) {
26 1
        return $dateTime;
27
    }
28
29 22
    \assert($dateTime instanceof \DateTime, new \Error('DateTimeInterface is not DateTimeImmutable or DateTime???'));
30
31 22
    return \DateTimeImmutable::createFromMutable($dateTime);
32
}
33